Table of Contents
More Resources
Editing a Header Template

Edit the custom-xsl.xsl file to add a header to your main content pages.
context

You want to add a header template to the custom-xsl.xsl file (located in cfg/fo/xsl/) to add a header with an image, the document title, or multiple lines of text.

  1. To add a custom header, you need to modify the insertBodyOddHeader template. The default header in org.dita.pdf2 uses the Body odd header variable, which displays the product name, header content, and page number:

    Step Information
    < xsl:template name="insertBodyOddHeader"> 
      < fo:static-content flow-name="odd-body-header"> 
        < fo:block xsl:use-attribute-sets="__body__odd__header"> 
          < xsl:call-template name="insertVariable"> 
            < xsl:with-param name="theVariableID" select="'Body odd header'"/> 
            < xsl:with-param name="theParameters"> 
              < prodname> 
                < xsl:value-of select="$productName"/> 
              < /prodname> 
              < heading> 
                < fo:inline xsl:use-attribute-sets="__body__odd__header__heading"> 
                  < fo:retrieve-marker retrieve-class-name="current-header"/> 
                < /fo:inline> 
              < /heading> 
              < pagenum> 
                < fo:inline xsl:use-attribute-sets="__body__odd__header__pagenum"> 
                  < fo:page-number/> 
                < /fo:inline> 
              < /pagenum> 
            < /xsl:with-param> 
          < /xsl:call-template> 
        < /fo:block> 
      < /fo:static-content> 
    < /xsl:template> 

  2. Anything you want to put in the header must be contained within the fo:static-content tag and the fo:block:

    Step Information
    < xsl:template name="insertBodyOddHeader"> 
      < fo:static-content flow-name="odd-body-header"> 
        < fo:block xsl:use-attribute-sets="__body__odd__header"> 
          
        < /fo:block> 
      < /fo:static-content> 
    < /xsl:template> 

  3. To add a header image, use the fo:block-container and fo:block element to add and absolutely position the image. Use the code below:

    Step Information
    < fo:block-container xsl:use-attribute-sets="__header__image"> 
      < fo:block> 
        < fo:external-graphic src="url(Customization/OpenTopic/common/artwork/[image.name])"/> 
      < /fo:block> 
    < /fo:block-container> 
    [image.name] is the name of the logo file (located in cfg/common/artwork)
    Tip:If you want to set the size of the image, you can use the content-width attribute on fo:external-graphic.

  4. To add the document title to the header, use the fo:block element.

    1. Use the following code to insert the title text into the header:

      Step Information
      < fo:block xsl:use-attribute-sets="__headerTitleText"> 
        < xsl:value-of select="$title"/> 
      < /fo:block> 

    2. The contents of the block are defined by the variable $title. The title value is somewhat complicated to calculate for bookmaps, so calculating it once and setting it to a variable is most effective. You’ll need the following code at the top of your custom-xsl.xsl file (just below the < xsl:stylesheet> element):

      Step Information
      < xsl:variable name="title"> 
        < xsl:choose> 
          < xsl:when test="$map//*[contains(@class,' bookmap/mainbooktitle ')][1]"> 
            < xsl:apply-templates select="$map//*[contains(@class,' bookmap/mainbooktitle ')][1]"/> 
          < /xsl:when> 
          < xsl:when test="$map/*[contains(@class,' topic/title ')][1]"> 
            < xsl:apply-templates select="$map/*[contains(@class,' topic/title ')][1]"/> 
          < /xsl:when> 
          < xsl:when test="//*[contains(@class, ' map/map ')]/@title"> 
            < xsl:value-of select="//*[contains(@class, ' map/map ')]/@title"/> 
          < /xsl:when> 
          < xsl:otherwise> 
            < xsl:value-of select="/descendant::*[contains(@class, ' topic/topic ')][1]/*[contains(@class, ' topic/title ')]"/> 
          < /xsl:otherwise> 
        < /xsl:choose> 
      < /xsl:variable> 
      This variable definition uses the $map variable, and because this is defined in the default org.dita.pdf2 stylesheets, we don’t need to worry about it.

  5. To add multiple lines of text in your header, you can use multiple fo:block elements. This examples inserts two lines: one for the Company Name, and one for the Document Title and Page Number:

    Step Information
    < fo:block xsl:use-attribute-sets="__header_line_one">    < xsl:call-template name="insertVariable">      < xsl:with-param name="theVariableID" select="'Company Name'"/>    < /xsl:call-template>  < /fo:block>  < fo:block xsl:use-attribute-sets="__header_line_two">    < xsl:value-of select="$title"/>    < fo:leader leader-pattern="space"/>    < xsl:call-template name="insertVariable">      < xsl:with-param name="theVariableID" select="'Custom Page Number'"/>      < xsl:with-param name="theParameters">        < pagenum>          < fo:page-number/>        < /pagenum>      < /xsl:with-param>    < /xsl:call-template>  < /fo:block>  

    Tip:You can use variables (by calling the “insertVariable” template as above) to specify string values that will automatically be translated with the rest of your content. To add a variable value, follow the steps outlined in Setting Global Variables.
    Note:If you want to display the total page count (for example, “Page x of y”), you’ll need to use the Total Page Number guide.
    Note:If you do add several lines of text into the header, use the Adjusting the Header Size guide to adjust the header spacing so that the text won’t overlap.

  6. Combine all the code together into the header template and add it to the custom-xsl.xsl file located at cfg/fo/xsl/.

    Example
    The example below uses an image and a document title:
    < xsl:template name="insertBodyOddHeader"> 
      < fo:static-content flow-name="odd-body-header"> 
        < fo:block xsl:use-attribute-sets="__body__odd__header"> 
    
          < fo:block-container xsl:use-attribute-sets="__header__image"> 
            < fo:block> 
              < fo:external-graphic src="url(Customization/OpenTopic/common/artwork/[image.name])"/> 
            < /fo:block> 
          < /fo:block-container> 
    
          < fo:block xsl:use-attribute-sets="__headerTitleText"> 
            < xsl:value-of select="$title"/> 
          < /fo:block> 
    
        < /fo:block> 
      < /fo:static-content> 
    < /xsl:template> 
result
You added your custom header template to the custom-xsl.xsl file located in cfg/fo/xsl/.
Tip:If you want the same custom header to appear on your Table of Contents page, you need to add another template. You can just copy your custom header template and change the template name to “insertTocOddHeader” and the static-content flow-name to “odd-toc-header”.

postreq
Your custom header template contains new attribute-sets. See Adding Header Attributes to define these new attribute-sets and ensure your header displays correctly.
In step 5, if you did call the insertVariable template with a custom variable Id, you’ll need to set it up using the Setting Global Variables guide.