使用 Aspose.PDF 在 PDF 中嵌入 TTF 字体

dir*_*irq 2 .net c# embed fonts aspose

使用 Aspose.PDF .NET 10.4、C#、.NET 4.5

我的应用程序的资源文件夹中有一个 .TTF 字体文件。它没有安装在系统中,我也不希望这样做。如何嵌入字体并在文档中使用它?

dir*_*irq 5

谢天谢地 ILSpy。我在 Aspose.PDF dll 中搜索了“字体”,终于找到了。下面的完整命名空间让您知道从哪里获取方法和对象。

//create the font from a ttf file
Aspose.Pdf.Text.Font myFont =
    Aspose.Pdf.Text.FontRepository.OpenFont("C:\temp\MyFont-Regular.ttf");
myFont.IsEmbedded = true;

//new doc, add a new blank page
Document doc = new Document();
Page page = doc.Pages.Add();

//use the font in a style
TextState style = new TextState();
style.Font = myFont;
style.FontSize = 12;
style.FontStyle = FontStyles.Regular;
style.LineSpacing = 4;

TextFragment frag = new TextFragment(sb.ToString());
frag.TextState.ApplyChangesFrom(style);
frag.IsInLineParagraph = true;
page.Paragraphs.Add(frag);

//save it
doc.Save("C:\temp\fontTest.pdf");
Run Code Online (Sandbox Code Playgroud)