Add Header to PDF in C++

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

Add header with margins

Add a blue 12-point Helvetica header using margin values of 20.

  1. Create the text content and formatting with FormattedText.
  2. Create PdfFileStamp.
  3. Bind the PDF input with Facade::BindPdf().
  4. Add the header with PdfFileStamp::AddHeader().
  5. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Facades/EncodingType.h>
#include <Aspose.PDF.Cpp/Facades/FontStyle.h>
#include <Aspose.PDF.Cpp/Facades/FormattedText.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <drawing/color.h>
#include <system/string.h>

namespace Facades::Examples::PDF_FileStamp
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
using DrawingColor = System::Drawing::Color;

void AddHeaderWithMargins(String inputFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    auto text = MakeObject<FormattedText>(u"Sample Header", DrawingColor::get_Blue(), FontStyle::Helvetica,
                                          EncodingType::Winansi, true, 12.0f);
    pdfStamper->AddHeader(text, 20, 20, 20);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp

Add image header

Add an image header from a file stream with a margin value of 20.

  1. Create PdfFileStamp.
  2. Bind the PDF input with Facade::BindPdf().
  3. Add the header with PdfFileStamp::AddHeader().
  4. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/PdfFileStamp.h>
#include <system/io/file.h>
#include <system/io/file_stream.h>
#include <system/string.h>

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

void AddImageHeader(String inputFile, String imageFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    auto imageStream = System::IO::File::OpenRead(imageFile);

    pdfStamper->BindPdf(inputFile);
    pdfStamper->AddHeader(imageStream, 20);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp

Add text header

Add Sample Header as formatted text with a margin value of 20.

  1. Create the text content and formatting with FormattedText.
  2. Create PdfFileStamp.
  3. Bind the PDF input with Facade::BindPdf().
  4. Add the header with PdfFileStamp::AddHeader().
  5. Save the resulting PDF with PdfFileStamp::Save().
#include <Aspose.PDF.Cpp/Facades/FormattedText.h>
#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 AddTextHeader(String inputFile, String outputFile)
{
    auto pdfStamper = MakeObject<PdfFileStamp>();
    pdfStamper->BindPdf(inputFile);
    auto text = MakeObject<FormattedText>(u"Sample Header");
    pdfStamper->AddHeader(text, 20);
    pdfStamper->Save(outputFile);
}
} // namespace Facades::Examples::PDF_FileStamp