首页 > 解决方案 > 如何防止 fo:page-number-citation 换行

问题描述

XSL-FO 处理器 - Apache FOP。

有一个非常简单的带有目录的 xsl-fo:

<?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="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
            <fo:region-body margin-top="3cm"/>
            <fo:region-before extent="3cm"/>
            <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="simple">
        <fo:flow flow-name="xsl-region-body">
            <fo:block>Block with fixed page number:</fo:block>
            <fo:block text-align-last="justify">Text text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/>1</fo:block>
            <fo:block>&#xa0;</fo:block>
            <fo:block>Block with page-number-citation to existing page:</fo:block>
            <fo:block text-align-last="justify">Text text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
            <fo:block>&#xa0;</fo:block>
            <fo:block>Block with page-number-citation to non-existing page:</fo:block>
            <fo:block text-align-last="justify">Text text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_zzz"/></fo:block>
            <fo:block id="id_test"/>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

第一个 ToC 行在页码位置包含固定文本。

第二个 ToC 行包含fo:page-number-citation第 1 页上的块 ID。

第三个 ToC 行包含fo:page-number-citation不存在的块的 id。

正如您在图片上看到的,第二个 ToC 行包含换行符。如何预防?看起来 Apache FOP 为页码保留了一些空间(参见第 3 条 ToC 行),但是否可以减少该空间?

渲染示例

更新: 我添加了属性text-align="justify"

<?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="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
            <fo:region-body margin-top="3cm"/>
            <fo:region-before extent="3cm"/>
            <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="simple">
        <fo:flow flow-name="xsl-region-body">
            <fo:block-container border-right="1pt solid red">
                <fo:block>Block with fixed page number:</fo:block>
                <fo:block text-align-last="justify">Text text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/>1</fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page:</fo:block>
                <fo:block text-align-last="justify">Text text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page (text-align="justify):</fo:block>
                <fo:block text-align-last="justify" text-align="justify">Text text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page (text-align="justify):</fo:block>
                <fo:block text-align-last="justify" text-align="justify">Test1: Text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page (text-align="justify):</fo:block>
                <fo:block text-align-last="justify" text-align="justify">Test2: Text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page (text-align="justify):</fo:block>
                <fo:block text-align-last="justify" text-align="justify">Test3: Text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page (text-align="justify):</fo:block>
                <fo:block text-align-last="justify" text-align="justify">Test4: Text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block>Block with page-number-citation to existing page (text-align="justify):</fo:block>
                <fo:block text-align-last="justify" text-align="justify">Test5: Text text text text text text text text text text text text text text text text text text<fo:leader leader-pattern="dots"/><fo:page-number-citation ref-id="id_test"/></fo:block>
                <fo:block>&#xa0;</fo:block>
                <fo:block id="id_test"/>
            </fo:block-container>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

但它有一个副作用 - 页码有时不会正确对齐(这取决于文本的长度): 在此处输入图像描述

标签: xsl-foapache-fop

解决方案


推荐阅读