Add PDF pages in C++
Contents
[
Hide
]
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
- Open the input PDF as a Document.
- Call Add() on the document PageCollection.
- 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.
- Open the input PDF as a Document.
- Call Insert() on PageCollection at position 2.
- 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.
- Create an output Document and open the input PDF.
- Add a first page with a TextFragment.
- Call CopyPage() to append page 1 from the input PDF.
- 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);
}