C++: Add Headers to Existing PDF Documents

Including a header to a PDF document brings numerous benefits and serves as an effective way to enhance the presentation and organization of important information. In this feature, a section of text or graphics is placed at the top of each page, allowing for the inclusion of essential details such as the document title, authorship, and page numbers. By customizing the header according to specific needs, individuals can create professional-looking reports, contracts, or other documents that maintain a consistent format throughout. This article demonstrates how to add a header to an existing PDF document in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.PDF for C++ in a C++ Application

Background Knowledge

When an existing PDF document is manipulated by Spire.PDF for C++, the origin of the coordinate system is located at the top left corner of the page, with the x-axis extending to the right and the y-axis extending downward. Adding a header to a page means adding content, such as text, images, automatic fields and shapes, to a specified location in the upper blank area of the page.

C++: Add Headers to Existing PDF Documents

If the blank area is not large enough to accommodate the content you want to add, you can consider increasing the PDF page margins.

Add Headers to an Existing PDF Documents in C++

Spire.PDF for C++ offers the PdfCanvas->DrawString() method, PdfCanvas->DrawImage() method, PdfCanvas->DrawLine() method and its similar methods, allowing users to draw text, images and shapes on a PDF page at the specified location. To add dynamic data to the footer, such as page numbers, sections, dates, you need to use the automatic fields. Spire.PDF for C++ provides the PdfPageNumberField class, PdfPageCountField calss, PdfSectionNumberField class etc. to achieve the addition of dynamic information.

The following are the steps to add a header consisting of text, an image, a date, and a line to a PDF document using Spire.PDF for C++.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument->LoadFromFile() method.
  • Create font, pen and brush objects that will be used to draw text or shapes.
  • Draw text on the top blank area of a page using PdfPageBase->GetCanvas()->DrawString() method.
  • Draw a line on the top blank area of a page using PdfPageBase->GetCanvas()->DrawLine() method.
  • Load an image using PdfImage::FromFile() method.
  • Draw the image on the top blank area of a page using PdfPageBase->GetCanvas()->DrawImage() method.
  • Create a PdfCreationDateField object that reflects the creation time of the document.
  • Draw the creation time on the top blank area of a page using PdfCreationDateField->Draw() method.
  • Save the document to another PDF file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;

int main()
{
    //Create a PdfDocument object
    intrusive_ptr<PdfDocument> doc = new PdfDocument();

    //Load a PDF file
    doc->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\TargetMarket.pdf");

    //Load an image for the header
    intrusive_ptr<PdfImage> headerImage = PdfImage::FromFile(L"C:\\Users\\Administrator\\Desktop\\logo.png");

    //Get image width in pixel
    float width = headerImage->GetWidth();

    //Convert pixel to point
    intrusive_ptr<PdfUnitConvertor> unitCvtr = new PdfUnitConvertor();
    float pointWidth = unitCvtr->ConvertUnits(width, PdfGraphicsUnit::Pixel, PdfGraphicsUnit::Point);

    //Specify text for the header
    wstring headerText = L"E-iceblue Technology\nwww.e-iceblue.com";

    //Create a true type font
    intrusive_ptr<PdfTrueTypeFont> font = new PdfTrueTypeFont(L"Times New Roman", 12.f, PdfFontStyle::Bold, true);

    //Create a brush
    intrusive_ptr<PdfBrush> brush = PdfBrushes::GetDarkBlue();

    //Create a pen
    intrusive_ptr<PdfPen> pen = new PdfPen(brush, 1.0f);

    //Create a creation date field
    intrusive_ptr<PdfCreationDateField> creationDateField = new PdfCreationDateField(font, brush);
    creationDateField->SetDateFormatString(L"yyyy-MM-dd");

    vector< intrusive_ptr<PdfAutomaticField>> list;
    list.push_back(creationDateField);

    //Create a composite field to combine string and date field
    intrusive_ptr<PdfCompositeField> compositeField = new PdfCompositeField(font, brush, L"creation time: {0}", list);
    compositeField->SetLocation(new PointF(55, 48));

    //Loop through the pages in the document
    for (int i = 0; i < doc->GetPages()->GetCount(); i++)
    {
        //Get specific page
        intrusive_ptr<PdfPageBase> page = doc->GetPages()->GetItem(i);

        //Draw the image on the top blank area
        page->GetCanvas()->DrawImage(headerImage, page->GetActualSize()->GetWidth() - pointWidth - 55, 20);

        //Draw text on the top blank area
        page->GetCanvas()->DrawString(headerText.c_str(), font, brush, 55, 20);

        //Draw a line on the top blank area
        page->GetCanvas()->DrawLine(pen, new PointF(55, 70), new PointF((int)page->GetActualSize()->GetWidth() - 55, 70));

        //Draw the composite field on the top blank area
        compositeField->Draw(page->GetCanvas(), 0, 0);
    }

    //Save to file
    doc->SaveToFile(L"AddHeader.pdf");
    doc->Dispose();
}

C++: Add Headers to Existing PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.