Drawings SDK Developer Guide > Working with .dwg Files > Working with Other File Formats > Importing from Other Formats > Importing PDF Files > Font Handling for PDF Import > Embed Fonts During PDF Import
Embed Fonts During PDF Import

Text objects in an imported PDF document may have embedded fonts. If the embedded font is not found in the operating system, it is replaced with the Arial font or with the preferred font determined through the corresponding algorithm (see Set Preferable Fonts for PDF Import for details).

If you want to make the imported text objects with embedded fonts look exactly the same as in the original PDF document, you need to extract these embedded fonts from the document. Please note that you can extract embedded fonts only if they match the TrueType standard.

Note: Embedded fonts generally contain only the set of glyphs used in the PDF document.

Support for embedded TrueType fonts in the PdfImport module is implemented through the following import properties:

  • ExtractEmbeddedFonts — A flag that determines whether embedded font extraction is used when importing (if it is possible). By default, this property is equal to false.
  • EbeddedFontsPath — A string parameter that determines the path to the directory where embedded fonts are extracted from during the PDF import process. If the property value is not set or is empty, the extracted font files are saved in the application's temporary directory. By default, the property is an empty value.

For additional information about PDF import parameters, see PDF Import Parameters.

In addition to the import properties, it is possible to use the extractEmbededFonts() method of the OdPdfImportEx class inherited from the OdPdfImport abstract class. The method saves embedded font data in a directory specified with the EbeddedFontsPath property if the ExtractEmbeddedFonts property is equal to true. The method returns a list of the full paths for saved font files.

TrueType fonts are not extracted and saved in the following cases:

  • A font from the same font family exists in the operating system and contains the necessary set of glyphs.
  • An embedded font can't be installed in the operating system (for example, if necessary data is missing).

Note: It is necessary to extract embedded TrueType fonts with the same settings as the PDF import properties, otherwise the result is not guaranteed.

Below is a code example that illustrates extracting embedded fonts:

      
OdPdfImportModulePtr pModule = ::odrxDynamicLinker()->loadModule(OdPdfImportModuleName, false);

PdfImporterExPtr pImporter = pModule->create();

OdRxDictionaryPtr pProps = pImporter->properties();

// fill main PdfImport properties

pImporter->properties()->putAt(OD_T("ExtractEmbeddedFonts"), OdRxVariantValue(true));
pImporter->properties()->putAt(OD_T("EbeddedFontsPath"), OdRxVariantValue(OD_T("C:\oda\embedded_fonts")));
  
//Extracting embedded fonts 
OdStringArray font_files_name;
pImporter->extractEmbededFonts(font_files_name);

//Install extracted embedded fonts for use in the application
EmbeddedFontHandler embedded_font_handler;
embedded_font_handler.AddFontList(font_files_name, pDb->appServices());

//Importing a PDF document
OdPdfImport::ImportResult importResult = pImporter->import();
      
    

The embedded_font_handler is an instance of a class for setting extracted embedded fonts for use in an application. An example implementation of such class is below:

      
class EmbeddedFontHandler
{
public:
  EmbeddedFontHandler() {};
  ~EmbeddedFontHandler()
  {
#if defined(OD_WINDOWS_DESKTOP)
    for (auto& elem : m_FontsList)
    {
      RemoveFontResourceW(elem.c_str());
    }
#endif
  }

  bool AddFontList(const OdStringArray& font_files_name, OdDbHostAppServices* pHostApp)
  {
    bool ret = true;
    for (auto& file_name : font_files_name)
    {
#if defined(OD_WINDOWS_DESKTOP)
      if(0 == AddFontResourceW(file_name.c_str()))
        ret = false;
#else
      OdTtfDescriptor descr;
      if (!pHostApp->ttfFileNameByDescriptor(descr, file_name))
        ret = false;
#endif
      m_FontsList.push_back(file_name);
    }
    return ret;
  }
  OdStringArray m_FontsList;
};
      
    

You can find another example of how to use this functionality in the PdfImportCommand example.

Note: Preferred font selection has higher priority over saving embedded fonts. This means that if the embedded font is set to the name of the font to be substituted, the embedded font file is not saved.

See Also

Setting Preferable Fonts for PDF Import
PDF Import Usage Examples
PDF Import Parameters
PDF Import Process
Importing PDF Files
OdPdfImport Classes
Copyright © 2002 – 2021. Open Design Alliance. All rights reserved.