i am working with iTextSharp (5.4.5) for a couple of weeks now. This week, i encountered something strangew hen it comes to the order of elements in the documents.
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 ...
below is a sample of code. I know code is not completely correct but it's just to point the issue. Let's assume the loop runs 10 times, so i expect 10 topic titles all directly followed by the image.
What i noticed is, that IF the page end is reached and a new IMAGE should be added, 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 Document.add method.
This is really strange. Anyone has any idea?
int currentQuestionNr = 0;
foreach (Topic currentTOPIC in Topics)
{
currentQuestionNr++;
//compose question (via table so all questions (with nr prefix) are aligned the same)
PdfPTable questionTable = new PdfPTable(2);
questionTable.WidthPercentage = 100;
questionTable.SetWidths(new int[] { 4, 96 });
PdfPCell QuestionNrCell = new PdfPCell();
QuestionNrCell.BorderWidth = 0;
QuestionNrCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
QuestionNrCell.VerticalAlignment = PdfPCell.ALIGN_TOP;
QuestionNrCell.AddElement(new Paragraph(String.Format("{0}. ", currentQuestionNr), PdfUtility.font_10_bold));
PdfPCell QuestionPhraseCell = new PdfPCell();
QuestionPhraseCell.BorderWidth = 0;
QuestionPhraseCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
QuestionPhraseCell.VerticalAlignment = PdfPCell.ALIGN_TOP;
QuestionPhraseCell.AddElement(new Paragraph(currentTOPIC.Title, PdfUtility.font_10_bold));
questionTable.addCell(QuestionNrCell);
questionTable.addCell(QuestionPhraseCell);
//add topic to document
Document.add(questionTable)
//compose image
Image itextImage = GetImageForTopic(currentTOPIC); //let's assume this function returns an image!
Paragraph chartParagraph = new Paragraph();
chartParagraph.IndentationLeft = indentionForQuestionInfo;
chartParagraph.Add(itextImage);
//add image to document
Document.Add(chartParagraph);
}