Click or drag to resize
Pdf Library for .NET

Pdf Templates

As seen in the previous sections, pdf elements need to be added to a specific page of the pdf document. But, there are cases when there is the need to display certain pdf elements in all pages of a pdf document. Select.Pdf has suppport for this, without the need to add those pdf elements in each individual page. This is done using pdf templates.

A pdf template belongs to a pdf document and contains elements that will automatically repeat on each page of the pdf document. The main object that handles the pdf template is PdfTemplate.

Pdf templates can be added to a pdf document using AddTemplate(Single, Single) and AddTemplate(RectangleF) methods of the PdfDocument class. Using these methods, the location of the template in the pdf pages is specified.

PdfTemplate class has several properties that can be used to customize the pdf template:

  • Docking - Gets or sets the template docking settings inside the pdf page.

  • Anchoring - Gets or sets the template anchoring settings inside the pdf page.

  • DisplayOnOddPages - Controls the visibility of the template on the odd numbered pages of the pdf document.

  • DisplayOnEvenPages - Controls the visibility of the template on the even numbered pages of the pdf document.

  • DisplayOnFirstPage - Controls the visibility of the template on the first page of the generated pdf document.

  • FirstPageNumber - Controls the page number for the first page being rendered. The page number is displayed setting a {page_number} placeholder in a Text property of a PdfTextElement object that can be added to the template. By default the page numbers start with 1. This can be changed using the FirstPageNumber property.

  • TotalPagesOffset - Controls the total number of pages offset in the pdf document. The total number of pages is displayed setting a {total_pages} placeholder in the Text property of a PdfTextElement object that can be added to the template. The total number of pages can be incremented with a value specified by the TotalPagesOffset property. This could be useful when the generated pdf will be merged with other documents.

  • Foreground - Controls whether the template is rendered in front of the page main content or not.

  • Background - Controls whether the template is rendered behind the page main content or not.

PdfDocument has also 2 special templates: Header and Footer to manage custom headers and footers that would appear in all pages of the pdf document. More about this in the next section, that describes pdf document headers and footers.

Sample Code

This sample code shows how to create a new PDF document using Select.Pdf, how to add several pages and pdf templates to the document.

// create a new pdf document
PdfDocument doc = new PdfDocument();

// create a new pdf font
PdfFont font = doc.AddFont(PdfStandardFont.Helvetica);
font.Size = 24;

// add a new page to the document
PdfPage page = doc.AddPage();

// create a new text element and add it to the page
PdfTextElement text = new PdfTextElement(0, 0, Helper.SomeLongText(), font);
page.Add(text);

// get image path
string imgFile = Server.MapPath("~/files/logo.png");

// add a template containing an image
// the image should repeat on all pdf pages automatically
PdfTemplate template1 = doc.AddTemplate(new RectangleF(100, 0, 400, 150));
PdfImageElement img1 = new PdfImageElement(0, 0, imgFile);
template1.Add(img1);

// add another template containing an image behind the existing page elements
// (under the text) the image should repeat on all pdf pages automatically
PdfTemplate template2 = doc.AddTemplate(new RectangleF(100, 200, 400, 150));
template2.Background = true;
PdfImageElement img2 = new PdfImageElement(0, 0, imgFile);
template2.Add(img2);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also