Extract PDF pages in C++

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.

  1. Open the input PDF and create an output Document.
  2. Call CopyPage() to copy page 2 into the output document.
  3. 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.

  1. Open the input PDF and create an output Document.
  2. Create an array containing page numbers 2 and 3.
  3. Call CopyPage() for each selected page in the supplied order.
  4. 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);
}