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#]
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]
Private Shared Sub Main(ByVal args As String())
    Dim doc As PdfDocument = New PdfDocument()
    doc.PageSettings.Size = PdfPageSize.A4
    doc.PageSettings.Margins = New PdfMargins(0)
    Dim margins As PdfMargins = New PdfMargins(60, 60, 60, 60)
    doc.Template.Bottom = CreateFooterTemplate(doc, margins)
    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)
    doc.Pages.Add()
    doc.Pages.Add()
    doc.SaveToFile("PdfFooter.pdf")
End Sub

Private Shared Function CreateFooterTemplate(ByVal doc As PdfDocument, ByVal margins As PdfMargins) As PdfPageTemplateElement
    Dim pageSize As SizeF = doc.PageSettings.Size
    Dim footerSpace As PdfPageTemplateElement = New PdfPageTemplateElement(pageSize.Width, margins.Bottom)
    footerSpace.Foreground = False
    Dim x As Single = margins.Left
    Dim y As Single = 0
    Dim pen As PdfPen = New PdfPen(PdfBrushes.Gray, 1)
    footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y)
    y = y + 5
    Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Impact", 10F), True)
    Dim format As PdfStringFormat = 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)
    Dim number As PdfPageNumberField = New PdfPageNumberField()
    Dim count As PdfPageCountField = New PdfPageCountField()
    Dim compositeField As PdfCompositeField = 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
End Function
Published in Header and Footer