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.

using System;
using System.IO;

using TallComponents.PDF;
using TallComponents.PDF.Shapes;
using TallComponents.PDF.Fonts;
using TallComponents.PDF.Transforms;

namespace EmbedTrueType
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create a new PDF document and add a page (Letter)
      Document pdf = new Document();
      Page page = new Page(PageSize.Letter);
      pdf.Pages.Add(page);

      // Load a TrueType font and set the embed mode.
      // You may select other embed modes such as Full or ReferenceOnly.
      Font font = Font.TrueType("segoepr.ttf");
      font.EmbedMode = EmbedMode.Auto;

      // Add text at the top of the page and set the font
      TextShape text = new TextShape("This text uses Segue Regular (segoepr.ttf)", font, 18);
      text.Transform = new TranslateTransform( 72, page.Height - text.MeasuredHeight - 72);
      page.Overlay.Add(text);

      // Write the PDF to file
      using (FileStream file = new FileStream("out.pdf", FileMode.Create, FileAccess.Write))
      {
        pdf.Write(file);
      }
    }
  }
}
Sub Main()

    ' Create a new PDF document and add a page (Letter)
    Dim pdf As New Document()
    Dim page As New Page(PageSize.Letter)
    pdf.Pages.Add(page)

    ' Load a TrueType font and set the embed mode.
    ' You may select other embed modes such as Full or ReferenceOnly.
    Dim font1 As Font = Font.TrueType("segoepr.ttf")
    font1.EmbedMode = EmbedMode.Auto

    ' Add text at the top of the page and set the font
    Dim text As New TextShape("This text uses Segue Regular (segoepr.ttf)", font1, 18)
    text.Transform = New TranslateTransform(72, page.Height - text.MeasuredHeight - 72)
    page.Overlay.Add(text)

    ' Write the PDF to file
    Using file As New FileStream("out.pdf", FileMode.Create, FileAccess.Write)
        pdf.Write(file)
    End Using
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