【问题标题】:Image not sequentialy added in pdf document itextsharp (wrong order of elements)pdf文档itextsharp中未按顺序添加图像(元素顺序错误)
【发布时间】:2014-03-31 15:22:05
【问题描述】:

我现在正在使用 iTextSharp (5.4.5) 几个星期。 这周,我在文档中的元素顺序方面遇到了一些奇怪的事情。

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

文档格式如下:

NR。主题 1 的主题标题

主题 1 的图表图像(来自字节数组)

NR。主题 2 的主题标题

主题 2 的图表图像 ...

以下是代码示例。我知道代码并不完全正确,但这只是指出问题所在。 假设循环运行 10 次,所以我预计 10 个主题标题都直接跟在图片后面。

我注意到的是,如果到达页面末尾并应添加新的图像,则图像将移动到下一页,并且下一个主题标题将打印在前一页上。

所以在纸面上我们有:

第 1 页:

主题 1 图片主题1

主题 2 图片主题2

主题 3 话题四

第 2 页:

图片主题 3 图片主题4

主题 5 图片主题5

...

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

这真的很奇怪。有人知道吗?

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

【问题讨论】:

    标签: itextsharp itext


    【解决方案1】:

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

    writer.setStrictImageSequence(true);
    

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

    【讨论】:

    • 就是这样!谢谢布鲁诺
    【解决方案2】:

    这是相当不直观...我得到相同的行为。那么为什么不在每次迭代时向表中添加另一行:

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

    【讨论】:

    • 您好 Kuujinbo,我最初是这样实现的,但是如果我使用表格来格式化所有内容,图像将添加到表格单元格中(而不是使用段落,我可以添加新行)。但如果我这样做,如果到达页面末尾,图像将完全缩放(缩小)以适合页面。这是不受欢迎的行为,因为图像完全不可读(图像是显示分析数据的图表)。
    • @user3149519 - 默认情况下,添加到 PdfPCell 的图像 会调整大小,但 if 您使用正确的 PdfPCell 构造函数,如代码示例图像将调整大小。也许你将来可以使用的东西。
    猜你喜欢
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多