Move PDF Pages programmatically C++

Moving a Page from one PDF Document to Another

Moving PDF pages in document is very interesting and popular task. This topic explains how to move page from one PDF document to the end of another document using C++. To move an page we should:

  1. Create a Document class object with the source PDF file.
  2. Get Page from the the PageCollection collection’s.
  3. Add page to the destination document.
  4. Save the output PDF using the Save method.
  5. Delete page in source document.
  6. Save the source PDF using the Save method.

The following code snippet shows you how to move one page.

void MovePage()
{
    // Open document
    String _dataDir("C:\\Samples\\");
    String srcFileName("<enter file name>");
    String dstFileName("<enter file name>");

    auto srcDocument = MakeObject<Document>(_dataDir + srcFileName);
    auto dstDocument = MakeObject<Document>();

    auto page = srcDocument->get_Pages()->idx_get(2);
    dstDocument->get_Pages()->Add(page);
    // Save output file
    dstDocument->Save(srcFileName);
    srcDocument->get_Pages()->Delete(2);
    srcDocument->Save(dstFileName);
}

Moving bunch of Pages from one PDF Document to Another

  1. Create a Document class object with the source PDF file.
  2. Define an array with page numbers to be moved.
  3. Run loop through array:
  4. Get Page from the the PageCollection collection’s.
  5. Add page to the destination document.
  6. Save the output PDF using the Save method.
  7. Delete page in source document.
  8. Save the source PDF using the Save method.

The following code snippet shows you how to insert an empty page at the end of a PDF file.

void MoveBunchPages()
{
    // Open document
    String _dataDir("C:\\Samples\\");
    String srcFileName("<enter file name>");
    String dstFileName("<enter file name>");

    auto srcDocument = MakeObject<Document>(_dataDir + srcFileName);
    auto dstDocument = MakeObject<Document>();


    auto pages = MakeArray<int>({ 1,3 });

    for (auto pageIndex : pages)
    {
        auto page = srcDocument->get_Pages()->idx_get(pageIndex);
        dstDocument->get_Pages()->Add(page);
    }
    // Save output files
    dstDocument->Save(srcFileName);
    srcDocument->get_Pages()->Delete();
    srcDocument->Save(dstFileName);
}

Moving a Page in new location in the current PDF Document

  1. Create a Document class object with the source PDF file.
  2. Get Page from the the PageCollection collection’s. collection’s.
  3. Add page to the new location (for example to end).
  4. Delete page in previous location.
  5. Save the output PDF using the Save method.
void MovePagesInOnePDF()
{
    // Open document
    String _dataDir("C:\\Samples\\");
    String srcFileName("<enter file name>");
    String dstFileName("<enter file name>");

    auto srcDocument = MakeObject<Document>(_dataDir + srcFileName);
    auto dstDocument = MakeObject<Document>();

    auto page = srcDocument->get_Pages()->idx_get(2);
    srcDocument->get_Pages()->Add(page);
    srcDocument->get_Pages()->Delete(2);

    // Save output file
    srcDocument->Save(dstFileName);
}