ImageGear for C and C++ on Linux v20.3 - Updated
Load and Save a PDF
[No Target Defined] > [No Target Defined] > Formats with Additional Functionality > PDF > How to... > Load and Save a PDF

Before working with a PDF document, make sure to initialize the PDF component (see Getting Started with PDF).

The following code snippet loads all pages from a PDF document from disk, to a multi-page vector document HMIGEAR:

C and C++
Copy Code
LPSTR inputPath = "MyFilePath.pdf";
HMIGEAR document = 0;
IG_mpi_create(&document, 0);
IG_mpi_file_open(inputPath, document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);

The following code saves all pages from the loaded PDF document:

C and C++
Copy Code
LPSTR outputPath = "mySavedPDF.pdf";
UINT startOutputPage = 0;
UINT startInputPage = 0;
UINT pageCount;
IG_mpi_page_count_get(document, &pageCount);
IG_mpi_file_save(outputPath, document, startOutputPage, startInputPage,
        pageCount, IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);

Please note that the arguments that control the page indexes are zero-based. Let's say your input document has two pages and you only want to save the second page to a new file. In this case you would use:

C and C++
Copy Code
LPSTR outputPath = "mySavedPDF.pdf";
UINT startOutputPage = 0;
UINT startInputPage = 1;
UINT pageCount = 1;
IG_mpi_file_save(outputPath, document, startOutputPage, startInputPage,
        pageCount, IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);

You can control other saving options using the different arguments for IG_mpi_file_save. For example, to append the first page of the source document to an existing destination document you would use:

C and C++
Copy Code
LPSTR outputPath = "mySavedPDF.pdf";
UINT startOutputPage = -1;
UINT startInputPage = 0;
UINT pageCount = 1;
IG_mpi_file_save(outputPath, document, startOutputPage, startInputPage,
        pageCount, IG_FORMAT_PDF, IG_MPI_SAVE_APPEND);

Saving a Password Protected PDF

To learn how to save a secured-pdf and work with PDF security in general, refer to Use PDF Security.

Saving a PDF as a Raster Image

To learn how to rasterize to a given resolution and save a PDF, refer to the Convert... PDF to Image topic.

Is this page helpful?
Yes No
Thanks for your feedback.