我正在使用OpenPDF生成一个文档。
在本文档中,我希望对某些元素进行分组,这样它们就不会在单独的页面上被分割。在我的例子中,这是一个由2行包围的段落,上面的1行和下面的1行。
我尝试过将它们分组到一个段落元素中,并使用keepTogether属性,但是这些行没有显示出来:
for (final String line : lines) {
Paragraph para = new Paragraph(line, font);
para.setSpacingAfter(20);
doc.add(para);
para.add(new LineSeparator());
para.add(0, new LineSeparator());
para.setKeepTogether(true);
}有什么办法让我在任何时候都把这些放在一起吗?还是有更好的建议?
信息
发布于 2020-05-24 11:34:33
我用一张桌子来解决这个问题:
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(527);
table.setLockedWidth(true);
for (final String line : lines) {
PdfPCell cell = new PdfPCell(new Phrase(line, font));
cell.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
cell.setPaddingBottom(20);
cell.setPaddingTop(20);
table.addCell(cell);
}
doc.add(table);https://stackoverflow.com/questions/61984123
复制相似问题