Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Set Preferable Fonts for PDF Import

When importing text objects from PDF to .dwg, the font used by text objects is often missing from the system. In older ODA SDK versions, all fonts that were not found were replaced with Arial, but starting with version 22.5 you can replace any font used in a PDF document with a preferred font when importing.

Note: To select a preferred Type3 font, switch on the ImportType3AsTrueTypeText import option (see PDF Import Parameters for details). Type3 fonts are typically used to represent SHX fonts during PDF export.

The callback function mechanism provides the ability to select a preferred font. You can set a special function that returns the preferred font name for the importer object (an instance of a class derived from the OdPdfImport abstract class) by a call of the setPreferableFontCallback() method, which accepts an STL wrapper of a function that determines the preferable font:

      
std::function<OdAnsiString(const char*, const char*, const bool)>
      
    

This function accepts the following arguments:

  • The origin font's name. This name is the same as the name displayed by Adobe® Acrobat® in the document properties.

  • The font family name. For example, the family name for the Arial-BoldItalicMT font is Arial.
  • A flag that defines whether the specified font is found in the operating system.

The function returns either the name of the preferred font or an empty string if font substitution is not needed. The substitution font can also be an SHX font. In this case, the name of the font file is returned, for example: romans.shx.

Below is example source code that sets a function wrapper to determine the preferable font:

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

PdfImporterExPtr pImporter = pModule->create();
OdRxDictionaryPtr pProps = pImporter->properties();
 
// fill main PdfImport properties

//Set the preferred font selection function
pImporter->setPreferableFontCallback([](const char* font_name, const char* font_family, const bool is_found)
            {
              OdAnsiString ret;
              if (!is_found)
                ret = " MS Gothic";
              static const OdAnsiString times_str("Times New Roman");
              if (0 == times_str.compare(font_family))
                ret = "Tahoma";
              return ret;
            });
  
OdPdfImport::ImportResult importResult = pImporter->import();
      
    

In this example, all fonts that are not found within the operating system are substituted with the MS Gothic font, fonts that are based on the Times New Roman font family are substituted with the Tahoma font, and other fonts remain unchanged.

You can find another example of preferred font selection in the PdfImportCommand example.

Note: The font replacement mechanism takes precedence over the value of the Type3TextDefaultFont property.

The process of choosing the font for imported text can be summarized as:

  • The font with the name returned by the function is used if:
    • The preferred font selection function is set
    • The preferred font selection function returns a non-empty string
  • The default font for Type3 fonts is used if:
    • The text object uses a Type3 font
    • No preferred font is set within the preferred font selection function
    • The default font for Type3 fonts is set (a value of the Type3TextDefaultFont import property)
  • In all other cases:
    • If the font is found in the operating system, the found font is used; otherwise, the Arial font is used

See Also

Embed Fonts
PDF Import Example Applications
Font Handling for PDF Import
Importing PDF Files
OdPdfImport Classes
Copyright © 2002 – 2022. Open Design Alliance. All rights reserved.