ceTe Software Help Library for Java August - 2020
DynamicPDF Generator for Java / Programming with Generator for Java / Templates
In This Topic
    Templates
    In This Topic

    Templates are used to automatically add page elements to all pages in a document or section. This is useful for things like headers and footers. There are two types of templates that can be applied. These are document wide templates and section wide templates. By default, a page will use both the document and section template if they are set. If you want to disable either template for an individual page, this can be done with the page's ApplyDocumentTemplate or ApplySectionTemplate property. For information on how to break a document into sections and apply a section template, see the Document Sectioning topic.

    DynamicPDF Generator for Java includes two template classes:

    Template
    The Template class is used to add Page Element's to all pages in the section or document.
    Even / Odd Template
    The EvenOddTemplate class is used to add different Page Elements to even or odd pages in the section or document.

    Document Templates

    Document templates are applied by creating a Template class, adding Page Elements to it, then setting it to the Document's Template property. All page elements within this template will appear in the background of the other contents of the PDF.

    [Java]
        Template template = new Template();
        template.getElements().add(new Label("Header", 0, 0, 200, 12));
        document.setTemplate(template);
    

    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.  

    [Java]
        EvenOddTemplate template = new EvenOddTemplate();
        template.getEvenElements().add(new Label("Even Header", 0, 0, 200, 12));
        template.getOddElements().add(new Label("Odd Header", 0, 0, 200, 12));
        template.getElements().add(new Label("Footer", 0, 680, 200, 12));
        document.setTemplate(template);
    

    Stamp Templates

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

    [Java]
        Template template = new Template();
        template.getElements().add( new Label( "Header", 0, 0, 200, 12 ) );
        document.setStampTemplate(template);
        
    

    Header Footer Template

    Document HeaderFooterTemplate is used to place header and footer for all the pages in the document with different alignments( left, right and center ). This header and footer text will be placed in the margin area of the page. Page elements also can be added to this template. By default header footer template class place the header text or footer text with center alignment. For left and right alignments, HeaderFooterText class can be used.

    [Java]
        HeaderFooterTemplate header = new HeaderFooterTemplate("HeaderText", "FooterText");
        document.setTemplate(header);  
    

    The following example shows how to create left aligned PageNumberingLabel using header footer template for the document:

    [Java]
        HeaderFooterTemplate header = new HeaderFooterTemplate();
        HeaderFooterText leftText = new HeaderFooterText(" of ");
        header.setHeaderLeft(leftText);
        document.setTemplate(header);
        
    

    The following example shows usage of Header footer template in HTML area:

    [Java]
        Document document = new Document();
        HeaderFooterTemplate header = new HeaderFooterTemplate("HeaderText", "FooterText");
        URI filePath = null;
        try {
            filePath = new URI("C:\\Temp\\TestPage.html");
             } catch (URISyntaxException ex) {
             System.out.println("A URISyntaxException was caught :" + ex.getMessage());
             }
        HtmlArea htmlArea = new HtmlArea(filePath, 0, 0, 500, 600);
        Page page = new Page();
        page.getElements().add(htmlArea);
        document.setTemplate(header);
        document.draw(pdfFilePath);
        
    
    See Also