Templates

Templates automatically add page elements to all pages in a document or section, including headers and footers. There are two types of templates, document-wide templates and section-wide templates.

DynamicPDF Core Suite for .NET includes three template classes. The Template class is parent to the EvenOddTemplate and HeaderFooterTemplate classes.

Template Classes Description
Template The Template class adds page elements to all pages in a section or document.
EvenOddTemplate The EvenOddTemplate class adds different page elements to even or odd pages to a section or document.
HeaderFooterTemplate The HeaderFooterTemplate class places a template on a page to the header and/or footer.

By default, a page sets both the document and section template when set. A page's ApplyDocumentTemplate or ApplySectionTemplate property disables either template for an individual page.

See the Document Sectioning topic for information on how to break a document into sections and apply a section template.

As of v12, all page elements now have a RelativeTo and IgnoreMargins property. By default, RelativeTo is TopLeft, while IgnoreMargins is False (the default behavior in previous versions). However, using the RelativeTo and IgnoreMargins properties, the default behavior can be changed to meet different needs. This flexibility is useful when working with other size pages and customizing alignment.

Template

Create a Template class by adding page elements and then setting the template to the Document's Template property. All page elements within this template will then appear in the background of the other PDF content.

Properties

Property Description
Elements Gets the page elements that will be applied by the template to all pages.

Example

Template template = new Template();
template.Elements.Add( new Label( "Header", 0, 0, 200, 12 ) );
document.Template = template;        
Dim MyTemplate As Template = New Template()
MyTemplate.Elements.Add( New Label( "Header", 0, 0, 200, 12 ) )
MyDocument.Template = MyTemplate   

Refer to the Template API documentation for a complete example.

EvenOddTemplate

Even/Odd templates (EvenOddTemplate) are applied by adding separate elements to both Odd and Even pages. All page elements within the Odd and Even templates will appear in the background on Odd and Even pages, respectively.

Properties

Property Description
EvenElements Gets the page elements that will be applied by the template to even pages.
OddElements Gets the page elements that will be applied by the template to odd pages.

Example

EvenOddTemplate template = new EvenOddTemplate();
template.EvenElements.Add( new Label( "Even Header", 0, 0, 200, 12 ) );
template.OddElements.Add( new Label( "Odd Header", 0, 0, 200, 12 ) );
template.Elements.Add( new Label( "Footer", 0, 680, 200, 12 ) );
document.Template = template;        
Dim MyTemplate As EvenOddTemplate = New EvenOddTemplate()
MyTemplate.EvenElements.Add( New Label( "Even Header", 0, 0, 200, 12 ) )
MyTemplate.OddElements.Add( New Label( "Odd Header", 0, 0, 200, 12 ) )
MyTemplate.Elements.Add( New Label( "Footer", 0, 680, 200, 12 ) )
MyDocument.Template = MyTemplate

Refer to the EvenOddTemplate API documentation for a complete example.

StampTemplate

This template gets or sets a Template object for a document. It has no properties other than those inherited from the Template class. All page elements within this template appear in the foreground of the other contents of the PDF.

The following example illustrates creating a StampTemplate.

Template template = new Template();
template.Elements.Add( new Label( "Header", 0, 0, 200, 12 ) );
document.StampTemplate = template;       
Dim MyTemplate As Template = New Template()
MyTemplate.Elements.Add( New Label( "Header", 0, 0, 200, 12 ) )
MyDocument.StampTemplate = MyTemplate 

HeaderFooterTemplate

A HeaderFooterTemplate places headers and footers for all pages in a document with different alignments (left, right, and center). It then places the header and footer text in the margin area of the page. Page elements can also be added to this template. By default, the HeaderFooter template class aligns the header or footer text using center alignment. For left and right alignments, use the HeaderFooterText class.

Properties

Property Description
FooterCenter Gets or sets the center footer text for the page.
FooterLeft Gets or sets the left footer text for the page.
FooterRight Gets or sets the right footer text for the page.
HeaderCenter Gets or sets the center header footer text for the page.
HeaderLeft Gets or sets the left header text for the page.
HeaderRight Gets or sets the right header text for the page.

Examples

The following three examples illustrate creating a HeaderFooterTemplate, creating a left-aligned PageNumberingLabel, and adding a HeaderFooterTemplate within an HTMLArea.

HeaderFooterTemplate Example

The following example illustrates using a HeaderFooterTemplate.

HeaderFooterTemplate header = new HeaderFooterTemplate("HeaderText", "FooterText");
document.Template = header;        
Dim MyHeader As HeaderFooterTemplate = New HeaderFooterTemplate("HeaderText", "FooterText")
MyDocument.Template = MyHeader

Refer to the HeaderFooterTemplate API documentation for a complete example.

PageNumberingLabel Example

The following example illustrates creating a left-aligned PageNumberingLabel using a HeaderFooterTemplate.

HeaderFooterTemplate header = new HeaderFooterTemplate();
HeaderFooterText leftText = new HeaderFooterText(" of ");
header.HeaderLeft = leftText;
document.Template = header;       
Dim MyHeader As HeaderFooterTemplate = New HeaderFooterTemplate()
Dim MyLeftText As HeaderFooterText = New HeaderFooterText(" of ")
MyHeader.HeaderLeft = MyLeftText
MyDocument.Template = MyHeader

HeaderFooterTemplate in HtmlArea Example

The following example illustrates using a HeaderFooterTemplate in an HtmlArea.

Document document = new Document();
HeaderFooterTemplate header = new HeaderFooterTemplate("HeaderText", "FooterText");
Uri filePath = new Uri(@"C:\\Temp\\TestPage");
HtmlArea htmlArea = new HtmlArea(filePath, 0, 0, 500, 600);
Page page = new Page();
page.Elements.Add(htmlArea);
document.Template = header;
documen.Draw(pdfFilePath);       
Dim MyDocument As Document = New Document()
Dim MyHeader As HeaderFooterTemplate = New HeaderFooterTemplate("HeaderText", "FooterText")
Dim MyFilePath As Uri = New Uri("C:\\Temp\\TestPage")
Dim MyHtmlArea As HtmlArea = New HtmlArea(MyFilePath, 0, 0, 500, 600)
Dim MyPage As Page = New Page()
MyPage.Elements.Add(MyHtmlArea)
MyDocument.Template = MyHeader
MyDocumen.Draw(pdfFilePath)

In this topic