Drawings SDK Developer Guide > Working with .dwg Files > Working with Other File Formats > Importing from Other Formats > Importing .pdf Files
Importing .pdf Files

PDF Import Support in ODA Drawings SDK

Drawings SDK supports importing a .pdf file to a .dwg database using the PdfImport extension module (TD_PdfImport.tx). The module is available for the following platforms and compilers:

  • Microsoft® Windows® (Microsoft Visual Studio® version 2015 or higher)
  • Linux (GCC version 4.8 or higher)
  • Apple® macOS® (Clang using macOS 10.9, 10.10, 10.11, 10.12, 10.13 SDKs)
  • iOS (iOS 12.0 SDK)
  • Google Android™ (with Android Google NDK r15c)

All source is available in the release packages (in the Drawing\Imports\PdfImport folder). For the API Reference, please refer to OdPdfImport Classes.

ODA Drawings SDK uses the Google PDFium library for importing from a .pdf file. This library is provided within the ODA Drawings archives and is located in the ThirdParty/pdfium folder. Note that the Google PDFium library requires compilers that support the C++11 standard.

PDF Import Process

To import .pdf file content to a drawing database:

  1. Load the PDF import module:
    
    OdRxModulePtr pModule = ::odrxDynamicLinker()->loadModule(OdPdfImportModuleName, false);
            
  2. Create the PDF importer object and check whether it was successfully created:
    
    OdPdfImportPtr  pdfImporter = ((OdPdfImportModule*)pModule.get())->create();;
    if (pdfImporter.isNull())
      odPrintConsoleString(L"Pdf Importer object creation failed!\n");
            
  3. Set import parameters using the properties() method of the importer object:
    
    OdDbDatabasePtr pDb = pHostApp->createDatabase();
    pdfImporter->properties()->putAt(OD_T("Database"), db);
    OdString path = pHostApp->findFile(file_name);
    pdfImporter->properties()->putAt(OD_T("PdfPath"), OdRxVariantValue(path));
            

    It is assumed that pHostApp is a raw pointer to an instance of the OdDbHostAppServices class which provides platform-dependent functionality for ODA software.

    To see the full list of import parameters, refer to the PDF Import Parameters section.

  4. Run the import() method of the importer object and analyze the returned value:
    
              OdPdfImport::ImportResult importResult = pdfImporter->import();
              if (importResult != OdPdfImport::success)
                odPrintConsoleString(L"PdfImportResult = %d\n", (int) importResult);
            

    The table below illustrates results of the import operation.

    Value Description
    success The import process successfully finished.
    fail The import process failed.
    bad_password The import process failed because of an incorrect password.
    bad_file The import process failed because of an incorrect input .pdf file.
    bad_database The import process failed because of an incorrect destination database object.
    encrypted_file The import process failed because of an encrypted input .pdf file.
    invalid_page_number The import process failed because of an invalid page number in the input .pdf file.
    image_file_error The import process failed because of an error while saving the image file (the possible reason is an invalid directory for saving).

  5. Do not forget to unload the PDF import module after the import is finished:
    
    ::odrxDynamicLinker()->unloadModule(OdPdfImportModuleName);
            

PDF Import Parameters

Drawings SDK supports a set of PDF import parameters. These parameters are stored in a dictionary object that can be retrieved for reading and modifying by calling the properties() method of the OdPdfImport class. Each parameter is accessible by its name represented with a string. To set a new parameter value, use the putAt() method of the dictionary object, which assumes two arguments:

  • Parameter name.
  • A new value of the parameter.

There are two mandatory PDF import parameters:

  • The target drawing database where the imported .pdf file content should be stored:
    
    pdfImporter->properties()->putAt(OD_T("Database"), db);
            
  • The full path to the source .pdf file:
    
    pdfImporter->properties()->putAt(OD_T("PdfPath"), OdRxVariantValue(path));
            

The full list of import parameters with their access names are described in the following table.

Parameter Name Parameter Brief Parameter Description
ApplyLineweight Line Weight Handling A boolean parameter that determines whether lineweight properties are retained (if equal to true) or ignored (if equal to false) during the import. By default this property is equal to true.
CharSpaceFactor Width of the Space between Characters Specifies the factor for the width of the space between characters in the word. If the distance between the test objects in the string is less than the width of the space taken from the font metric multiplied by this factor, the text objects are combined into one word. Only affects if the CombineTextObjects option is used (equal to 0.6 by default).
CombineTextObjects Text Combination Flag A flag that determines whether text objects that use the same font and are on the same line are combined (false by default).
ConvertSolidsToHatches Solids Conversion Handling A boolean parameter that determines whether two-dimensional solid objects are converted into solid filled hatches (if equal to true). By default this property is equal to true.
Database Database for Import An OdDbDatabase object where the contents of a .pdf file should be imported.
ImportAsBlock Importing as a Block A boolean parameter that determines whether the .pdf file is imported as a block (if equal to true) or as separate objects (if equal to false). By default this property is equal to false.
ImportGradientFills Gradient Fills Import Handling A boolean parameter that determines whether shaded objects are saved as .png image files and are attached to the current drawing database (if equal to true). By default this property is equal to true.
ImportInvalidTTTextAsGeometry Import of Invalid True Type Text A boolean parameter that determines whether text objects that use invalid TrueType fonts are imported as polylines and solid-filled hatches (if the flag value is equal to true) or are not imported at all (if the flag value is equal to false). The parameter is equal to true by default.
ImportRasterImages Raster Images Import Handling A boolean parameter that determines whether raster images are saved as .png files and are attached to the current drawing database (if equal to true). By default this property is equal to true. The directory used for saving is specified by the system variable PDFIMPORTIMAGEPATH. If the variable value is not set, the images are saved in the application's temporary directory.
ImportSolidFills Solid Fills Import Handling A boolean parameter that determines whether all solid filled areas are imported (if equal to true) or not (if equal to false). By default this property is equal to true.
ImportTrueTypeText True Type Text Import Handling A boolean parameter that determines whether text objects that use TrueType fonts are imported (if equal to true) or not (if equal to false). By default this property is equal to true. Some symbols exported from a PDF document may be displayed incorrectly. It happens because the PDF document uses symbols, which the origin TrueType font does not contain. For guaranteed correct displaying all symbols, please switch on the ImportTrueTypeTextAsGeometry option for the import process.
ImportTrueTypeTextAsGeometry True Type Text as Geometry A boolean parameter that determines whether text objects that use True Type fonts are imported as polylines and solid filled hatches (if equal to true). By default this property is equal to false.

To import text as geometry switch on the following import options:

ImportType3AsTrueTypeText Type3 to TrueType Text Conversion A boolean parameter that determines whether text objects that use Type3 fonts are imported as objects that use TrueType fonts (false by default). It is useful to set it to true when some symbols exported from an input PDF document are displayed incorrectly in the target database. It happens because the PDF document uses Type3 fonts glyphs, which the origin TrueType font does not contain.
ImportVectorGeometry Vector Geometry Import Handling A boolean parameter that determines whether a vector geometry data type (such as linear paths, Bezier curves, and solid-filled areas) are imported as polylines and 2D solids or solid-filled hatches. By default this property is true.
InsertionPointX Insertion Point An X-axis coordinate of the imported .pdf content location relative to the location of the current UCS. By default this property is equal to 0.0.
InsertionPointY Insertion Point Y A Y-axis coordinate of the imported .pdf content location relative to the location of the current UCS. By default this property is equal to 0.0.
JoinLineAndArcSegments Polyline and Arcs Joining A boolean parameter that determines whether contiguous segments are joined into a polyline when it is possible (if equal to true). By default this property is equal to true.
LayersUseType Layers Import Handling Defines how imported objects are assigned to layers in the target drawing database. Can accept the following values:
  • 0 — Uses .pdf layers. Layers are created based on layer names from the input .pdf document. Each layer's visibility is determined by its visibility in the .pdf document. If the origin object is not assigned a layer, it is assigned to a layer according to option 1 for creating object layers. If the object in the .pdf document is located on more than one layer, when it is imported it is set to the "inner" layer.
  • 1 — Creates object layers. Creates a predefined list of layers for visible objects in the target database:
    • PDF_Geometry
    • PDF_Solid Fills
    • PDF_Images
    • PDF_Text, etc.
    For invisible objects, specific layers are created as well:
    • PDF_Geometry_Invisible
    • PDF_Solid Fills_Invisible
    • PDF_Images_Invisible
    • PDF_Text _Invisible, etc.
  • 2 — Uses the current layer. Imports all visible PDF objects to the current layer. For invisible objects, invisible layers are created (PDF_Geometry_Invisible, PDF_Solid Fills_Invisible, PDF_Images_Invisible, and PDF_Text _Invisible).
By default this property is equal to 1.
PageNumber Page Number to Import A number of the page to import from the input .pdf file. Starts from 1. By default the property is equal to 1. Only one page from the original .pdf document can be imported.
Password PDF File Password A password for the input .pdf file. By default this property is empty.
PdfPath PDF File Path A full path to the input .pdf file.
Rotation Import Rotation Specifies a different rotation angle for imported objects. By default this property is equal to 0.0.
Scaling Import Scaling Specifies a different import scale factor for imported objects. By default this property is equal to 1.0.
SpaceFactor Factor of the Width for the Space Between Words Defines the factor for the width of the space between words on a line. If the distance between the test objects in the string is greater than the width of the space between characters in the word (specified by the CharSpaceFactor parameter), but less than the width of the space taken from the font metrics multiplied by this factor, the text objects are combined into one word. Only affects if CombineTextObjects is used (equal to 1.5 by default).
TakeAddMeasurementFactors Additional Measurement Factors Usage A flag that determines whether additional measurement factors are used for the pdf page (true by default).
UseGeometryOptimization Geometry Optimization A boolean parameter that determines whether geometry optimization should be used during the import operation (if equal to true). By default this property is equal to true. Geometry optimization assumes that objects that resemble arcs, circles, and ellipses are interpolated as such within a tolerance. Using the geometry optimization can reduce operation performance.
UseClipping Clipping Usage A flag that determines whether clipping support is used at import (false by default).
UsePageBorderClipping A flag that determines whether clipping support is used at the border of the page. Only affects if UseClipping is used (false by default).
UsePdfTransparency PDF Transparency Usage A flag that determines whether transparency for geometric objects is used from a PDF stream (if true) or set to 50% for objects that will be imported as solid-filled areas (if false). By default, the property is equal to false.
UseProgressMeter Using the Progress Meter A boolean parameter that determines whether progress metering should be used during the import operation (if equal to true). By default this property is equal to true. Note that using progress metering can reduce performance.
UseRgbColor Color Import Handling A boolean parameter that determines whether the import process uses RGB colors (it is equal to true) or colors are selected from the palette are used (if the flag is equal to false). The parameter is equal to false by default.

PDF Import Example Applications

An example of using PDF import functionality is located in the OdaMfcApp sample application. The sample source code is located in the Drawing\Examples\win\OdaMfcApp folder of the Drawings SDK installation directory. Note that the OdaMfcApp sample is available only for Windows platforms.

To import .pdf file content into a drawing database:

  1. Open or create a drawing to store the imported content of a .pdf file, then select File -> Import Pdf.

  2. Click Browse, then select the .pdf file to import. The dialog with import parameters appears.

  3. Set the import parameters, then click OK to add the .pdf file content to the database.
  4. Check the import result in the drawing structure and render the target drawing database:

OdaMfcApp also contains a sample command for PDF import: pdfin. This command imports .pdf file content in console mode and supports import from a PDF Underlay entity contained in the drawing database to the database itself.

To run the pdfin command:

  1. Select Edit -> Registered Commands -> IMPORTS -> pdfin:

  2. Choose either importing from a separate .pdf file or from an underlay entity:

If you can't find the pdfin command in the application menu, the appropriate module was not loaded. To load it, select Tools -> Load Applications. You will see the list of loaded modules:

Click Load and add the TD_PdfImport.tx file to the list. The concrete name of the file differs depending on the ODA software version and compiler. For example, for ODA version 2019 Update 1 and Microsoft Visual Studio 2017, the module file has the name TD_PdfImport_19.7_15.tx.

Source code of the PDF Import commands is available in the Drawing\Imports\PdfImport\Source\PdfImportCmd.h and PdfImportCmd.cpp files.

See Also

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.