Why aren't images added sequentially?

Category: 
Tags: imagesimage sequenceiText 7

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

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 < 15) {
    doc.add(p);
    doc.add(img);
    i++;
}
doc.close();

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