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 footerPageNumberFont) { string thisPageURL = HttpContext.Current.Request.Url.AbsoluteUri; string headerAndFooterHtmlUrl = thisPageURL.Substring(0, thisPageURL.LastIndexOf('/')) + "/HeaderAndFooterHtml.htm"; //create a template to be added in the header and footer document.FooterTemplate = document.AddTemplate(document.Pages[0].ClientRectangle.Width, 60); // create a HTML to PDF converter element to be added to the header template HtmlToPdfElement footerHtmlToPdf = new HtmlToPdfElement(headerAndFooterHtmlUrl); document.FooterTemplate.AddElement(footerHtmlToPdf); // add page number to the footer TextElement pageNumberText = new TextElement(document.FooterTemplate.ClientRectangle.Width - 100, 30, "This is page &p; of &P; pages", footerPageNumberFont); document.FooterTemplate.AddElement(pageNumberText); }
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:
protected void btnCreatePDF_Click(object sender, EventArgs e) { string pdfToModify = textBoxPdfFilePath.Text.Trim(); // create a PDF document Document document = new Document(pdfToModify); // get the first page the PDF document PdfPage firstPage = document.Pages[0]; string logoImagePath = System.IO.Path.Combine(Server.MapPath("~"), @"img\logo.jpg"); // display image in the available space in page and with a auto determined height to keep the aspect ratio ImageElement imageElement1 = new ImageElement(0, 0, logoImagePath); AddElementResult addResult = firstPage.AddElement(imageElement1); // add image border // add a border to watermark RectangleElement imageBorderRectangleElement = new RectangleElement(1, 1, addResult.EndPageBounds.Width, addResult.EndPageBounds.Height); firstPage.AddElement(imageBorderRectangleElement); System.Drawing.Image logoImg = System.Drawing.Image.FromFile(logoImagePath); // calculate the watermark location System.Drawing.SizeF imageSizePx = logoImg.PhysicalDimension; // transform from pixels to points float imageWidthPoints = UnitsConverter.PixelsToPoints(imageSizePx.Width); float imageHeightPoints = UnitsConverter.PixelsToPoints(imageSizePx.Height); float watermarkXLocation = (firstPage.ClientRectangle.Width - imageWidthPoints)/2; float watermarkYLocation = firstPage.ClientRectangle.Height / 4; // add a template watermark to the document repeated on each document page // the watermark size is equal to image size in points Template watermarkTemplate = document.AddTemplate(new System.Drawing.RectangleF(watermarkXLocation, watermarkYLocation, imageWidthPoints, imageHeightPoints + 20)); // add a standard font to the document PdfFont watermarkTextFont = document.AddFont(StdFontBaseFamily.HelveticaBold); watermarkTextFont.Size = 10; // Add a text element to the watermark. You can add any other graphic element to a template TextElement watermarkTextElement = new TextElement(3, 0, "This is Watermark Text", watermarkTextFont); watermarkTextElement.ForeColor = System.Drawing.Color.Red; watermarkTextElement.Transparency = 100; watermarkTemplate.AddElement(watermarkTextElement); // add an image to the watermak ImageElement watermarkImageElement = new ImageElement(0, 20, logoImg); watermarkImageElement.Transparency = 100; watermarkTemplate.AddElement(watermarkImageElement); // add a border to watermark RectangleElement watermarkRectangleElement = new RectangleElement(0, 0, watermarkTemplate.ClientRectangle.Width, watermarkTemplate.ClientRectangle.Height); watermarkTemplate.AddElement(watermarkRectangleElement); // dispose the image logoImg.Dispose(); // save the document on http response stream document.Save(Response, false, "Demo.pdf"); }