如何在XSL-FO(FOP 2.1)中将图形/表格对齐到页面的顶部或底部

时间:2017-04-03 12:29:17

标签: xslt xsl-fo apache-fop

如果图或图表有点大,那么将它们移动到页面的顶部或底部是非常合适的。通过重复其标题将表重排到下一页的能力很棒,但并不总是我想要的。在案例中,表格或图形非常适合页面,并且它不一定固定在段落上,因此没有理由对其进行重排。最好让它“浮动”在页面正文之外。

This answer真的很鼓舞人心,但我不知道如何将它应用到我的案例中。如果我能按照表格的@rendition属性来做,我会很高兴。

<fo:layout-master-set>
    <fo:simple-page-master master-name="PageMaster.Title-Page" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="{$margin-top}" margin-bottom="{$margin-bottom}">
        <fo:region-body/>
    </fo:simple-page-master>
    <fo:simple-page-master master-name="PageMaster.CopyRight-Page" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="{$margin-top}" margin-bottom="{$margin-bottom}">
        <fo:region-body display-align="after"/>
    </fo:simple-page-master>
    <fo:simple-page-master master-name="PageMaster.TOC" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="18mm" margin-bottom="{$margin-bottom}">
        <fo:region-body margin="0mm 0mm 0mm 60mm"/>
    </fo:simple-page-master><!-- Whole page left and right masters -->
    <fo:simple-page-master master-name="PageMaster.Page-Right.First" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="{$margin-top}" margin-bottom="{$margin-bottom}">
        <fo:region-body margin="8mm 0mm 20mm 0mm" column-count="2" column-gap="5mm"/>
        <fo:region-before region-name="Region-Before-Right" extent="10mm" display-align="after"/>
        <fo:region-after region-name="Region-After-First" extent="20mm" display-align="before"/>
    </fo:simple-page-master>
    <fo:simple-page-master master-name="PageMaster.Page-Left" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="{$margin-top}" margin-bottom="{$margin-bottom}">
        <fo:region-body margin="8mm 0mm 20mm 0mm" column-count="2" column-gap="5mm"/>
        <fo:region-before region-name="Region-Before-Left" extent="10mm" display-align="after"/>
    </fo:simple-page-master>
    <fo:simple-page-master master-name="PageMaster.Page-Right" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="{$margin-top}" margin-bottom="{$margin-bottom}">
        <fo:region-body margin="8mm 0mm 20mm 0mm" column-count="2" column-gap="5mm"/>
        <fo:region-before region-name="Region-Before-Right" extent="10mm" display-align="after"/>
    </fo:simple-page-master>
    <fo:simple-page-master master-name="PageMaster.Blank" page-height="{$paper-height}" page-width="{$paper-width}" margin-left="{$margin-left}" margin-right="{$margin-right}" margin-top="{$margin-top}" margin-bottom="{$margin-bottom}">
        <fo:region-body margin="8mm 0mm 20mm 0mm" column-count="2" column-gap="5mm"/>
    </fo:simple-page-master><!-- Sequence of even / odd pages -->
    <fo:page-sequence-master master-name="Content-Pages">
        <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference master-reference="PageMaster.Blank" blank-or-not-blank="blank"/>
            <fo:conditional-page-master-reference master-reference="PageMaster.Page-Right.First" odd-or-even="odd" page-position="first"/>
            <fo:conditional-page-master-reference master-reference="PageMaster.Page-Left" odd-or-even="even"/>
            <fo:conditional-page-master-reference master-reference="PageMaster.Page-Right" odd-or-even="odd"/>
        </fo:repeatable-page-master-alternatives>
    </fo:page-sequence-master>
</fo:layout-master-set>

1 个答案:

答案 0 :(得分:2)

正如Tony Graham评论的那样, FOP在浮动之前不支持 (奇怪的是,它在没有警告的情况下默默地吞下它们)。

但是,您可以使用脚注代替将表格放在引用它们的页面的底部。如果我没记错的话,FOP倾向于将脚注全部放在添加更多“流动”内容上,因此结果应该是预期的内容。

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-width="14.85cm" page-height="21cm">
            <fo:region-body margin="1in"/>
        </fo:simple-page-master>
    </fo:layout-master-set>

    <fo:page-sequence master-reference="simple">
        <fo:flow flow-name="xsl-region-body" text-align="justify">
            <fo:block>Maecenas lectus nisl, pulvinar at tincidunt a, semper sit amet nibh. Morbi at dolor quis sem porta volutpat. Quisque eget magna lectus. Donec laoreet congue enim ut ullamcorper. Ut vestibulum consequat neque rhoncus laoreet.</fo:block>
            <fo:block>Here is a block referring to a <fo:footnote><fo:inline font-weight="bold">TABLE 1</fo:inline><fo:footnote-body>
                <fo:block font-weight="bold">TABLE 1:</fo:block>
                <fo:table width="100%" table-layout="fixed">
                    <fo:table-column column-width="30%"/>
                    <fo:table-column column-width="70%"/>
                    <fo:table-body>
                        <fo:table-row>
                            <fo:table-cell><fo:block>A</fo:block></fo:table-cell>
                            <fo:table-cell><fo:block>132</fo:block></fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell><fo:block>B</fo:block></fo:table-cell>
                            <fo:table-cell><fo:block>556638</fo:block></fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell><fo:block>C</fo:block></fo:table-cell>
                            <fo:table-cell><fo:block>574</fo:block></fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell><fo:block>D</fo:block></fo:table-cell>
                            <fo:table-cell><fo:block>8893</fo:block></fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:footnote-body></fo:footnote>; the table should be in the same page as the bolded part.</fo:block>
            <fo:block>This is another block of content. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tincidunt ipsum eget nisl iaculis fringilla. Pellentesque sagittis quam tellus, ac varius nisl rutrum non. Phasellus eget imperdiet velit.</fo:block>
            <fo:block>Aenean eleifend, arcu et feugiat facilisis, diam orci luctus lectus, id laoreet ex risus sed nunc. Proin eget scelerisque orci. Donec porttitor tellus libero, a molestie orci vestibulum mattis. Ut eu massa ac massa sagittis molestie. Quisque viverra ipsum vel mauris laoreet aliquet.</fo:block>
            <fo:block>Maecenas lectus nisl, pulvinar at tincidunt a, semper sit amet nibh. Morbi at dolor quis sem porta volutpat. Quisque eget magna lectus. Donec laoreet congue enim ut ullamcorper. Ut vestibulum consequat neque rhoncus laoreet.</fo:block>
            <fo:block> Sed ipsum massa, maximus eget posuere et, pellentesque accumsan nulla. Vestibulum erat mauris, imperdiet at mattis in, rhoncus eget orci. Nullam rhoncus hendrerit magna. </fo:block>  
        </fo:flow>
    </fo:page-sequence>
</fo:root>

产生的结果:

PDF screenshot

使用脚注的缺点是脚注放置的约束更严格比浮动之前的约束<(可以放在页面跟随他们的锚点 - 区域,根据section 6.12.2 of the recommentation),因此如果输入在一系列段落中引用了许多表,则可能存在布局问题(页面中的空白区域)。

(披露:我是FOP开发人员,但现在不是很活跃)