iText page wrapping- changes order of elements

安稳与你 提交于 2019-12-07 20:25:21

问题


I'm using iText to generate PDF reports when I came across this issue, and worked up a simple example to illustrate it.

I'm combining simple paragraphs, and images.

The height of the images is such that 3 will fit on a PDF page, but when if text is on a page, only 2 images will fit.

I create my PDF with the following code:

    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();

I expect the output to look like this

hello world1
image
image
<page break>
image
hello world2

Instead, what I get is,

Hello world 1
image
image
hello world 2
<page break>
image

I'm not setting any sort of odd wrapping parameters using iText, the example really is just a simple one.

Any ideas on why it seems to be auto-wrapping this incorrectly?

In the real case, just adding a page break is not an acceptable solution.

Thanks very much.


回答1:


Figure it out myself ;)

writer.setStrictImageSequence(true); 

It was a design decision in iText to not cut images in two, instead it adds other content first.

setting this boolean causes iText to respect the ordering.



来源:https://stackoverflow.com/questions/491752/itext-page-wrapping-changes-order-of-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!