Extract PDF pages in C++
Contents
[
Hide
]
Copy selected PDF pages into a new document while retaining the input file using Aspose.PDF for C++.
Extract a single page
The input PDF must contain page 2. The source document is not modified.
- Open the input PDF and create an output Document.
- Call CopyPage() to copy page 2 into the output document.
- Save the output 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 ExtractPagesExamples.extractPage.
void ExtractPage(System::String inputFile, System::String outputFile)
{
auto srcDocument = MakeObject<Document>(inputFile);
auto dstDocument = MakeObject<Document>();
dstDocument->get_Pages()->CopyPage(srcDocument->get_Pages()->idx_get(2));
dstDocument->Save(outputFile);
}
Extract multiple pages
The input PDF must contain at least three pages.
- Open the input PDF and create an output Document.
- Create an array containing page numbers 2 and 3.
- Call CopyPage() for each selected page in the supplied order.
- Save the output 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 ExtractPagesExamples.extractBunchPages.
void ExtractBunchPages(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto anotherDocument = MakeObject<Document>();
auto pages = MakeArray<int32_t>({2, 3});
for (int pageIndex : pages) {
anotherDocument->get_Pages()->CopyPage(document->get_Pages()->idx_get(pageIndex));
}
anotherDocument->Save(outputFile);
}