Add PDF header and footer artifacts in C++

Use HeaderArtifact and FooterArtifact to add running text as page artifacts. Each example modifies only the first page and requires Arial to be available.

The input PDF must contain at least one page. Configure the Aspose.PDF for C++ SDK and use a writable output path for examples that save a PDF.

Add a header artifact

Add the centered text Sample Header to the first page.

  1. Open the input PDF with Document.
  2. Create a HeaderArtifact and set its text.
  3. Configure its TextState with Arial, size 14, and navy text.
  4. Set the Artifact horizontal alignment to Center.
  5. Add the artifact to the first page with ArtifactCollection::Add().
  6. 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 <Aspose.PDF.Cpp/Artifacts/HeaderArtifact.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/HorizontalAlignment.h>
#include <Aspose.PDF.Cpp/Text/FontRepository.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>

void AddHeaderArtifact(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto artifact = MakeObject<HeaderArtifact>();
    artifact->set_Text(u"Sample Header");
    artifact->get_TextState()->set_FontSize(14);
    artifact->get_TextState()->set_Font(Aspose::Pdf::Text::FontRepository::FindFont(u"Arial"));
    artifact->get_TextState()->set_ForegroundColor(Color::get_Navy());
    artifact->set_ArtifactHorizontalAlignment(HorizontalAlignment::Center);
    document->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);
    document->Save(outputFile);
}

Add the centered text Sample Footer to the first page.

  1. Open the input PDF with Document.
  2. Create a FooterArtifact and set its text.
  3. Configure its TextState with Arial, size 14, and navy text.
  4. Set the Artifact horizontal alignment to Center.
  5. Add the artifact to the first page with ArtifactCollection::Add().
  6. 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 <Aspose.PDF.Cpp/Artifacts/FooterArtifact.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/HorizontalAlignment.h>
#include <Aspose.PDF.Cpp/Text/FontRepository.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>

void AddFooterArtifact(System::String inputFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto artifact = MakeObject<FooterArtifact>();
    artifact->set_Text(u"Sample Footer");
    artifact->get_TextState()->set_FontSize(14);
    artifact->get_TextState()->set_Font(Aspose::Pdf::Text::FontRepository::FindFont(u"Arial"));
    artifact->get_TextState()->set_ForegroundColor(Color::get_Navy());
    artifact->set_ArtifactHorizontalAlignment(HorizontalAlignment::Center);
    document->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);
    document->Save(outputFile);
}

Moved inspection and removal guides

See Remove Headers and Footers for this operation.