繁体   English   中英

通过xslt和xsl:fo生成pdf时,根据pdf上的页码打印一些文本

[英]Print some text depending page number on pdf while generating pdf through xslt and xsl:fo

我正在使用以下lib / namespaces从xml生成pdf

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:math="http://exslt.org/math" 
    xmlns:exslt="http://exslt.org/common"
    xmlns:str="http://exslt.org/strings"
    exclude-result-prefixes="exslt math str">

我的要求是我要打印一些文本fo:block-container文本将打印在第二页上。

<fo:block-container xsl:use-attribute-sets="section border_red">
    <fo:block xsl:use-attribute-sets="header">Product:</fo:block>                   
        <xsl:for-each select="ROWSET2/ROWSET2_ROW">
            <fo:block xsl:use-attribute-sets="paragraph indented"><xsl:value-of select="PRODUCTNAME"/></fo:block>
        </xsl:for-each>
        <!-- when page not first print something at the end of this block -->               
</fo:block-container>

因此,当块溢出到2ed,3ed ..页面时,如果块在第一页完成,则不需要在文本中打印,从而在块内进行打印。

尝试过“ fo:retrieve-table-marker”

                <fo:table>
                    <fo:table-header>
                        <fo:table-cell>
                            <fo:block xsl:use-attribute-sets="header">Product:
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-header>

                    <fo:table-footer>
                        <fo:table-row>
                            <fo:table-cell>

                                <fo:retrieve-table-marker
                                    retrieve-class-name="test123" retrieve-boundary-within-table="table"
                                    retrieve-position-within-table="last-starting" />

                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-footer>

                    <fo:table-body>
                        <xsl:for-each select="ROWSET2/ROWSET2_ROW">
                            <xsl:choose>
                                <xsl:when test="position() != last()">
                                    <fo:table-row>
                                        <fo:marker marker-class-name="test123">To be continued...
                                        </fo:marker>
                                        <fo:table-cell>
                                            <fo:block xsl:use-attribute-sets="paragraph indented">
                                                <xsl:value-of select="PRODUCTNAME" />
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </xsl:when>
                                <xsl:otherwise>
                                    <fo:table-row>
                                        <fo:marker marker-class-name="test123"></fo:marker>
                                        <fo:table-cell>
                                            <fo:block xsl:use-attribute-sets="paragraph indented">
                                                <xsl:value-of select="PRODUCTNAME" />
                                                last
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                    </fo:table-body>
                </fo:table>

如果当fo:block-container分成多个页面时,您的要求是在页面末尾输出特殊文本,那么XSL 1.1和XSL-FO处理器供应商的扩展中就没有这样的功能(据我所知)。

但是,如果您使用fo:table,fo:table-footer,fo:retrieve-table-marker和fo:marker并具有表外部规则,并且每行没有规则,则当表被分成两部分时,可能会输出特殊文本多页。

[XSL 1.1] 6.7.7 fo:table-footer

[XSL 1.1] 6.13.7 fo:retrieve-table-marker =“

[XSL 1.1] 6.13.5 fo:marker

请参阅以下博客文章(日语)中的示例快照:

テーブルのタイトルに“(续)”と出す。 (3)

第一页

第二页

最后一页

在此示例中,“继续到下一页”在fo:table-cell的fo:marker中定义,并通过fo:retrieve-table-marker在fo:table-footer中显示。

样本数据:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
    <ROWSET2>
        <ROWSET2_ROW><PRODUCTNAME>Product1</PRODUCTNAME></ROWSET2_ROW>
        <ROWSET2_ROW><PRODUCTNAME>Product2</PRODUCTNAME></ROWSET2_ROW>
        ...
        <ROWSET2_ROW><PRODUCTNAME>Product24</PRODUCTNAME></ROWSET2_ROW>
        <ROWSET2_ROW><PRODUCTNAME>Product25</PRODUCTNAME></ROWSET2_ROW>
    </ROWSET2>
</ROOT>

示例代码:

<xsl:template match="ROOT">
    <fo:table width="100%">
        <fo:table-header>
            <fo:table-cell>
                <fo:block xsl:use-attribute-sets="header">Product:
                </fo:block>
            </fo:table-cell>
        </fo:table-header>
        <fo:table-footer>
            <fo:table-row>
                <fo:table-cell>
                    <fo:retrieve-table-marker
                        retrieve-class-name="test123"/>
                </fo:table-cell>
            </fo:table-row>
        </fo:table-footer>
        <fo:table-body>
            <xsl:for-each select="ROWSET2/ROWSET2_ROW">
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block xsl:use-attribute-sets="paragraph indented">
                            <xsl:choose>
                                <xsl:when test="position() = 1">
                                    <fo:marker marker-class-name="test123">
                                        <fo:block xsl:use-attribute-sets="indented" color="teal">To be continued...</fo:block>
                                    </fo:marker>
                                </xsl:when>
                                <xsl:when test="position() = last()">
                                    <fo:marker marker-class-name="test123"/>
                                </xsl:when>
                            </xsl:choose>
                            <xsl:value-of select="PRODUCTNAME" />
                            <xsl:if test="position()=last()">
                                <fo:inline> Last</fo:inline>
                            </xsl:if>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </xsl:for-each>
        </fo:table-body>
    </fo:table>
</xsl:template>

样本结果:

样本PDF

找到的解决方案:以下代码与apache fop2.3一起使用

当我在<fo:marker> <fo:inline>使用<fo:inline> ,它的工作原理。

    <fo:table width="100%" xsl:use-attribute-sets="section border_red">
            <fo:table-header>
                <fo:table-cell>
                    <fo:block xsl:use-attribute-sets="header">Product:
                    </fo:block>
                </fo:table-cell>
            </fo:table-header>
            <fo:table-footer>
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block text-align="end">
                        <fo:retrieve-table-marker
                            retrieve-class-name="test123"
                            retrieve-boundary-within-table="table"
                            retrieve-position-within-table="last-ending"
                            />
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-footer>
            <fo:table-body>
                <xsl:for-each select="ROWSET2/ROWSET2_ROW">
                    <fo:table-row>
                        <fo:table-cell>
                            <fo:block xsl:use-attribute-sets="paragraph indented">
                                <xsl:choose>
                                    <xsl:when test="position() = 1">
                                        <fo:marker marker-class-name="test123">                                        
                                            <fo:inline>To be continued...</fo:inline>
                                        </fo:marker>
                                    </xsl:when>
                                    <xsl:when test="position() = last()">
                                        <fo:marker marker-class-name="test123"/>
                                    </xsl:when>
                                </xsl:choose>
                                <xsl:value-of select="PRODUCTNAME" />
                                <xsl:if test="position()=last()">
                                    <fo:inline>Last..</fo:inline>
                                </xsl:if>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </xsl:for-each>
            </fo:table-body>
        </fo:table> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM