How to display one or the other information depending on the page number in XSL-FO?

◇◆丶佛笑我妖孽 提交于 2019-12-08 10:52:11

问题


I'm using XSL-FO to create PDF's. Now I have a label that I want to display only on the first page, and another label that I want to display on all other pages, e.g.

Page 1

Last month's balance

Page 2 - n

Previous page's balance

The whole PDF is actually just a simple table, so this isn't about a cover page that stands outside of the data. How can I do it?

See also this related question: How can I sum up some values per page in a table in XSL-FO?


回答1:


This one can either be achieved using fo:marker and fo:retrieve-marker (from fo:static-content), or with the help of fo:page-sequence-master, that provides different fo:simple-page-master entries for the first page (@page-position="first") and other pages (="rest").

In the latter case you would need to name static regions on different simple-page-masters differently, and provide an fo:static-content in the fo:page-sequence for each page master.




回答2:


I haven't tried Michael Sulyaev's suggestion about page-sequence-master on this problem. However, using <fo:marker/> and <fo:retrieve-marker/> seemed to work. I'm not sure if this is a valid XSL-FO solution, but it works with Apache fop.

It seems to be important that the <fo:retrieve-marker/> is a <fo:static-content/> descendant, according to the w3.org specification. It cannot be part of the table itself.

<fo:static-content flow-name="xsl-region-before">
  <fo:table>
    <!-- Render <fo:table-column/> elements -->
    <xsl:call-template name="TableColumns"/>

    <!-- fo:table-body, fo:table-row, fo:table-cell, fo:block 
         omitted for the example -->
    <fo:retrieve-marker retrieve-class-name="carryover-label"/>
  </fo:table>
</fo:static-content>

Then, the <fo:marker/> elements are positioned in the data table itself:

<fo:flow flow-name="xsl-region-body">
  <fo:table>
    <!-- Render the same <fo:table-column/> elements -->
    <xsl:call-template name="TableColumns"/>

    <fo:table-body>
      <xsl:for-each select="/data-path">
        <!-- fo:table-row, fo:table-cell, fo:block 
             omitted for the example -->
        <xsl:choose>
          <xsl:when test="position() = 1">
            <fo:marker marker-class-name="carryover-label">
              Last month's balance</fo:marker>
          </xsl:when>
          <xsl:otherwise>
            <fo:marker marker-class-name="carryover-label">
              Last page's balance</fo:marker>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</fo:flow>

Now the two tables only have to be positioned exactly one on top of the other. For my problem, that's a valid workaround.



来源:https://stackoverflow.com/questions/7930833/how-to-display-one-or-the-other-information-depending-on-the-page-number-in-xsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!