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

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

This section illustrates how to split a multi-page PDF into separate 1-page PDFs.

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
   HMIGEAR document = 0;
   IG_mpi_create(&document, 0);
   IG_mpi_file_open((LPSTR)fileIn.c_str(), document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);

Next, we save each page as its own PDF document:

C and C++
Copy Code
   UINT pageCount;
   // Where to insert a page if saveMode is IG_MPI_SAVE_APPEND. -1 appends at the end.
   UINT insertIndex = -1;
   UINT pagesPerNewPDF = 1;
   char fileName[12];
   unsigned int i = 0;
   // Get the number of pages in the document.
   IG_mpi_page_count_get(document, &pageCount);
   // Iterate through each page.
   for (i = 0; i < pageCount; i++)
   {
       // Create filename based on page number.
       snprintf(fileName, 12, "page%03d.pdf", i);
       // Save each page.
       IG_mpi_file_save(fileName, document, insertIndex, i, pagesPerNewPDF, IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);
   }
Is this page helpful?
Yes No
Thanks for your feedback.