PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.

Embed TrueType font

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 Embedded

Download PDFKit.NET 4.0
We will send you a download link

  • This field is for validation purposes and should be left unchanged.
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.