Add PDF pages in C++

Add blank pages, insert a page at a specific position, or copy a page from another PDF using Aspose.PDF for C++.

Add an empty page at the end

  1. Open the input PDF as a Document.
  2. Call Add() on the document PageCollection.
  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 AddPagesExamples.addEmptyPageToEnd.
void AddEmptyPageToEnd(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);

    document->get_Pages()->Add();
    document->Save(outputFile);
}

Insert an empty page

Use an input PDF with at least one page.

  1. Open the input PDF as a Document.
  2. Call Insert() on PageCollection at position 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 AddPagesExamples.insertEmptyPage.
void InsertEmptyPage(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);

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

Copy a page from another document

The output contains a new text page followed by the copied page.

  1. Create an output Document and open the input PDF.
  2. Add a first page with a TextFragment.
  3. Call CopyPage() to append page 1 from the input PDF.
  4. Save the result with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Text/Font.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Generator/Paragraphs.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

// Port of AddPagesExamples.addPageFromAnotherDocument.
void AddPageFromAnotherDocument(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>();
    auto anotherDocument = MakeObject<Document>(inputFile);

    document->get_Pages()->Add()->get_Paragraphs()->Add(MakeObject<TextFragment>(u"This is first page!"));
    document->get_Pages()->CopyPage(anotherDocument->get_Pages()->idx_get(1));
    document->Save(outputFile);
}