Table of Contents
More Resources
Total Page Number
Total Page Number
Calculating the Total Number of Pages
Edit the templates in custom-xsl.xsl to calculate the total page number.
Updating the ‘Custom Page Number’ Variable
Update the custom-xsl.xsl and en.xml to include the total number of pages in your page number variable.
Total Page Number

In your header or footer, you can display a total page number with the page number. For example, instead of just displaying “Page x”, your PDF output displays “Page x of y”.
First, you must already have a custom variable element for the page number in Editing a Header Template or Editing a Footer Template.
Adding a total page number to your footer or header requires:
Calculating the Total Number of Pages

Edit the templates in custom-xsl.xsl to calculate the total page number.
To calculate the total number of pages, we need to place a block with an Id on the last page of the document. We can then reference the page number of this block to get the total number of pages. This involves building the whole < fo:root> element inside a variable and then passing it through a postprocessing template that sticks the Id’d block at the end.

You need to edit the custom-xsl.xsl file (located in cfg/fo/xsl/).
  1. First, you need to build the < fo:root> inside a variable instead of straight into the output. You’ll copy the default root template and overwrite it. This is what the default from pdf2 looks like:

    Step Information
    < xsl:template match="/" name="rootTemplate"> 
      < xsl:call-template name="validateTopicRefs"/> 
      < fo:root xsl:use-attribute-sets="__fo__root"> 
        < xsl:call-template name="createLayoutMasters"/> 
        < xsl:call-template name="createMetadata"/> 
        < xsl:call-template name="createBookmarks"/> 
        < xsl:call-template name="createFrontMatter"/> 
        < xsl:if test="not($retain-bookmap-order)"> 
          < xsl:call-template name="createToc"/> 
        < /xsl:if> 
        < xsl:apply-templates/>       
        < xsl:if test="not($retain-bookmap-order)"> 
          < xsl:call-template name="createIndex"/> 
        < /xsl:if> 
      < /fo:root> 
    < /xsl:template> 
  2. Copy the default template to your custom-xsl.xsl file, wrap the < fo:root> in a variable, and then run each element through a postprocess template.

    Step Information
    < xsl:template match="/" name="rootTemplate"> 
      < xsl:call-template name="validateTopicRefs"/> 
      < xsl:variable name="fo"> 
        < fo:root xsl:use-attribute-sets="__fo__root"> 
          < xsl:call-template name="createLayoutMasters"/> 
          < xsl:call-template name="createMetadata"/> 
          < xsl:call-template name="createBookmarks"/> 
          < xsl:call-template name="createFrontMatter"/> 
          < xsl:if test="not($retain-bookmap-order)"> 
            < xsl:call-template name="createToc"/> 
          < /xsl:if> 
          < xsl:apply-templates/>       
          < xsl:if test="not($retain-bookmap-order)"> 
            < xsl:call-template name="createIndex"/> 
          < /xsl:if> 
        < /fo:root> 
      < /xsl:variable> 
      < xsl:apply-templates select="$fo/*" mode="postprocess"/> 
    < /xsl:template> 
  3. Next, you’ll add two required postprocess templates to the custom-xsl.xsl file. The first template matches the last < fo:flow> element and appends a block element to it:

    Step Information
    < xsl:template match="fo:flow[not(following::fo:flow)]" mode="postprocess"> 
      < xsl:copy> 
        < xsl:copy-of select="@*"/> 
        < xsl:apply-templates mode="postprocess"/> 
        < fo:block id="last-page-id" keep-with-previous.within-page="always"/> 
      < /xsl:copy> 
    < /xsl:template> 
  4. The second template matches everything else and passes the data through:

    Step Information
    < xsl:template match="*" mode="postprocess"> 
      < xsl:copy> 
        < xsl:copy-of select="@*"/> 
        < xsl:apply-templates mode="postprocess"/> 
      < /xsl:copy> 
    < /xsl:template> 
You added templates to calculate the total page number using the last page of the document. Now, see Updating the ‘Custom Page Number’ Variable to display the total number of pages.
Updating the ‘Custom Page Number’ Variable

Update the custom-xsl.xsl and en.xml to include the total number of pages in your page number variable.
You already calculated the total number of pages using the procedure in Calculating the Total Number of Pages.
You want to display the total number of pages in your PDF output.
  1. Now that you have a value for the total page number, you need to edit your call to the insertVariable template (located in cfg/fo/xsl/custom-xsl.xsl) to pass it in. The following example modifies the footer block from Editing a Footer Template. If you want to add this to the header, you can change the call to call-template in your header block.

    Step Information

    Add the following reference into the < totalpages> element within the parameters that are passed into the insertVariable call-template: 

    < fo:page-number-citation-last ref-id="last-page-id"/> 

    Expected Result
    < fo:block xsl:use-attribute-sets="__body__odd__footer"> 
      < xsl:call-template name="insertVariable"> 
        < xsl:with-param name="theVariableID" select="'Custom Footer Text'"/> 
      < /xsl:call-template> 
      < 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> 
          < totalpages> < fo:page-number-citation-last ref-id="last-page-id"/> < /totalpages> 
        < /xsl:with-param> 
      < /xsl:call-template> 
    < /fo:block> 

  2. Now, you need to update your variable definition in cfg/common/vars/en.xml to:

    Step Information
    < variable id="Custom Page Number"> 
      Page < param ref-name="pagenum"/>  of < param ref-name="totalpages"/> 
    < /variable> 

Your page number should now appear as “Page x of y:”. Here’s an example of the output: