如何在PDF中使用xslt显示页码(N of N)报告

问题描述:

我正在使用XSLT生成PDF报告。我的要求是以报告页脚的N页(例如,第1页,共3页)格式显示页码。对于静态值,它可以正常工作,并在每个页面上重复。由于报告中的页面总数未知,并且会改变运行时间,所以我将如何完成此任务。如何在PDF中使用xslt显示页码(N of N)报告

我的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> 

我将用变量,但如何处理这种替换字符串(第1页1)。

请帮帮我。

我按以下说明解决我的问题。

把一个格式化对象与一个ID在区域的末尾。然后,您可以执行文档最后一页上显示的标签块。这里的标记的外观:

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

的代码创建一个块与TheVeryLastPage(那是不可能被任何人使用的值)的ID,而现在你可以参考该ID来获得的页码文档的最后一页。以下是该区域内容的外观:

<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” –

您应该将id属性添加到您的fo:page-sequence元素,然后使用page-number-citation-last

<xsl:text>Page </xsl:text> 
<fo:page-number-citation /> 
<xs:text> of </xsl:text> 
<fo:page-number-citation-last page-citation-strategy="all" ref-id="my-sequence-id"/> 

看规格:http://www.w3.org/TR/xslfo20/#fo_page-number-citationhttp://www.w3.org/TR/xslfo20/#fo_page-number-citation-last

+0

示例代码中第三行的小错字。 masche

+0

/ 用于工作的版本。 – Jan