iText页面包装 – 改变元素的顺序

当我遇到这个问题时,我正在使用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  image hello world2 

相反,我得到的是,

 Hello world 1 image image hello world 2  image 

我没有使用iText设置任何奇怪的包装参数,这个例子真的很简单。

关于为什么它似乎是自动包装这个错误的任何想法?

在实际情况中,仅添加分页符不是可接受的解决方案。

非常感谢。

自己搞清楚;)

 writer.setStrictImageSequence(true); 

iText中的设计决定是不将图像切成两半,而是首先添加其他内容。

设置此布尔值会使iText遵守排序。