在pdf文档中没有顺序添加图像itextsharp(元素顺序错误) [英] Image not sequentialy added in pdf document itextsharp (wrong order of elements)

查看:467
本文介绍了在pdf文档中没有顺序添加图像itextsharp(元素顺序错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与iTextSharp(5.4.5)合作几周。
本周,我遇到了一些奇怪的事情,它涉及到文件中元素的顺序。

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.

我正在制作包含主题和图像的pdf报告(图表)。

I am working on a pdf report that contains topics and images (charts).

文档格式如下:

NR。主题1的主题标题

NR. TOPIC TITLE FOR TOPIC 1

主题1的图表图像(来自bytearray)

CHART IMAGE for topic 1 (from bytearray)

NR。主题2的主题标题

NR. TOPIC TITLE FOR TOPIC 2

主题2的图表图像...

CHART IMAGE for topic 2 ...

以下是代码示例。我知道代码不完全正确,但它只是指出问题。
让我们假设循环运行10次,所以我希望10个主题标题都直接跟随图像。

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.

所以在纸上我们有:

第1页:

主题1
图片主题1

topic 1 image topic 1

主题2
图像主题2

topic 2 image topic 2

主题3
主题4

topic 3 topic 4

第2页:

图片主题3
图片主题4

image topic 3 image topic 4

主题5
image topic 5

topic 5 image topic 5

...

所以纸上元素的顺序,与通过Document.add方法将元素放在文档中的顺序不一样。

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


推荐答案

如果您有 PdfWriter 实例(例如 writer ),您需要强制iText使用严格的图像序列,如下所示:

If you have a PdfWriter instance (for instance writer), you need to force iText to use strict image sequence like this:

writer.setStrictImageSequence(true);

否则,iText会推迟添加图片,直到页面上有足够的空间来添加图片。

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

这篇关于在pdf文档中没有顺序添加图像itextsharp(元素顺序错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆