eDocEngine VCL
Create documents and reports in 18 formats
Compatibility
Delphi C++Builder

How To Selectively Embed Fonts When Creating PDF Files

Are embedded TrueType fonts consuming too much file size in the PDF files you create? How about embedding fonts selectively - only the ones you need to. Here is the trick to do just that!
By S. Vikram & S. Bhamini
Please click here for the eDocEngine ActiveX/.NET version of this article.

To ensure that a PDF file is truly portable, fonts used in the document need to be embedded in the file.

In case of certain TrueType fonts, such as Arial and Courier New, every Windows system is already bundled with them. Hence, embedding these fonts can only serve to increase size of the PDF file.

A good solution would be to embed only those TrueType fonts that are not always likely to be found on all systems, while excluding those that can reasonably be expected to be present everywhere.

This article deals with such a solution. We will walk you through the steps:

  1. Use gtPDFEngine component for creating the document
    1. Drop the gtPDFEngine component on the form from the eDocEngine tab on the Delphi component palette.
    2. Drop a button component on the form.
    3. Handle the OnClick event of the button and use the gtPDFEngine component to start creating the PDF
  2. Specify properties to selectively embed fonts
    1. Set the property gtPDFEngine1.Preferences.EmbedTrueTypeFonts to indicate that you want to embed fonts used in the PDF document.
    2. Add the names of the fonts you DO NOT want to embed to gtPDFEngine1.FontEmbedExcludeList. (This is the trick!).
    NOTE: Font names are case-sensitive.
      with gtPDFEngine1 do
      begin
        ...
        Preferences.EmbedTrueTypeFonts := etfFull;
        FontEmbedExcludeList.Add('Arial');
        FontEmbedExcludeList.Add('Courier New');
        FontEmbedExcludeList.Add('Verdana');
        ...
    
  3. Render text in the PDF using different fonts
    1. Set gtPDFEngine1.Font for the text to be rendered.
    2. Render the text using the method gtPDFEngine1.TextOut(...)
    3. Repeat these steps for more text with different fonts.
    4. Complete the rest of the code and run the project to generate the PDF.
        ...
        Font.Name := 'Arial';
        TextOut(1, 1,
            'This sentence is in Arial font.');
        Font.Name := 'Times New Roman';
        TextOut(1, 2,
            'This sentence is in Times New Roman font.');
        Font.Name := 'Courier New';
        TextOut(1, 3,
            'This sentence is in Courier New font.');
        ...
    
  4. Verify which of the fonts have been embedded in the generated PDF file
    1. Open the generated PDF file in Adobe® Reader®.
    2. Choose File » Document Properties ... option from the menu or press the Ctrl+D keyboard shortcut.
    3. Switch to Fonts tab.
    4. You should now be able to confirm that select fonts have not been embedded. See screenshot.
---o0O0o---

Downloads:

Privacy | Legal | Feedback | Newsletter © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.