Add Page Number to PDF in C++

Use PdfFileStamp for this task. The examples below retain the original C++ code and API Reference steps.

Add page numbers at coordinates

Add Page # numbering at (300, 20).

  1. Create PdfFileStamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Add page numbering with PdfFileStamp::AddPageNumber().
  4. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileStamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void AddPageNumbersAtCoordinates(String inputFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    pdfStamper->AddPageNumber(u"Page #", 300, 20);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp

Add page numbers default

Add Page # numbering using default positioning.

  1. Create PdfFileStamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Add page numbering with PdfFileStamp::AddPageNumber().
  4. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileStamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void AddPageNumbersDefault(String inputFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    pdfStamper->AddPageNumber(u"Page #");
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp

Add page numbers with position and margins

Add Page # numbering at PosBottomRight with all four margins set to 10.

  1. Create PdfFileStamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Add page numbering with PdfFileStamp::AddPageNumber().
  4. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileStamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void AddPageNumbersWithPositionAndMargins(String inputFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    pdfStamper->AddPageNumber(u"Page #", PdfFileStamp::PosBottomRight, 10, 10, 10, 10);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp

Add page numbers with roman style

Set uppercase Roman numbering, start at 42, and add the page number at PosUpperRight.

  1. Create PdfFileStamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Set numbering style through PdfFileStamp using the example values.
  4. Set starting number through PdfFileStamp using the example values.
  5. Add page numbering with PdfFileStamp::AddPageNumber().
  6. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <Aspose.PDF.Cpp/NumberingStyle.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileStamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void AddPageNumbersWithRomanStyle(String inputFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    pdfStamper->set_NumberingStyle(NumberingStyle::NumeralsRomanUppercase);
    pdfStamper->set_StartingNumber(42);
    pdfStamper->AddPageNumber(u"Page #", PdfFileStamp::PosUpperRight);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp