Template Class |
The Template class represents content which is repeated on each page of the PDF document. The templates can be used to create headers and footers, watermarks or any other content that needs to be repeated on each page of the PDF document.
A Template class can render all the graphic elements (e.g HTML to PDF converter, texts, images, shapes) a PdfPage can render, but it cannot render interactive elements like PDF links, attachments, text notes. A Template class object can be instantiated using the AddTemplate interface of the Document class.
There two logical types of templates in PDF Creator: predefined templates like header and footer and custom templates. Both predefined and custom templates are instances of the Template class, but the predefined templates dimensions are considered when calculating the available client area in a PDF page.
The predefined templates are the document header, footer, left bar template and right bar template. 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 to 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. When adding a text element to a template, the &p; token will be replaced with the current page number and the &P; token will be replaced with the total number of pages of the PDF document. This is possible because the templates are rendered after the PDF document main layer was rendered.
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); }
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, taken from the ModifyExistingPdf sample, a watermark containing a text and an image is added to an existing document:
// open an existing document 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; text.Transparency = 30; // mostly transparent: the page content stays readable watermark.AddElement(text); ImageElement logo = new ImageElement(0, 40, 60, 30, "logo.png"); logo.Transparency = 30; watermark.AddElement(logo); document.Save("contract-watermarked.pdf"); document.Close();