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.

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.

A footer is text or other content located at the bottom of a page, usually including page numbers, dates, authors, and other information. Footers contribute to the overall professional appearance of the document by maintaining a consistent layout across all pages. By incorporating footers, PDF documents become more professional, user-friendly, and compliant with legal requirements. This article demonstrates how to add a footer to an existing PDF document in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.4.4</version>
    </dependency>
</dependencies>

Background Knowledge

When using Spire.PDF for Java 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, automatic fields and shapes, to a specified location in the bottom blank area of the page.

Java: Add a Footer to an Existing PDF Document

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 a Footer to an Existing PDF Document in Java

Spire.PDF for Java 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 Java 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 Java.

  • 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.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.PdfBrush;
import com.spire.pdf.graphics.PdfBrushes;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTrueTypeFont;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class AddFooterToPdf {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Load an image
        PdfImage footerImage = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\bg.jpg");

        //Create a true type font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", Font.BOLD, 12),true);

        //Create a brush
        PdfBrush brush = PdfBrushes.getWhite();

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

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

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

        //Get the text size
        Dimension2D fontSize = font.measureString(compositeField.getText());

        //Get the page size
        Dimension2D pageSize = doc.getPages().get(0).getSize();

        //Set the position of the composite field
        compositeField.setLocation(new Point2D.Double((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
            PdfPageBase page = doc.getPages().get(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());
        }

        //Save to file
        doc.saveToFile("output/AddFooter.pdf");
        doc.dispose();
    }
}

Java: Add a Footer to an Existing PDF Document

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.

page 82