Search code examples
xmlpdfxsltxsl-fo

How to align text with the bottom of an XSL-FO block?


I am writing an XSLT script, that transforms an XML input document to an XSL-FO intermediate document, which get transformed into a PDF output document.

My fo:simple-page-master exists out of three parts, a page header, body, and footer.

<fo:layout-master-set>
    <fo:simple-page-master master-name="a4-portrait"
        page-width="210mm" page-height="297mm"
        margin-top="0mm" margin-right="12mm" margin-bottom="0mm" margin-left="12mm">
        <fo:region-body region-name="page-body" margin-top="20mm" margin-bottom="20mm"/>
        <fo:region-before region-name="page-header" extent="20mm"/>
        <fo:region-after region-name="page-footer" extent="20mm"/>
    </fo:simple-page-master>
    <fo:simple-page-master master-name="a4-landscape"
        page-width="297mm" page-height="210mm"
        margin-top="0mm" margin-right="12mm" margin-bottom="0mm" margin-left="12mm">
        <fo:region-body region-name="page-body" margin-top="20mm" margin-bottom="20mm"/>
        <fo:region-before region-name="page-header" extent="20mm"/>
        <fo:region-after region-name="page-footer" extent="20mm"/>
    </fo:simple-page-master>
</fo:layout-master-set>

Here is a simple hello world page:

<fo:page-sequence master-reference="a4-portrait">
    <!-- Page header -->
    <fo:static-content flow-name="page-header">
        <fo:block vertical-align="text-bottom">Some headline in the page header</fo:block>
        <fo:block vertical-align="text-bottom" border-bottom="0.25pt solid black"></fo:block>
    </fo:static-content>

    <!-- Page footer -->
    <fo:static-content flow-name="page-footer">
        <fo:block border-top="0.25pt solid black"></fo:block>
        <fo:block>Page <fo:page-number/></fo:block>
    </fo:static-content>

    <!-- Page body -->
    <fo:flow flow-name="page-body">
        <fo:block>Hello World!</fo:block>
    </fo:flow>
</fo:page-sequence>

As you can see, I already tried the vertical-align="text-bottom" approach. Unfortunately, the fo:block-elements did not align to the bottom of the superordinate element.

How to write the XSLT in order to have the page-header writes the text from bottom to top, instead of top-to-bottom?

XSLT 2.0, XPath 2.0, Apache FOP, Saxon-HE 9.8


Solution

  • Use display-align="after" (see https://www.w3.org/TR/xsl11/#display-align).

    display-align applies to fo:region-before (see https://www.w3.org/TR/xsl11/#fo_region-before) as well as to the other regions, fo:block-container, fo:external-graphic, fo:instream-foreign-object, fo:inline-container, and fo:table-cell.

    <fo:layout-master-set>
        <fo:simple-page-master master-name="a4-portrait"
            page-width="210mm" page-height="297mm"
            margin-top="0mm" margin-right="12mm" margin-bottom="0mm" margin-left="12mm">
            <fo:region-body region-name="page-body" margin-top="20mm" margin-bottom="20mm"/>
            <fo:region-before region-name="page-header" extent="20mm" display-align="after"/>
            <fo:region-after region-name="page-footer" extent="20mm"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="a4-landscape"
            page-width="297mm" page-height="210mm"
            margin-top="0mm" margin-right="12mm" margin-bottom="0mm" margin-left="12mm">
            <fo:region-body region-name="page-body" margin-top="20mm" margin-bottom="20mm"/>
            <fo:region-before region-name="page-header" extent="20mm" display-align="after"/>
            <fo:region-after region-name="page-footer" extent="20mm"/>
        </fo:simple-page-master>
    </fo:layout-master-set>