iText pdf library
Website search

Why aren't images added sequentially?

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


CHART IMAGE for topic 1 (from bytearray)


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:


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

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now