Henrik Holle wrote:
can you please explain me how to count the page in a fo with an xslt?

You have to render the XML with a placeholder, count pages, and render again with the actual number of pages. FOP provides a method to access the total number of pages rendered, this allows automation (note: code can be optimized):

  public static void main(String args[]) {
    try {
      Driver driver =new Driver();
      driver.setOutputStream(new FileOutputStream(args[2]));
      driver.setRenderer(Driver.RENDER_PDF);
      Transformer transformer=TransformerFactory.newInstance()
        .newTransformer(new StreamSource(new File(args[1])));
      transformer.setParameter("page-count","#");
      transformer.transform(new StreamSource(new File(args[0])),
        new SAXResult(driver.getContentHandler()));
      String pageCount=Integer.toString(driver.getResults().getPageCount());
      driver =new Driver();
      driver.setOutputStream(new FileOutputStream(args[2]));
      driver.setRenderer(Driver.RENDER_PDF);
      transformer=TransformerFactory.newInstance().
         newTransformer(new StreamSource(new File(args[1])));
      transformer.setParameter("page-count",pageCount);
      transformer.transform(new StreamSource(new File(args[0])),
         new SAXResult(driver.getContentHandler()));
    }
    catch( Exception e) {
      e.printStackTrace();
    }
  }

A sample XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:fo="http://www.w3.org/1999/XSL/Format";>
  <xsl:strip-space elements="*"/>
  <xsl:param name="page-count" select="'#'"/>
  <xsl:template match="doc">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format";>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="content"
          page-width="210mm" page-height="297mm"
          margin-left="25mm" margin-right="25mm"
          margin-top="25mm" margin-bottom="15mm">
          <fo:region-body margin-bottom="10mm"/>
          <fo:region-after extent="10mm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="content">
        <fo:static-content flow-name="xsl-region-after">
          <fo:block text-align="end">
            <fo:page-number/>
            <xsl:text> of </xsl:text>
            <xsl:value-of select="$page-count"/>
          </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="sect1">
    <fo:block space-before="12pt">
      <xsl:apply-templates select="title"/>
    </fo:block>
    <fo:block space-before="3pt">
      <xsl:apply-templates select="title/following-sibling::*"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="p">
    <fo:block space-before="3pt">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

</xsl:stylesheet>

Note the parameter page-count.
A sample XML, just for completeness:<?xml version="1.0"?>

<doc>
  <sect1>
    <title>Password</title>
    <p>The users password, as an MD5 hash. The hash is calculated
      as follows: First the password String is converted into an
      UTF8 encoded array of bytes: String.getBytes("UTF8"). The
      MD5 digest is calculated over the given array of bytes and
      stored as a hex string.</p>
  </sect1>
</doc>

HTH
J.Pietschmann



Reply via email to