Search code examples
xsl-fo

How to display page number if total page greater than 1?


I'm using XSL-FO to generate PDF documents with one or more pages. However, if my document has one page I want to hide the {pageNumber}/{totalPage} (the red rectangle area in the below picture), and only display when {totalPage} > 1. How do I do that?

enter image description here


Solution

    • Use a different fo:simple-page-master for the single page that uses a different region-name for its fo:region-after.
    • Add an fo:page-sequence-master that selects the new fo:simple-page-master for the single page using page-position="only". (See XSL-FO Different header/footer depending on page-position, https://stackoverflow.com/a/58343013/4092205, and https://www.w3.org/TR/xsl11/#page-position.)
    • When you generate the fo:static-content that you direct to the fo:region-after for regular pages, generate another fo:static-content without the fo:page-number and direct that to the fo:region-after for the fo:simple-page-master for the single page.
    • When the formatter works out that the document fits on a single page, it will use the fo:simple-page-master for the single page, and that page will use the fo:static-content that has a flow-name that matches the region-name of its fo:region-after.

    So that's a few steps, but that's because there's some flexibility in what you can do with fo:page-sequence-master selecting different page masters.