Add Bates numbering to PDF in C++

Bates numbering assigns sequential identifiers to pages. Both C++ addition examples append two blank pages to the input document before numbering the entire page collection. This behavior differs from the Python examples, which number the existing pages.

The shared helper sets six digits starting at 000001, no prefix or suffix, and bottom-right placement. Save it as bates-artifact.h beside either addition example. Configure the SDK and choose a writable output path.

Configure the shared Bates artifact

Save the following helper as bates-artifact.h beside the addition examples. It configures six digits starting at 1, all pages through the end of the document, no prefix or suffix, bottom-right alignment, and explicit margins.

  1. Create a BatesNArtifact.
  2. Set its page range and subset using PaginationArtifact properties.
  3. Set the number of digits, start number, prefix, and suffix on the BatesNArtifact.
  4. Configure alignment and margins through Artifact properties.
#pragma once
#include <Aspose.PDF.Cpp/Artifacts/BatesNArtifact.h>
#include <Aspose.PDF.Cpp/Artifacts/Pagination/Subset.h>
#include <Aspose.PDF.Cpp/HorizontalAlignment.h>
#include <Aspose.PDF.Cpp/VerticalAlignment.h>

inline System::SharedPtr<Aspose::Pdf::BatesNArtifact> CreateBatesArtifact()
{
    using namespace Aspose::Pdf;
    auto artifact = System::MakeObject<BatesNArtifact>();
    artifact->set_StartPage(1);
    artifact->set_EndPage(0);
    artifact->set_Subset(Subset::All);
    artifact->set_NumberOfDigits(6);
    artifact->set_StartNumber(1);
    artifact->set_Prefix(u"");
    artifact->set_Suffix(u"");
    artifact->set_ArtifactVerticalAlignment(VerticalAlignment::Bottom);
    artifact->set_ArtifactHorizontalAlignment(HorizontalAlignment::Right);
    artifact->set_RightMargin(72);
    artifact->set_LeftMargin(72);
    artifact->set_TopMargin(36);
    artifact->set_BottomMargin(36);
    return artifact;
}

Add Bates numbering directly

Append two blank pages and apply the shared Bates artifact to the document pages.

  1. Open the input PDF with Document.
  2. Append two pages with PageCollection::Add().
  3. Call CreateBatesArtifact() to obtain the configured BatesNArtifact.
  4. Apply it with PageCollectionExtensions::AddBatesNumbering().
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/Artifact.h>
#include <Aspose.PDF.Cpp/Artifacts/ArtifactCollection.h>

using namespace System;
using namespace Aspose::Pdf;

#include "bates-artifact.h"
#include <Aspose.PDF.Cpp/Artifacts/Pagination/PageCollectionExtensions.h>

void AddBatesNArtifact(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (int i = 0; i < 2; ++i) {
        document->get_Pages()->Add();
    }
    auto artifact = CreateBatesArtifact();
    PageCollectionExtensions::AddBatesNumbering(document->get_Pages(), artifact);
    document->Save(outputFile);
}

Add Bates numbering through pagination

Use the same helper, but pass the Bates artifact in a collection of PaginationArtifact objects to AddPagination().

  1. Open the input PDF with Document.
  2. Append two pages with PageCollection::Add().
  3. Call CreateBatesArtifact() and add the result to a list of PaginationArtifact objects.
  4. Call PageCollectionExtensions::AddPagination() for the document page collection.
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Artifacts/Artifact.h>
#include <Aspose.PDF.Cpp/Artifacts/ArtifactCollection.h>

using namespace System;
using namespace Aspose::Pdf;

#include "bates-artifact.h"
#include <Aspose.PDF.Cpp/Artifacts/Pagination/PageCollectionExtensions.h>
#include <Aspose.PDF.Cpp/Artifacts/PaginationArtifact.h>
#include <system/collections/list.h>

void AddBatesNArtifactPagination(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (int i = 0; i < 2; ++i) {
        document->get_Pages()->Add();
    }
    auto artifact = CreateBatesArtifact();
    auto artifacts = MakeObject<System::Collections::Generic::List<SharedPtr<PaginationArtifact>>>();
    artifacts->Add(artifact);
    PageCollectionExtensions::AddPagination(document->get_Pages(), artifacts);
    document->Save(outputFile);
}

Moved inspection and removal guides

See Remove Bates Numbering for this operation.