Delete PDF pages in C++

Remove one page or a selected set of pages from a PDF document using Aspose.PDF for C++.

Delete a single page

The input PDF must contain page 2.

  1. Open the input PDF as a Document.
  2. Call Delete() on PageCollection for page 2.
  3. 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 DeletePagesExamples.deletePage.
void DeletePage(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);

    document->get_Pages()->Delete(2);
    document->Save(outputFile);
}

Delete multiple pages

The input PDF must contain at least four pages.

  1. Open the input PDF as a Document.
  2. Create an array containing page numbers 2, 3, and 4.
  3. Pass the array to Delete() on PageCollection.
  4. Save the result 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 DeletePagesExamples.deleteBunchPages.
void DeleteBunchPages(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);

    document->get_Pages()->Delete(MakeArray<int32_t>({2, 3, 4}));
    document->Save(outputFile);
}