Friday, 24 June 2011 05:51

Add PDF Footer in C#, VB.NET

A PDF header or footer presents consistent information (For example: a date, page numbering, the title of the overall document, or author’s name) in the page margins throughout a PDF. In this article, you will learn to add text and automatic page numbering to footer space when creating a PDF document from scratch.

Spire.PDF has a class named PdfPageTemplateElement, which represents a page template element that can be used as header, footer, watermark or stamp. The template can contain text, image as well as dynamic fields like PdfPageCountField, PdfPageNumberField, etc.

Step 1: Define a custom function CreateFooterTemplate() to create a page template element that servers as footer, and return a PdfPageDocumentElement object.

static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
{
    //get page size
    SizeF pageSize = doc.PageSettings.Size;

    //create a PdfPageTemplateElement object which works as footer space
    PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
    footerSpace.Foreground = false;

    //declare two float variables
    float x = margins.Left;
    float y = 0;

    //draw line in footer space
    PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
    footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);

    //draw text in footer space
    y = y + 5;
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 10f), true);
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
    String footerText = "E-iceblue Technology Co., Ltd.\nTel:028-81705109\nWebsite:http://www.e-iceblue.com";
    footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Gray, x, y, format);

    //draw dynamic field in footer space
    PdfPageNumberField number = new PdfPageNumberField();
    PdfPageCountField count = new PdfPageCountField();
    PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Gray, "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, size.Width, size.Height);
    compositeField.Draw(footerSpace.Graphics);

    //return footerSpace
    return footerSpace;
}

Step 2: Create a PDF document, call the method CreateFooterTemplate() to create a footer template and apply it to the document.

static void Main(string[] args)
{
    //create a PDF document
    PdfDocument doc = new PdfDocument();
    doc.PageSettings.Size = PdfPageSize.A4;

    //reset the default margins to 0
    doc.PageSettings.Margins = new PdfMargins(0);

    //create a PdfMargins object, the parameters indicate the page margins you want to set
    PdfMargins margins = new PdfMargins(60, 60, 60, 60);

    //create a footer template with content and apply it to bottom page template
    doc.Template.Bottom = CreateFooterTemplate(doc, margins);

    //apply blank templates to other parts of page template
    doc.Template.Top = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Top);
    doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
    doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);

    //add two pages in the document
    doc.Pages.Add();
    doc.Pages.Add();

    //save the file
    doc.SaveToFile("PdfFooter.pdf");
}

Output:

Add PDF Footer in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;


namespace AddPDFFooter
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a PDF document
            PdfDocument doc = new PdfDocument();
            doc.PageSettings.Size = PdfPageSize.A4;

            //reset the default margins to 0
            doc.PageSettings.Margins = new PdfMargins(0);

            //create a PdfMargins object, the parameters indicate the page margins you want to set
            PdfMargins margins = new PdfMargins(60, 60, 60, 60);

            //create a footer template with content and apply it to page template
            doc.Template.Bottom = CreateFooterTemplate(doc, margins);

            //apply blank templates to other parts of page template
            doc.Template.Top = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Top);
            doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);

            //add two pages in the document
            doc.Pages.Add();
            doc.Pages.Add();

            //save the file
            doc.SaveToFile("PdfFooter.pdf");
        }
        static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
        {
            //get page size
            SizeF pageSize = doc.PageSettings.Size;

            //create a PdfPageTemplateElement object which works as footer space
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
            footerSpace.Foreground = false;

            //declare two float variables
            float x = margins.Left;
            float y = 0;

            //draw line in footer space
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
            footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);

            //draw text in footer space
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 10f), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String footerText = "E-iceblue Technology Co., Ltd.\nTel:028-81705109\nWebsite:http://www.e-iceblue.com";
            footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Gray, x, y, format);

            //draw dynamic field in footer space
            PdfPageNumberField number = new PdfPageNumberField();
            PdfPageCountField count = new PdfPageCountField();
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Gray, "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, size.Width, size.Height);
            compositeField.Draw(footerSpace.Graphics);

            //return footerSpace
            return footerSpace;
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.AutomaticFields
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace AddPDFFooter
	Class Program
		Private Shared Sub Main(args As String())
			'create a PDF document
			Dim doc As New PdfDocument()
			doc.PageSettings.Size = PdfPageSize.A4

			'reset the default margins to 0
			doc.PageSettings.Margins = New PdfMargins(0)

			'create a PdfMargins object, the parameters indicate the page margins you want to set
			Dim margins As New PdfMargins(60, 60, 60, 60)

			'create a footer template with content and apply it to page template
			doc.Template.Bottom = CreateFooterTemplate(doc, margins)

			'apply blank templates to other parts of page template
			doc.Template.Top = New PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Top)
			doc.Template.Left = New PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height)
			doc.Template.Right = New PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height)

			'add two pages in the document
			doc.Pages.Add()
			doc.Pages.Add()

			'save the file
			doc.SaveToFile("PdfFooter.pdf")
		End Sub
		Private Shared Function CreateFooterTemplate(doc As PdfDocument, margins As PdfMargins) As PdfPageTemplateElement
			'get page size
			Dim pageSize As SizeF = doc.PageSettings.Size

			'create a PdfPageTemplateElement object which works as footer space
			Dim footerSpace As New PdfPageTemplateElement(pageSize.Width, margins.Bottom)
			footerSpace.Foreground = False

			'declare two float variables
			Dim x As Single = margins.Left
			Dim y As Single = 0

			'draw line in footer space
			Dim pen As New PdfPen(PdfBrushes.Gray, 1)
			footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y)

			'draw text in footer space
			y = y + 5
			Dim font As New PdfTrueTypeFont(New Font("Impact", 10F), True)
			Dim format As New PdfStringFormat(PdfTextAlignment.Left)
			Dim footerText As [String] = "E-iceblue Technology Co., Ltd." & vbLf & "Tel:028-81705109" & vbLf & "Website:http://www.e-iceblue.com"
			footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Gray, x, y, format)

			'draw dynamic field in footer space
			Dim number As New PdfPageNumberField()
			Dim count As New PdfPageCountField()
			Dim compositeField As New PdfCompositeField(font, PdfBrushes.Gray, "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, size.Width, size.Height)
			compositeField.Draw(footerSpace.Graphics)

			'return footerSpace
			Return footerSpace
		End Function
	End Class
End Namespace
Published in Header and Footer