有没有办法为 itextpdf 7 中的元素创建容器

Is there a way to create a container for elements in itextpdf 7

我想创建一种容器,以便我可以为多个元素的 itext 中的文档评估分页符。 这就是我在 HTML:

中要做的
<div style={{page-break-inside: avoid}}>
   <p>Row 1</p>
   <p>Row 2</p>
</div>

我如何在 itextpdf 中做到这一点?

您可以实现内部分页:避免使用应用于 div 的 setKeepTogether 方法。

Div div = new Div()
.setKeepTogether(true);

要向 div 添加元素,只需使用 .add 即可:

div.add(new Paragraph("Hello World");

iText 7 Building Blocks ebook 第 4 章的 iText 知识库中的“使用 Div class 对元素进行分组”部分的特色内容。

另一方面,如果您想要分页符,可以使用 document.add(new AreaBreak());

将其添加到文档中的任何位置