xml 如何为xsl-fo中的每个页面添加页眉和页脚以生成pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20657579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add header and footer for every pages in xsl-fo to generate pdf
提问by Vishnu
please find the following xsl-fo , tried to set header and footer for every page in pdf, but got only header at first page and footer at last page. But here i needed for every page. How to perform this.
请找到以下 xsl-fo ,尝试为 pdf 中的每个页面设置页眉和页脚,但只有第一页的页眉和最后一页的页脚。但在这里我需要每一页。如何执行此操作。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match='/'>
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="0.1cm"
margin-left="0.8cm"
margin-right="1.0cm" >
<fo:region-body margin-top="2.5cm" margin-bottom="2.5cm"/>
<fo:region-before extent="2.0cm"/>
<fo:region-after extent="2.0cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-before">
<fo:block>
Message Body
</fo:block>
</fo:flow>
<fo:flow flow-name="xsl-region-body">
Message Content
</fo:flow>
<fo:flow flow-name="xsl-region-after">
<h2>
Page Footer
</h2>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template name="replace-returns">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<xsl:value-of select="substring-before($text, '
')"/>
<xsl:value-of select="'<br />'" disable-output-escaping="yes"/>
<xsl:call-template name="replace-returns">
<xsl:with-param name="text" select="substring-after($text, '
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
回答by PhillyNJ
Below is a simple example on how to get a header and footer on each page. Hope this helps
下面是一个关于如何在每个页面上获取页眉和页脚的简单示例。希望这可以帮助
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple" page-height="11in" page-width="8.5in" margin-top=".5in" margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
<fo:region-body region-name="xsl-region-body" margin-bottom=".5in" margin-top=".50in"/>
<fo:region-before region-name="xsl-region-before" extent="5in"/>
<fo:region-after region-name="xsl-region-after" extent=".5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:static-content flow-name="xsl-region-before">
<fo:block>header</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block>footer</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block break-after="page">
Body
</fo:block>
<fo:block>
Body
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
回答by Andreas
If you want header and footer, don't put them into the fo:flowelement. They belong into <fo:static-content flow-name="your_flow">where your_flowcan be xsl-region-beforeor xsl-region-afteror any other name you like to give.
如果您想要页眉和页脚,请不要将它们放入fo:flow元素中。它们属于<fo:static-content flow-name="your_flow">where your_flowcan bexsl-region-before或xsl-region-after您喜欢的任何其他名称。
This leads to the missing definition of your regions. The ones you use are not defined. Make this work like <fo:region-body region-name="xsl-region-body">or <fo:region-before region-name="xsl-region-before">
这会导致您的区域缺少定义。你使用的那些没有定义。使这项工作像<fo:region-body region-name="xsl-region-body">或<fo:region-before region-name="xsl-region-before">
I didn't check if something else prevents the script from working, but your question should be answered with this.
我没有检查是否有其他原因阻止脚本工作,但您的问题应该用这个来回答。

