Do you own a Debenu Quick PDF Library 12/11/10/9/8/7? Upgrade to Debenu Quick PDF Library 13!

Foxit Quick PDF Library

Frequently Asked Question:

Return to FAQ Index

Extract fonts from pdf

Question

I've looked over the documentation, but it is less than clear (at least to me) how to extract a font and save it to a file. Anyone have some sample code?

Answer

I have provided some sample C# code for you below which demonstrates how to save fonts from a PDF file to disk. Please note that this code will only work for embedded TrueType fonts. All other font types and subsetted TrueType fonts are not supported by the SaveFontToFile function.

QP.LoadFromFile("fonts.pdf");
for (int i = 1; i < QP.FindFonts(); i++)
{
    int FontID = QP.GetFontID(i);
    QP.SelectFont(FontID);
    if (QP.FontType() == 4)
    {
        QP.SaveFontToFile(QP.FontName() + ".ttf");
    }  
}

Some details:

  1. First you need to load the PDF using the LoadFromFile function or one of the other LoadFrom* functions.
  2. Then you need to count all of the fonts in the document using the FindFonts function. This function will return the total number of fonts in the document. Use this as an index to loop through each font in the document -- starting from 1 to the total number of fonts.
  3. While looping through the index of the fonts, retrieve the fond ID and use this ID to select the font using the SelectFont function.
  4. Only embedded TrueType fonts are supported by the SaveFontToFile function, so you need to use the FontType function to filter our all fonts that are not embedded TrueType fonts.
  5. Finally, now you can save the embedded TrueType fonts to disk using the SaveFontToFile function as they are discovered in the loop.

© 2015 Debenu & Foxit. All rights reserved. AboutBuyContactBlogNewsletterSupportFAQProduct UpdatesForum