C++: Add Footers to Existing PDF Documents

Adding footers to a PDF document is a common practice that enhances the professionalism and readability of the file. Footers provide valuable information such as page numbers, dates, document titles, or copyright notices at the bottom of each page. By including footers, users can easily navigate through lengthy documents, identify specific sections, and maintain consistent branding. In this article, you will learn how to add footers 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 using Spire.PDF for C++ to process an existing PDF document, 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 footer to a page means adding content, such as text, images, shapes and automatic fields, to a specified location in the bottom blank area of the page.

C++: Add Footers 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 Footers to an Existing PDF Document 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 footer consisting of an image and page number to a PDF document using Spire.PDF for C++.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument->LoadFromFile() method.
  • Load an image using PdfImage::FromFile() method.
  • Draw the image on the bottom blank area of a page using PdfPageBase->GetCanvas()->DrawImage() method.
  • Create a PdfPageNumberField object, a PdfPageCountField object, and combine them in a PdfCompositeField object to return the string "Page X of Y".
  • Draw page number on the bottom blank area of a page using PdfCompositeField->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
    intrusive_ptr<PdfImage> footerImage = PdfImage::FromFile(L"C:\\Users\\Administrator\\Desktop\\bg.jpg");

    //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::GetWhite();

    //Create a page number field
    intrusive_ptr<PdfPageNumberField> pageNumberField = new PdfPageNumberField();

    //Create a page count field
    intrusive_ptr<PdfPageCountField> pageCountField = new PdfPageCountField();


    vector< intrusive_ptr<PdfAutomaticField>> list;
    list.push_back(pageNumberField);
    list.push_back(pageCountField);

    //Create a composite field to combine page count field and page number field in a single string
    intrusive_ptr<PdfCompositeField> compositeField = new PdfCompositeField(font, brush, L"Page {0} of {1}", list);

    //Get the text size
    intrusive_ptr<SizeF> fontSize = font->MeasureString(compositeField->GetText());

    //Get the page size
    intrusive_ptr<SizeF> pageSize = doc->GetPages()->GetItem(0)->GetSize();

    //Set the position of the composite field
    compositeField->SetLocation(new PointF((pageSize->GetWidth() - fontSize->GetWidth()) / 2, pageSize->GetHeight() - 45));

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

        //Draw the image on the bottom blank area
        page->GetCanvas()->DrawImage(footerImage, 55, pageSize->GetHeight() - 65, pageSize->GetWidth() - 110, 50);

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

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

C++: Add Footers 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.