XSL-FO表格违规组分页问题:避免单组跨页显示
解决XSL-FO违规组不跨页的方案
核心思路
把每个3行的违规记录组用<fo:table-row-group>包裹,给这个组添加keep-together.within-page="always"属性,强制整个组保持在同一页面内,分页仅在组与组之间触发。
修改后的代码示例
<fo:table table-layout="fixed" width="100%"> <fo:table-column/> <fo:table-column/> <fo:table-column/> <fo:table-body> <!--Violation Group 1--> <fo:table-row-group keep-together.within-page="always"> <fo:table-row> <fo:table-cell><fo:block>Violation 1 Attrib 1</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 2</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 3</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 1 Attrib 4</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 5</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 6</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 1 Attrib 7</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 8</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 9</fo:block></fo:table-cell> </fo:table-row> </fo:table-row-group> <!--Violation Group 2 - 分页仅会在此类组之间发生--> <fo:table-row-group keep-together.within-page="always"> <fo:table-row> <fo:table-cell><fo:block>Violation 2 Attrib 1</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 2 Attrib 2</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 2 Attrib 3</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 2 Attrib 4</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 2 Attrib 5</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 2 Attrib 6</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 2 Attrib 7</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 2 Attrib 8</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 2 Attrib 9</fo:block></fo:table-cell> </fo:table-row> </fo:table-row-group> <!--Violation Group 3--> <fo:table-row-group keep-together.within-page="always"> <fo:table-row> <fo:table-cell><fo:block>Violation 3 Attrib 1</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 3 Attrib 2</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 3 Attrib 3</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 3 Attrib 4</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 3 Attrib 5</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 3 Attrib 6</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 3 Attrib 7</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 3 Attrib 8</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 3 Attrib 9</fo:block></fo:table-cell> </fo:table-row> </fo:table-row-group> </fo:table-body> </fo:table>
替代方案(无row-group场景)
如果你的FO处理器对table-row-group支持有限,可以给每个违规组的前两行添加keep-with-next.within-page="always"属性,确保前两行必须和第三行在同一页:
<!--Violation Group 1--> <fo:table-row keep-with-next.within-page="always"> <fo:table-cell><fo:block>Violation 1 Attrib 1</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 2</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 3</fo:block></fo:table-cell> </fo:table-row> <fo:table-row keep-with-next.within-page="always"> <fo:table-cell><fo:block>Violation 1 Attrib 4</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 5</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 6</fo:block></fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell><fo:block>Violation 1 Attrib 7</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 8</fo:block></fo:table-cell> <fo:table-cell><fo:block>Violation 1 Attrib 9</fo:block></fo:table-cell> </fo:table-row>
注意事项
keep-together.within-page="always"会强制整个组留在同一页,如果单个组高度超过页面剩余空间,FO处理器会自动将整个组移到下一页。- 该属性兼容XSL 1.0标准,符合你的使用环境。
内容的提问来源于stack exchange,提问作者Sergey Novitsky
相关产品推荐
相关产品推荐

