XSL-FO 不同的页眉/页脚取决于页面位置 [英] XSL-FO Different header/footer depending on page-position
问题描述
这可能是 XSL-FO 的一个很常见的问题:我尝试构建一个具有明确规范的计费:
This might be a quite common problem with XSL-FO: I try to build a billing which has clear specifications:
- 主标题":在每个页面上(文本、徽标和条形码)
子标题":(客户数据)
- "Main Header": on every page (Text, Logo and Barcode)
"Sub Header": (Customer Data)
-> 在第一页,这个块应该是页面高度的 10% 左右
-> On the first page this block should be about 10% of the pages height
-> 在所有其他页面上,这个块应该小一半,所以说 5%
-> On all the others page this block should be about the half smaller, so lets say 5%
最后一页页脚":当然就在最后一页(总金额和签名)
"Last-Page Footer": just on the last page of course (Total amount and signatures)
每页页脚":仅用于打印日期
"Every-page Footer": just for the printing date
正文":帐单的内容(每个位置)应该自动在所有页眉和页脚之间流动
"Body": the content of the billing (every position) should flow in between of the all headers and footers automatically
所以我知道有些点可以通过使用属性page-position来实现:
So i know that some points can be realised by using the attribute page-position:
<fo:page-sequence-master master-name="masterSequenceName1">
<fo:repeatable-page-master-alternatives>
<fo:conditional-page-master-reference master-reference="masterNamePageFirst1" page-position="first"></fo:conditional-page-master-reference>
<fo:conditional-page-master-reference master-reference="masterNamePageLast1" page-position="last"></fo:conditional-page-master-reference>
<fo:conditional-page-master-reference master-reference="masterNamePageAny" page-position="any"></fo:conditional-page-master-reference>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
我可以为每种情况定义区域,但存在以下问题:
I can define the regions for each of this cases but having these issues:
- 如果第一页是最后一页(只有一页),则不会出现最后一页页脚.知道 page-position 可以具有only"值,也可能为此设置静态内容.但是我仍然会有两个相同内容的块,每当我想编辑这部分时,我都必须更改两次.
- 主标题通常可以由 region-before 设置,并引用 page-position="any"(这是标准),但不知何故这对我不起作用.我只是在不是第一个或最后一个的页面上获取标题.这实际上不应该是 page-position="rest" 所需的功能吗?
推荐答案
page-position
属性声明(强调):
A note in the definition of the page-position
property states that (emphasis added):
这些值中的几个可以同时为真;例如,任何"始终为真,并且当 'first' 和 'last' 都为真时,'only' 为真真的.因此,有必要订购fo:conditional-page-master-references 使包含最少的测试在更具包容性的测试之前执行,这也是正确的.
Several of these values can be true simultaneously; for example, 'any' is always true and 'only' is true when both 'first' and 'last' are true. For that reason, it is necessary to order the fo:conditional-page-master-references so that the least inclusive test is performed before the more inclusive test which are also true.
换句话说,被选为构建页面的conditional-page-master-reference
是第一个条件评估为真的,后面的引用是甚至没有考虑.
In other words, the conditional-page-master-reference
that is elected to build a page is the first one whose conditions evaluate to true, and following references are not even taken into consideration.
另一个需要记住的重点是条件(page-position
、odd-or-even
、blank-or-not-blank
) 选择页面母版,而不是特定的静态内容.
Another important point to remember is that conditions (page-position
, odd-or-even
, blank-or-not-blank
) select a page master, not a specific static content.
所以,看看你的问题:
1.如果第一页是最后一页(只有一页),最后一页页脚不会出现.
发生这种情况是因为适用于第一个也是唯一一个页面的第一个 conditional-page-master-reference
在您的示例中是指向 "masterNamePageFirst1"
的那个(我想,因为它不在问题中)有一个区域之后,只有每个页面页脚"被映射到其中.
this happens because the first conditional-page-master-reference
applicable to the first and only page is, in your example, the one pointing to "masterNamePageFirst1"
which (I guess, as it is not in the question) has a region-after into which only the "every page footer" is mapped.
知道 page-position 可以具有值only",这是一种可能性也将为此设置一个静态内容.但我还是会两个相同内容的块,每当我想编辑它时部分,我将不得不更改它两次.
您不必重复两次相同的内容:您可以将其放入一个命名模板,然后从两个静态内容内部调用该模板.
You don't have to repeat twice the same content: you can put it into a named template, and call that template from inside the two static contents.
2.主标题通常可以由区域之前设置引用 page-position="any" (这是标准),但不知何故这对我不起作用.我只是在页面上获取标题不是第一个或最后一个.这不应该实际上是所需的功能吗页面位置="休息"?
静态内容映射到一个区域:检查该区域是否实际存在第一页和最后一页的页面母版中.
Static content is mapped to a region: check whether that region is actually present in the page masters for the first and for the last page.
完整示例
这是一个示例,它使用 显示了 不同的页面几何形状 only/first/rest/any 页面和根据页面位置的不同页眉/页脚="http://www.w3.org/TR/xsl/#fo_marker" rel="nofollow">fo:marker
s 和 fo:retrieve-marker
s.
Here is an example that shows both different page geometry for only/first/rest/any page and different headers/footers according to the page position using fo:marker
s and fo:retrieve-marker
s.
我使用 FOP 1.1 对其进行了测试,生成的 pdf 应该符合您的规范.
I tested it with FOP 1.1, and the resulting pdf should conform to your specifications.
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="singlePage" margin="1cm">
<fo:region-body margin-top="5cm" margin-bottom="4cm" background-color="#FFFFAA"/>
<fo:region-before extent="4cm" background-color="#AAFFFF"/>
<fo:region-after extent="3cm" background-color="#FFAAFF" display-align="after"/>
</fo:simple-page-master>
<fo:simple-page-master master-name="firstPage" margin="1cm">
<fo:region-body margin-top="5cm" margin-bottom="2cm" background-color="#FFFFAA"/>
<fo:region-before extent="4cm" background-color="#AAFFFF"/>
<fo:region-after extent="1cm" background-color="#FFAAFF" display-align="after"/>
</fo:simple-page-master>
<fo:simple-page-master master-name="middlePage" margin="1cm">
<fo:region-body margin-top="2cm" margin-bottom="2cm" background-color="#FFFFAA"/>
<fo:region-before extent="1cm" background-color="#AAFFFF"/>
<fo:region-after extent="1cm" background-color="#FFAAFF" display-align="after"/>
</fo:simple-page-master>
<fo:simple-page-master master-name="lastPage" margin="1cm">
<fo:region-body margin-top="2cm" margin-bottom="4cm" background-color="#FFFFAA"/>
<fo:region-before extent="1cm" background-color="#AAFFFF"/>
<fo:region-after extent="3cm" background-color="#FFAAFF" display-align="after"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="allPages">
<fo:repeatable-page-master-alternatives>
<fo:conditional-page-master-reference page-position="only" master-reference="singlePage"/>
<fo:conditional-page-master-reference page-position="first" master-reference="firstPage"/>
<fo:conditional-page-master-reference page-position="rest" master-reference="middlePage"/>
<fo:conditional-page-master-reference page-position="last" master-reference="lastPage"/>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="allPages">
<!--
header
-->
<fo:static-content flow-name="xsl-region-before" font-size="90%">
<!-- main header on every page -->
<fo:block>Text, logo, barcode</fo:block>
<!-- sub header -->
<fo:retrieve-marker retrieve-class-name="subHeader" retrieve-position="first-starting-within-page"/>
</fo:static-content>
<!--
footer
-->
<fo:static-content flow-name="xsl-region-after" font-size="90%">
<!-- special footer -->
<fo:retrieve-marker retrieve-class-name="footer" retrieve-position="first-starting-within-page"/>
<!-- common footer on every page -->
<fo:block>printing date dd/mm/yyyy</fo:block>
</fo:static-content>
<!--
document body
-->
<fo:flow flow-name="xsl-region-body">
<!-- empty blocks with markers for the header -->
<fo:block>
<!-- sub header for the first page -->
<fo:marker marker-class-name="subHeader">
<fo:block>LARGE SUB HEADER</fo:block>
</fo:marker>
</fo:block>
<fo:block>
<!-- sub header for the not-first pages -->
<fo:marker marker-class-name="subHeader">
<fo:block>small sub header</fo:block>
</fo:marker>
</fo:block>
<!-- normal content -->
<!--
YOUR REAL CONTENT GOES HERE
(I just put some blocks with page breaks to produce a few pages)
-->
<fo:block break-after="page">Long sequence with many pages ...</fo:block>
<fo:block break-after="page">... bla bla bla ...</fo:block>
<fo:block>... the end</fo:block>
<!-- empty block with marker for the footer -->
<fo:block>
<!-- footer for the last page -->
<fo:marker marker-class-name="footer">
<fo:block>TOTAL AMOUNT $$$ AND SIGNATURES</fo:block>
</fo:marker>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
这篇关于XSL-FO 不同的页眉/页脚取决于页面位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!