Search code examples
xmlpdfxsltxsl-foapache-fop

How to vertically center a letter using XSLT


I'm using XSLT to tranform XML to PDF using FOP. What I want is just center a letter horizontally and vertically, i.e. it should be in the center of the page. What I got so far is this.

enter image description here

You can see that it's already horizontally center, but I don't know to center it vertically, too. I have tried vertical-align, display-align, but to no avail. Is there any way to achieve it?

This is my code so far.

XML

<?xml version="1.0" encoding="UTF-8"?>
<letter>A</letter>

XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4"
                               page-height="210mm" page-width="297mm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="A4">
        <fo:flow flow-name="xsl-region-body">
          <fo:block
              text-align="center" font-size="100mm">
            <xsl:value-of select="letter"/>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

Solution

  • In this case @display-align can be used in:

    • fo:region-body
    • fo:block-container that encloses fo:block

    Here are two examples.

    [1st method]

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/">
            <fo:root>
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="A4"
                        page-height="210mm" page-width="297mm">
                        <fo:region-body display-align="center"/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="A4">
                    <fo:flow flow-name="xsl-region-body">
                        <fo:block
                            text-align="center" font-size="100mm">
                            <xsl:value-of select="letter"/>
                        </fo:block>
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
    </xsl:stylesheet>
    

    [2nd method]

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/">
            <fo:root>
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="A4"
                        page-height="210mm" page-width="297mm">
                        <fo:region-body/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="A4">
                    <fo:flow flow-name="xsl-region-body">
                        <fo:block-container height="100%" display-align="center">
                            <fo:block
                                text-align="center" font-size="100mm">
                                <xsl:value-of select="letter"/>
                            </fo:block>
                        </fo:block-container>
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
    </xsl:stylesheet>
    

    [Result PDF view]

    enter image description here