2017-02-27 132 views
0

我正在研究一个C#.NET WPF桌面应用程序,该应用程序将生成需要非特定字体的PDF,该字体不会安装在系统上。我正在使用PdfSharp-WPF v1.32。文件importantSc.ttf位于项目资源文件夹中。在PDFsharp中使用私有TTF字体

我最好的选择是我的URI路径是错误的,但我认为我是正确的。我的字体系列名称是正确的,因为它在字体安装在我的开发计算机上时工作正常。

using System; 
using PdfSharp.Drawing; 
using PdfSharp.Pdf; 
using PdfSharp.Pdf.IO; 
using Microsoft.Win32; 
using PdfSharp.Drawing.Layout; 
using System.Collections.Generic; 
using System.Windows.Input; 
using System.Windows; 

namespace MyProject 
{ 
    public class MyPdfFile 
    { 
     private PdfDocument doc; 

     public void MakePdf() 
     { 
      Double x1, y1;  
      XBrush brush = XBrushes.Black; 
      XGraphics xgf = XGraphics.FromPdfPage(page); 
      doc = new PdfDocument(); 
      PdfPage page = doc.AddPage(); 

      // Load in a private font to use. 
      XPrivateFontCollection privateFontCollection = new XPrivateFontCollection(); 
      String fontFamilyName = "Important Script AM"; 
      String fontUriPath = @"pack://application:,,,/importantSc.ttf"; 

      Uri fontUri = new Uri(fontUriPath); 
      privateFontCollection.Add(fontUri, "./#" + fontFamilyName); 

      XFont font = new XFont(fontFamilyName, 9, XFontStyle.Regular); // <-- the error happens here 

      //Add text to page 
      x1 = 1.96 * 72; 
      y1 = 3.25 * 72; 
      String printString = "I wish this would work!"; 
      xgf.DrawString(printString, font, brush, x1, y1); 

      xgf.Dispose(); 
     } 
    } 
} 

我收到以下错误:

'System.InvalidOperationException' 类型的未处理的异常发生在PdfSharp-WPF.dll

其他信息:无法获取匹配字形字体为'重要脚本AM'。

回答

0

您不问任何问题。
以下是一些注意事项。

使用PDFsharp 1.50(目前为beta 3b)字体处理效果更好。我会使用1.50,因为我认为新测试版比旧的“稳定版”更好。
使用IFontResolver可以避免处理“pack:”资源语法。

您可以使用JetBrains的DotNetPeek等工具来检查TTF是否真的嵌入程序集中,并且使用哪个名称。

您可以使用PDFsharp的源代码包并通过privateFontCollection.Add进行调试,以查看它是否可以找到字体以及是否可以找到哪些字体。

使用MCVE我可以帮助您调试。
https://stackoverflow.com/help/mcve