Move PDF pages in C++

Move pages to a new PDF or move a page to the end of its original document using Aspose.PDF for C++.

Move a page to a new document

Supply distinct sourceOutputFile and outputFile paths. The input must contain page 2.

  1. Open the input PDF and create a destination Document.
  2. Call CopyPage() to copy page 2 into the destination.
  3. Call Delete() to remove page 2 from the source.
  4. Save the modified source and destination to separate paths with Save().

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;

// Port of MovePagesExamples.movePageFromOneDocumentToAnother.
void MovePageFromOneDocumentToAnother(System::String inputFile, System::String sourceOutputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto anotherDocument = MakeObject<Document>();

    anotherDocument->get_Pages()->CopyPage(document->get_Pages()->idx_get(2));
    document->get_Pages()->Delete(2);
    document->Save(sourceOutputFile);
    anotherDocument->Save(outputFile);
}

Move multiple pages to a new document

Use a PDF with at least two pages and distinct output paths.

  1. Open the input PDF and create a destination Document.
  2. Copy pages 1 and 2 into the destination using CopyPage().
  3. Save the destination with Save().
  4. Pass the selected page array to Delete() on the source.
  5. Save the modified source to sourceOutputFile with Save().

#include <system/array.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;

// Port of MovePagesExamples.moveBunchPagesFromOneDocumentToAnother.
void MoveBunchPagesFromOneDocumentToAnother(System::String inputFile, System::String sourceOutputFile, System::String outputFile)
{
    auto srcDocument = MakeObject<Document>(inputFile);
    auto dstDocument = MakeObject<Document>();

    auto pages = MakeArray<int32_t>({1, 2});
    for (int pageIndex : pages) {
        dstDocument->get_Pages()->CopyPage(srcDocument->get_Pages()->idx_get(pageIndex));
    }
    dstDocument->Save(outputFile);
    srcDocument->get_Pages()->Delete(pages);
    srcDocument->Save(sourceOutputFile);
}

Move page 2 to the end

This example moves page 2 to the end; it does not accept an arbitrary destination position.

  1. Open the input PDF as a Document.
  2. Append a copy of page 2 using CopyPage().
  3. Remove the original page 2 with Delete().
  4. Save the result with Save().

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;

// Port of MovePagesExamples.movePageInNewLocationInSameDocument.
void MovePageInNewLocationInSameDocument(System::String inputFile, System::String outputFile)
{
    auto srcDocument = MakeObject<Document>(inputFile);

    srcDocument->get_Pages()->CopyPage(srcDocument->get_Pages()->idx_get(2));
    srcDocument->get_Pages()->Delete(2);
    srcDocument->Save(outputFile);
}