我正在研究一个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'。