Templates

Templates automatically add page elements to all pages in a document or section, making templates useful for headers and footers. There are two types of templates, document-wide templates and section wide templates. 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. For information on how to break a document into sections and apply a section template, see the Document Sectioning topic.

As of v11, 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 primarily useful when working with other size pages and customizing alignment.

DynamicPDF Core Suite for .NET includes three template classes.

Template Classes Description
Template The Template class adds Page Element 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 Header Footer Template on a page.

Document Templates

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.

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   

Even / Odd Templates

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

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

Stamp Templates

All page elements within this Template will appear in the foreground of the other contents of the PDF.

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 

Header Footer Template

Document 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 header or footer text using center alignment. For left and right alignments, HeaderFooterText class can be used. The following example illustrates using the HeaderFooterTemplate

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

PageNumberingLabel

The following example illustrates creating left-aligned PageNumberingLabel using header footer template for the document.

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

HeaderFooter Template in HTML Area

The following example illustrates using a Header footer template in HTML area.

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