iText page wrapping- changes order of elements

2019-05-24 21:37发布

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条回答
啃猪蹄的小仙女
2楼-- · 2019-05-24 21:50

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.

查看更多
登录 后发表回答