Click or drag to resize

Template Class

The Template class represents content repeated on the pages of the PDF document. Templates are used for headers and footers, watermarks, page numbers or any other content that has to be repeated on the pages of the document.

A Template renders the same page elements as a PdfPage: HTML to PDF elements, texts, images and shapes, and the interactive elements, which are created on each page the template is drawn on (see Overview). A Template object is created with the AddTemplate methods of the Document class.

There are two kinds of templates: predefined templates like the header and the footer, and custom templates. Both are instances of the Template class, but the space of the predefined templates is taken from the drawable area of the PDF pages.

DrawOnFirstPage, DrawOnOddPages and DrawOnEvenPages select the pages a template is drawn on; a predefined template keeps its space on the pages it is not drawn on. BringToFront (the default) draws the template over the page content and SendToBackground draws it behind. On a single page, CustomHeaderTemplate and CustomFooterTemplate replace the document header and footer.

Predefined Templates

The predefined templates are the document header, footer, left bar template and right bar template (HeaderTemplate, FooterTemplate, LeftTemplate and RightTemplate). The predefined templates are automatically docked to the corresponding side of the PDF page.

For example, the predefined header is automatically docked to the top of the PDF page, which means the header location is the top left corner and the width of the header template is the width of the page. Also if a predefined header was set, the available client area in PDF page will start right under the header. Similarly, if a predefined footer was set, it will be docked to the bottom of page and the available client area in PDF will end right above the footer template. It is recommended to set the predefined templates before starting to add elements to the document.

Below is a sample code to add a footer to the PDF document. Another special element in the sample code below is the text element used to add page numbering. In a text element added to a template, the &p; token is replaced with the current page number and the &P; token with the total number of pages of the PDF document. PageNumberingStartIndex and PageNumberingPageCountIncrement offset the two numbers.

private void AddHtmlFooter(Document document, PdfFont pageNumberFont,
    string footerHtmlUrl)
{
    // a template 60 points high, repeated at the bottom of every page
    document.FooterTemplate = document.AddTemplate(
        document.Pages[0].ClientRectangle.Width, 60);

    // HTML content in the footer
    HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlUrl);
    document.FooterTemplate.AddElement(footerHtml);

    // the page number: &p; is the current page, &P; the number of pages
    TextElement pageNumber = new TextElement(
        document.FooterTemplate.ClientRectangle.Width - 120, 30,
        "Page &p; of &P;", pageNumberFont);
    document.FooterTemplate.AddElement(pageNumber);
}
Custom Templates

The custom templates can be used to add watermarks or any other content that must be repeated in the same position on each page. In the code sample below, a watermark containing a text and an image is added to an existing document:

// open an existing document
using (Document document = new Document("contract.pdf"))
{
    PdfPage firstPage = document.Pages[0];

    // a watermark repeated on every page: a template in the middle of the page
    // with a text and an image
    float width = 220, height = 80;
    float x = (firstPage.ClientRectangle.Width - width) / 2;
    float y = (firstPage.ClientRectangle.Height - height) / 2;
    Template watermark = document.AddTemplate(new RectangleF(x, y, width, height));

    PdfFont font = document.AddFont(StdFontBaseFamily.HelveticaBold);
    font.Size = 24;
    TextElement text = new TextElement(0, 0, "CONFIDENTIAL", font);
    text.ForeColor = Color.Red;
    // mostly transparent: the page content stays readable
    text.Transparency = 30;
    watermark.AddElement(text);

    ImageElement logo = new ImageElement(0, 40, 60, 30, "logo.png");
    logo.Transparency = 30;
    watermark.AddElement(logo);

    document.Save("contract-watermarked.pdf");
}
See Also