【问题标题】:XSL-FO: Absolute positioning of allocation rectangle rather than content rectangleXSL-FO:分配矩形而不是内容矩形的绝对定位
【发布时间】:2017-08-03 18:57:52
【问题描述】:

我正在使用 XSL-FO(使用 Apache FOP 作为渲染引擎)实现“仪表板”的 PDF 导出。这些仪表板被定义为在特定大小的区域内呈现的一组矩形元素,其位置由上/左偏移百分比和宽度/高度百分比定义。使用 XSL-FO 绝对定位进行渲染似乎是显而易见的解决方案,并且在大多数情况下效果很好。

以下 XSL-FO:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-size="10pt" id="sequence-id">
<fo:layout-master-set>
<fo:simple-page-master master-name="page-layout" page-width=" 4in " page-height=" 4in ">
    <fo:region-body region-name="document-content" margin="0.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page-layout" initial-page-number="1">
<fo:flow flow-name="document-content">
<fo:block-container border="1px solid green" height="3in">
<fo:block-container absolute-position="absolute" width="50%" left="0" top="0" height="40%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content. It is possible for me to occupy the entire width of my allocated area.</fo:block>
</fo:block-container>
<fo:block-container absolute-position="absolute" width="50%" left="50%" top="0" height="50%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
<fo:block-container absolute-position="absolute" width="50%" left="0" top="40%" height="60%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
<fo:block-container position="absolute" width="50%" left="50%" top="50%" height="50%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
</fo:block-container>
</fo:flow>
</fo:page-sequence>
</fo:root>

...产生以下输出:

代表仪表板元素内容的绿色区域相互对接,很难看。所以,我想在内容周围添加一些填充。如果我向代表第一个仪表板元素的block-container 添加填充:

<fo:block-container absolute-position="absolute" padding="5px" width="50%" left="0" top="0" height="40%" border="1px solid red">

...仪表板元素的(红色)边框向外扩展,而不是向内添加填充:

这是因为,与 CSS 相比,XSL-FO 中的绝对定位偏移针对的是内容矩形(即内边距),而不是包括内边距和边框的矩形。每the spec:

这些属性设置相关区域内容矩形的位置。

我想要一些方法在我通过绝对定位属性分配给仪表板元素的区域内添加填充/边框。我已经尝试了各种将内容放在另一个嵌套的block-container 或block 中的方法,但我尝试过的所有方法都无法正常工作。作为最后的手段,我可​​以计算偏移量/尺寸以有效地在元素之间留下一些空白空间,但这感觉像是应该由渲染引擎自动处理的东西。有什么建议吗?

【问题讨论】:

  • 填充=0,边距=#?或 margin=0, padding=# ...我忘了。测试它们。
  • @KevinBrown - 使用 'margin' 而不是 'padding' 确实增加了水平方向的空间,但没有增加垂直方向的空间。对于我的目的来说,这可能已经足够了;如果您想发布有关该效果的答案,我会将其标记为解决方案。解释为什么它起作用的奖励积分(因为它对我来说真的没有意义 - 边距出现在边框内?!),或者用于处理垂直方向的方法!谢谢。

标签: xsl-fo apache-fop


【解决方案1】:

所有维度的最简单方法...在块容器中添加一个块,您可以在其中添加所有内容。这个块应该有你想要的边距。所以:

    <fo:flow flow-name="document-content">
        <fo:block-container border="1px solid green" height="3in">
            <fo:block-container absolute-position="absolute" width="50%" left="0" top="0" height="40%" border="1px solid red">
                <fo:block margin="5px">
                    <fo:block border="1px solid blue" background-color="green">I am some content. It is possible for me to occupy the entire width of my allocated area.</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container absolute-position="absolute" width="50%" left="50%" top="0" height="50%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container absolute-position="absolute" width="50%" left="0" top="40%" height="60%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container position="absolute" width="50%" left="50%" top="50%" height="50%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
        </fo:block-container>
    </fo:flow>

产生这个:

【讨论】:

  • 例如,如果您只使用红色边框并且它们不会出现,您可以将它们设置为白色和厚实以模仿相同的结果
猜你喜欢
  • 2015-11-04
  • 1970-01-01
  • 2023-01-08
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多