C#/VB.NET: Add a Footer to an Existing PDF Document

Adding footers to a PDF document is a useful way to provide additional information and context to the content within the document. Footers typically appear at the bottom of each page and can include elements such as page numbers, dates, copyright information, or any other relevant details. By incorporating footers, you can enhance the professionalism and organization of your PDF files, making them more informative and easier to navigate for readers. In this article, you will learn how to add a footer to an existing PDF document in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLLs included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Background Knowledge

When an existing PDF document is manipulated by Spire.PDF for .NET, 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.

C#/VB.NET: 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 C#, VB.NET

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

  • 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.Canvas.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#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddHeaderToExistingPdf
{
    class Program
    {
        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", 12f, FontStyle.Bold), true);

            //Create a brush
            PdfBrush brush = PdfBrushes.White;

            //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
            SizeF fontSize = font.MeasureString(compositeField.Text);

            //Get the page size
            SizeF pageSize = doc.Pages[0].Size;

            //Set the position of the composite field
            compositeField.Location = new Point((int)(pageSize.Width - fontSize.Width) / 2, (int)pageSize.Height - 45);

            //Loop through the pages in the document
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                //Get a specific page
                PdfPageBase page = doc.Pages[i];

                //Draw the image on the bottom blank area
                page.Canvas.DrawImage(footerImage, 55, pageSize.Height - 65, pageSize.Width - 110, 50);

                //Draw the composite field on the bottom blank area
                compositeField.Draw(page.Canvas);
            }

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

C#/VB.NET: 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.