简体   繁体   English

使用Aspose.PDF将TTF字体嵌入PDF

[英]Embed a TTF font in a PDF using Aspose.PDF

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

I have a .TTF font file in a resources folder of my application. 我的应用程序的资源文件夹中有一个.TTF字体文件。 It is not installed in the system, nor do I wish to do so. 它未安装在系统中,也不希望这样做。 How do I embed the font and use it in a Document? 如何嵌入字体并在文档中使用它?

Thank goodness for ILSpy. 感谢ILSpy。 I searched through the Aspose.PDF dll for "font" and finally figured it out. 我在Aspose.PDF dll中搜索“字体”,最后找到了答案。 Full namespaces below so you know where to get the methods and objects. 下面是完整的名称空间,因此您知道从何处获取方法和对象。

//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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM