iText pdf library
Website search

Why aren't images added sequentially?

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:


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

There is no such problem in iText 7. Look for the resulting PDF in this example:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Image img = new Image(ImageDataFactory.create(IMAGE));
Paragraph p = new Paragraph("Quick brown fox");
int i = 0;
while (i 

Click this link if you want to see how to answer this question in iText 5.


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