Embed TrueType font

Fonts, Generate PDF
8/11/2015

This code sample shows how to draw text on a page using a TrueType font and embed this font in the PDF.

pdf

PDFKit.NET embeds a subset of the original font based on the characters that are actually used. By doing this, it is possible to use a 40Mb font and generate a PDF of only 20kb while still embedding all glyph information.

C# code sample

1 using System; 2 using System.IO; 3 4 using TallComponents.PDF; 5 using TallComponents.PDF.Shapes; 6 using TallComponents.PDF.Fonts; 7 using TallComponents.PDF.Transforms; 8 9 namespace EmbedTrueType 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 // Create a new PDF document and add a page (Letter) 16 Document pdf = new Document(); 17 Page page = new Page(PageSize.Letter); 18 pdf.Pages.Add(page); 19 20 // Load a TrueType font and set the embed mode. 21 // You may select other embed modes such as Full or ReferenceOnly. 22 Font font = Font.TrueType("segoepr.ttf"); 23 font.EmbedMode = EmbedMode.Auto; 24 25 // Add text at the top of the page and set the font 26 TextShape text = new TextShape("This text uses Segue Regular (segoepr.ttf)", font, 18); 27 text.Transform = new TranslateTransform( 72, page.Height - text.MeasuredHeight - 72); 28 page.Overlay.Add(text); 29 30 // Write the PDF to file 31 using (FileStream file = new FileStream("out.pdf", FileMode.Create, FileAccess.Write)) 32 { 33 pdf.Write(file); 34 } 35 } 36 } 37 }

VB.NET code sample

1 Sub Main() 2 3 ' Create a new PDF document and add a page (Letter) 4 Dim pdf As New Document() 5 Dim page As New Page(PageSize.Letter) 6 pdf.Pages.Add(page) 7 8 ' Load a TrueType font and set the embed mode. 9 ' You may select other embed modes such as Full or ReferenceOnly. 10 Dim font1 As Font = Font.TrueType("segoepr.ttf") 11 font1.EmbedMode = EmbedMode.Auto 12 13 ' Add text at the top of the page and set the font 14 Dim text As New TextShape("This text uses Segue Regular (segoepr.ttf)", font1, 18) 15 text.Transform = New TranslateTransform(72, page.Height - text.MeasuredHeight - 72) 16 page.Overlay.Add(text) 17 18 ' Write the PDF to file 19 Using file As New FileStream("out.pdf", FileMode.Create, FileAccess.Write) 20 pdf.Write(file) 21 End Using 22 End Sub

If you open the document properties dialog (CTRL+D) and then select the fonts tab, you will see that the font is embedded.

doc-props