Image not sequentialy added in pdf document itexts

2019-05-12 05:26发布

问题:

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);
}

回答1:

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.



回答2:

That is quite unintuitive...I get the same behavior. So why not add another row to the table on each iteration:

  for (int i = 1; i < 5; ++i) {
    PdfPTable questionTable = new PdfPTable(2) {
// try and keep topic & chart together on same page for each iteration    
      KeepTogether = true, 
      WidthPercentage = 100
    };
    questionTable.SetWidths(new int[] { 4, 96 });
// _defaultCell(): 
// sets BorderWidth/HorizontalAlignment/VerticalAlignment
// same as your example    
    PdfPCell QuestionNrCell = _defaultCell();
    QuestionNrCell.AddElement(new Paragraph(string.Format("{0}. ", i)));
    PdfPCell QuestionPhraseCell = _defaultCell();
    QuestionPhraseCell.AddElement(new Paragraph(string.Format("{0}", s)));

    Image itextImage = Image.GetInstance(imagePath);
// second parameter used so image is NOT scaled
    PdfPCell imageCell = new PdfPCell(itextImage, false) {
      Border = Rectangle.NO_BORDER,
      Colspan = 2, PaddingLeft = indentionForQuestionInfo
    };
    questionTable.AddCell(QuestionNrCell);
    questionTable.AddCell(QuestionPhraseCell);
    questionTable.AddCell(imageCell);
    document.Add(questionTable);
  }