Move PDF pages in C++
Contents
[
Hide
]
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.
- Open the input PDF and create a destination Document.
- Call CopyPage() to copy page 2 into the destination.
- Call Delete() to remove page 2 from the source.
- 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.
- Open the input PDF and create a destination Document.
- Copy pages 1 and 2 into the destination using CopyPage().
- Save the destination with Save().
- Pass the selected page array to Delete() on the source.
- 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.
- Open the input PDF as a Document.
- Append a copy of page 2 using CopyPage().
- Remove the original page 2 with Delete().
- 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);
}