Legacy notice!

iText 5 is the previous major version of iText's leading PDF SDK. iText 5 has been EOL, and is no longer developed. Switch your project to iText 7, integrating the latest developments.
Check related iText 7 content!

The order of the elements on paper is not the same as the order that I used to put the element in the document via the document.add() method. Why is this?

I am working on a pdf report that contains topics and images (charts). The document is formatted this way:

NR. TOPIC TITLE FOR TOPIC 1

CHART IMAGE for topic 1 (from bytearray)

NR. TOPIC TITLE FOR TOPIC 2

CHART IMAGE for topic 2 ...

Let's assume that I add this information in a loop, and that the loop runs 10 times. I expect 10 topic titles all directly followed by the image.

However, if the page end is reached and a new image should be added, I notice that the image is moved to the next page and the next topic title is printed on the previous page.

So on paper we have:

page 1: topic 1
        image topic 1
        topic 2
        image topic 2
        topic 3
        topic 4
page 2: image topic 3
        image topic 4
        topic 5
        image topic 5
So the order of the elements on paper, is NOT the same as the order that I used to put the element in the document via the `document.add() method. This is really strange. Anyone has any idea?

Posted on StackOverflow on Mar 26, 2014 by wim boone

If you have a PdfWriter instance (for instance writer), you need to force iText to use strict image sequence like this:

writer.setStrictImageSequence(true);

Otherwise, iText will postpone adding images until there's sufficient space on the page to add the image.