PdfSharpCore 中文漢字問題處理


  PdfSharpCore是PdfSharp 的.net core版。

問題:
  使用PdfSharpCore會遇到中文亂碼問題。我們首先想到的是引用自定義字體。在PdfSharp中解決方案一般代碼如下:

1 System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
2 string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字體設置為微軟雅黑
3 pfcFonts.AddFontFile(strFontPath);
4 XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
5 XFont font = new XFont(pfcFonts.Families[0], 15, XFontStyle.Regular, options);

  然而在PdfSharpCore中這樣是不行的。按照代碼我們應該是這樣的:

1 System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
2 string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字體設置為微軟雅黑
3 pfcFonts.AddFontFile(strFontPath);
4 XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode);
5 XFont font = new XFont(pfcFonts.Families[0].Name, 15, XFontStyle.Regular, options);

  但是這樣我們始終獲取不到我們指定的字體。

 

原因:

  這個問題是因為XFont構造時,會找入參的字體,如果沒有找到指定字體,會找已安裝ttf格式字體的第一個。而第一個往往是不支持中文的,所以需要手動指定中文字體。XFont構造代碼:

1 XFont font = new XFont("字體名", 20);

"字體名"具體是什么呢?字體中文名,還是?
為了找到答案,下面貼出PdfSharpCore字體查找的部分源碼:

 1 public virtual FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
 2 {
 3     if (InstalledFonts.Count == 0)
 4         throw new FileNotFoundException("No Fonts installed on this device!");
 5 
 6     if (InstalledFonts.TryGetValue(familyName.ToLower(), out var family))
 7     {
 8         if (isBold && isItalic)
 9         {
10             if (family.FontFiles.TryGetValue(XFontStyle.BoldItalic, out string boldItalicFile))
11                 return new FontResolverInfo(Path.GetFileName(boldItalicFile));
12         }
13         else if (isBold)
14         {
15             if (family.FontFiles.TryGetValue(XFontStyle.Bold, out string boldFile))
16                 return new FontResolverInfo(Path.GetFileName(boldFile));
17         }
18         else if (isItalic)
19         {
20             if (family.FontFiles.TryGetValue(XFontStyle.Italic, out string italicFile))
21                 return new FontResolverInfo(Path.GetFileName(italicFile));
22         }
23 
24         if (family.FontFiles.TryGetValue(XFontStyle.Regular, out string regularFile))
25             return new FontResolverInfo(Path.GetFileName(regularFile));
26 
27         return new FontResolverInfo(Path.GetFileName(family.FontFiles.First().Value));
28     }
29 
30     if (NullIfFontNotFound)
31         return null;
32 
33     string ttfFile = InstalledFonts.First().Value.FontFiles.First().Value;
34     return new FontResolverInfo(Path.GetFileName(ttfFile));
35 }

通過以上源碼,不難知道這個"字體名"是字體的英文名!

到這里,我們也就知道了為啥下面這種方式為啥找不到中文字體了。

XFont font = new XFont(pfcFonts.Families[0].Name, 15, XFontStyle.Regular, options);

   原因:XFont構造時只支持字體英文名,而"pfcFonts.Families[0].Name"獲取的是字體中文名。

解決方案:

  知道原因那就好解決了,只需構造時用字體英文名就好了。

1 XFont font = new XFont("字體英文名", 20);

  這里給出2種獲取英文名的途徑:

  1.windos環境下獲取本地已安裝的ttf格式字體英文名:

 1  public string GetWindowsFamilyName(string fontName = "方正楷體簡體")
 2  {
 3      // 注意,很多字體是收費的,所以找免費的字體,沒有的話正規網上下載免費字體安裝
 4      // 免費字體(可商用):方正黑體(FZHei-B01S)、方正書宋(FZShuSong-Z01S)、方正仿宋(FZFangSong-Z02S)、方正楷體(FZKai-Z03S)
 5      var fontDir = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\Fonts");
 6      string fontPathFile = Path.Combine($"{fontDir}\\{fontName}.ttf");
 7 
 8      FontDescription fontDescription = FontDescription.LoadDescription(fontPathFile);
 9      string fontFamilyName = fontDescription.FontFamily(CultureInfo.InvariantCulture);
10      return fontFamilyName;
11  }

 

  2.通過網址獲取:

  http://www.shangaoshuiyuan.com/前端/各字體一覽表/

 

字體版權問題:

  注意:字體商用時注意版權問題。

  比如方正系列的除了4種字體,其他都不是免費的:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM