简体   繁体   中英

Embed a TTF font in a PDF using Aspose.PDF

Using Aspose.PDF .NET 10.4, C#, .NET 4.5

I have a .TTF font file in a resources folder of my application. 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. I searched through the Aspose.PDF dll for "font" and finally figured it out. 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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