Add Footer to Existing PDF in C#, VB.NET

In this article, you will learn how to add footer to an existing PDF document using Spire.PDF. For a page in a ready-made PDF document, the coordinate system follows the following rules.

  • The origin of the PDF coordinate system (0, 0) represents the top-left corner of the PDF page.
  • The x-axis extends to the right and the y-axis extends downward.

Before you draw text or page number at the specified coordinates in the empty space, you must measure the margins of the page yourself to prevent the footer from overlapping the body.

Step 1: Load a sample PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("SalesReport.pdf");

Step 2: Get the coordinates that you want to start adding footer.

SizeF pageSize = doc.Pages[0].Size;
float x = 90;
float y = pageSize.Height - 72;

Step 3: Add a line, some text, page number and page count to the footer space.

for (int i = 0; i < doc.Pages.Count; i++)
{
    //draw line at bottom
    PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
    doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y);

    //draw text at bottom
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri", 14f));
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
    String footerText = "E-iceblue Technology Co., Ltd.\nTel:86-028-81705109\nWebsite:http://www.e-iceblue.com";
    doc.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.RoyalBlue, x, y + 2, format);

    //draw page number and page count at bottom
    PdfPageNumberField number = new PdfPageNumberField();
    PdfPageCountField count = new PdfPageCountField();
    PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.RoyalBlue, "Page {0} of {1}", number, count);
    compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
    SizeF size = font.MeasureString(compositeField.Text);
    compositeField.Bounds = new RectangleF(pageSize.Width - x, y + 2, size.Width, size.Height);
    compositeField.Draw(doc.Pages[i].Canvas);
}

Step 4: Save to file.

doc.SaveToFile("output.pdf");

Output:

Add Footer to Existing PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("SalesReport.pdf");

SizeF pageSize = doc.Pages[0].Size;
float x = 90;
float y = pageSize.Height - 72;

for (int i = 0; i < doc.Pages.Count; i++)
{
    //draw line at bottom
    PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
    doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y);

    //draw text at bottom
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri", 14f));
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
    String footerText = "E-iceblue Technology Co., Ltd.\nTel:86-028-81705109\nWebsite:http://www.e-iceblue.com";
    doc.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.RoyalBlue, x, y + 2, format);

    //draw page number and page count at bottom
    PdfPageNumberField number = new PdfPageNumberField();
    PdfPageCountField count = new PdfPageCountField();
    PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.RoyalBlue, "Page {0} of {1}", number, count);
    compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
    SizeF size = font.MeasureString(compositeField.Text);
    compositeField.Bounds = new RectangleF(pageSize.Width - x, y + 2, size.Width, size.Height);
    compositeField.Draw(doc.Pages[i].Canvas);
}

doc.SaveToFile("output.pdf");
[VB.NET]
Dim doc As PdfDocument = New PdfDocument()
doc.LoadFromFile("SalesReport.pdf")
Dim pageSize As SizeF = doc.Pages(0).Size
Dim x As Single = 90
Dim y As Single = pageSize.Height - 72

For i As Integer = 0 To doc.Pages.Count - 1
    Dim pen As PdfPen = New PdfPen(PdfBrushes.Gray, 0.5F)
    doc.Pages(i).Canvas.DrawLine(pen, x, y, pageSize.Width - x, y)
    Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Calibri", 14F))
    Dim format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Left)
    Dim footerText As String = "E-iceblue Technology Co., Ltd." & vbLf & "Tel:86-028-81705109" & vbLf & "Website:http://www.e-iceblue.com"
    doc.Pages(i).Canvas.DrawString(footerText, font, PdfBrushes.RoyalBlue, x, y + 2, format)
    Dim number As PdfPageNumberField = New PdfPageNumberField()
    Dim count As PdfPageCountField = New PdfPageCountField()
    Dim compositeField As PdfCompositeField = New PdfCompositeField(font, PdfBrushes.RoyalBlue, "Page {0} of {1}", number, count)
    compositeField.StringFormat = New PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top)
    Dim size As SizeF = font.MeasureString(compositeField.Text)
    compositeField.Bounds = New RectangleF(pageSize.Width - x, y + 2, size.Width, size.Height)
    compositeField.Draw(doc.Pages(i).Canvas)
Next

doc.SaveToFile("output.pdf")