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

本文关键字:PDF TTF 字体 Aspose 使用 | 更新日期: 2023-09-27 17:50:34

使用Aspose.PDF .NET 10.4, c#, .NET 4.5

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

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

感谢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");