当我遇到这个问题时,我正在使用 iText 生成 PDF 报告,并编写了一个简单的示例来说明它。
我正在组合简单的段落和图像。
图像的高度使得 3 个图像适合 PDF 页面,但如果页面上有文本,则仅适合 2 个图像。
我使用以下代码创建 PDF:
Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, fileOutput);
document.open();
document.add(new Paragraph("hello world1"));
addImage(document);
addImage(document);
addImage(document);
document.add(new Paragraph("hello world2"));
document.close();
我希望输出看起来像这样
hello world1
image
image
<page break>
image
hello world2
相反,我得到的是,
Hello world 1
image
image
hello world 2
<page break>
image
我没有使用 iText 设置任何奇怪的换行参数,该示例实际上只是一个简单的示例。
关于为什么它似乎会错误地自动换行有什么想法吗?
在实际情况中,仅添加分页符并不是可接受的解决方案。
非常感谢。
最佳答案
我自己想办法;)
writer.setStrictImageSequence(true);
iText 的设计决策是不将图像切成两半,而是先添加其他内容。
设置此 boolean 值会使 iText 遵守顺序。
关于java - iText 页面换行 - 更改元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/491752/