如何在PDF报告中使用XSLT显示页面编号(N of N)

19

我正在使用XSLT生成PDF报告。我的要求是在报告的页脚以“Page N of N”的格式显示页码(例如“Page 1 of 3”)。对于静态值,它可以正常工作并在每个页面上重复出现。由于无法知道报告中总页数且运行时会发生变化,因此我该如何完成这项任务。

我的XSLT代码片段:

<xsl:template name="footerall">
<xsl:variable name="maxwidth" select="7.07000" />
<fo:static-content flow-name="xsl-region-after">
<fo:block>
<xsl:variable name="tablewidth29" select="$maxwidth * 1.00000" />
<xsl:variable name="sumcolumnwidths29" select="0.04167 + 1.56250 + 0.04167" />
<xsl:variable name="factor29">
<xsl:choose>
<xsl:when
test="$sumcolumnwidths29 &gt; 0.00000 and $sumcolumnwidths29 &gt; $tablewidth29">
<xsl:value-of select="$tablewidth29 div $sumcolumnwidths29" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1.000" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="defaultcolumns29" select="1" />
<xsl:variable name="defaultcolumnwidth29">
<xsl:choose>
<xsl:when test="$factor29 &lt; 1.000">
<xsl:value-of select="0.000" />
</xsl:when>
<xsl:when test="$defaultcolumns29 &gt; 0">
<xsl:value-of
select="($tablewidth29 - $sumcolumnwidths29) div $defaultcolumns29" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0.000" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="columnwidth29_0" select="$defaultcolumnwidth29" />
<xsl:variable name="columnwidth29_1" select="1.56250 * $factor29" />
<fo:table width="{$tablewidth29}in" border-collapse="separate"
border-separation="0.04167in" color="black" display-align="center">
<fo:table-column column-width="{$columnwidth29_0}in" />
<fo:table-column column-width="{$columnwidth29_1}in" />
<fo:table-body>
<fo:table-row>
<fo:table-cell number-columns-spanned="2"
padding-top="0.00000in" padding-bottom="0.00000in" padding-left="0.00000in"
padding-right="0.00000in">
<fo:block padding-top="1pt" padding-bottom="1pt">
<fo:block text-align="center" space-before.optimum="-8pt">
<fo:leader leader-length="100%" leader-pattern="rule"
rule-thickness="1pt" color="black" />
</fo:block>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell
font-size="inherited-property-value(&apos;font-size&apos;) - 2pt"
text-align="left" padding-top="0.00000in" padding-bottom="0.00000in"
padding-left="0.00000in" padding-right="0.00000in">
<fo:block padding-top="1pt" padding-bottom="1pt">
<fo:inline font-family="Courier" font-size="10px">
<xsl:value-of select="$My XPath to varaible" />
</fo:inline>
<fo:inline font-family="Courier" font-size="10px">
<xsl:text> - </xsl:text>
<xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
<xsl:text>Page 1 of 1</xsl:text>
</fo:inline>
<fo:inline font-family="Courier" font-size="10px">
<xsl:value-of select="$My XPath to varaible" />
</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell
font-size="inherited-property-value(&apos;font-size&apos;) - 2pt"
text-align="right" padding-top="0.00000in" padding-bottom="0.00000in"
padding-left="0.00000in" padding-right="0.00000in">
<fo:block padding-top="1pt" padding-bottom="1pt" />
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:block>
</fo:static-content>
</xsl:template>

我将会用变量替换字符串"(Page 1 of 1)",但是如何处理呢。

请帮帮我。

2个回答

29

我按照以下步骤解决了我的问题。

<body> 结束标签处添加一个带有 id 的格式化对象。然后,您可以执行跳转到文档最后一页上出现的已标记块。这是标记的样子:

</body> 后添加:
<fo:page-sequence master-reference="last-page"></fo:page-sequence>

<fo:flow flow-name="xsl-region-body">
... Lots and lots of content here
<fo:block id="TheVeryLastPage"> </fo:block>
</fo:flow>

这段代码创建了一个块,其id为TheVeryLastPage(这个值很少被使用),现在您可以引用该id来获取文档的最后一页的页码。以下是area区域中内容应如何显示:

<fo:block text-align="end">
Page <fo:page-number/> of 
<fo:page-number-citation 
ref-id="TheVeryLastPage"/>
</fo:block>

FOP格式化此标记时,会生成类似于“第2页,共5页”的内容。

我参考的网址是:http://www.ibm.com/developerworks/xml/tutorials/x-xslfo2/section4.html


3
只要在最后一页上放置一个包含被引用ID属性的对象(例如你的示例中的fo:flow),就可以使用之前的答案来解决问题,而不必添加一个空块。这样,你就有了fo:flow flow-name="xsl-region-body" id="document",然后你可以使用fo:page-number-citation-last ref-id="document"。 - Kevin Brown

10

1
样例代码中第三行有一个小笔误。<xsl:... 无论如何,这里似乎少了点什么,没有任何输出。我无法弄清楚哪里出了问题。 - masche
2
xsl:text页面</xsl:text> fo:page-number/ xsl:text / </xsl:text> <fo:page-number-citation-last ref-id="sequence"/> 以获取可用版本。 - Jan
1
这在多区域页面序列中会失败,它显示的页码如下: 1/4, 2/4, 3/4, 5/4, 6/6, 7/6, 8/8。 - c.sankhala
2
使用fo:page-number-citation-last时出现错误:Invalid property encountered on "fo:page-number-citation-last": page-citation-strategy (No context info available)。有任何想法如何解决吗? 如果没有page-citation-strategy="all",则该错误会消失,但是总页数将显示错误的数字,例如'1 of 1','2 of to2'等。 - Cengiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接