【问题标题】:itext pdf with list of image and text belowitext pdf,下面有图像和文本列表
【发布时间】:2015-12-16 03:26:42
【问题描述】:

需要帮助以生成包含图像列表和描述其下图像的文本的 pdf。

尝试了以下方法,但图像和文本并排放置。请在这方面需要帮助。谢谢。

........

PdfPTable table = new PdfPTable(1);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.setSplitRows(true);
table.setWidthPercentage(90f);

Paragraph paragraph = new Paragraph();
for (int counter = 0; counter < empSize; counter++) { 
    String imgPath = ... ".png");
    Image img = Image.getInstance(imgPath);
    img.scaleAbsolute(110f, 95f);

    Paragraph textParagraph = new Paragraph("Test" + counter));
    textParagraph.setLeading(Math.max(img.getScaledHeight(), img.getScaledHeight()));
    textParagraph.setAlignment(Element.ALIGN_CENTER);

        Phrase imageTextCollectionPhase = new Phrase();
    Phrase ph = new Phrase();
    ph.add(new Chunk(img, 0, 0, true));
        ph.add(textParagraph);  

    imageTextCollectionPhase.add(ph);
    paragraph.add(imageTextCollectionPhase);
}

PdfPCell cell = new PdfPCell(paragraph);
table.addCell(cell);
doc.add(table);

【问题讨论】:

  • 为什么要将所有内容添加到单个单元格中?如果只使用 ine 单元格,那么创建表格有什么意义?将您的图像和文本添加到单独的单元格中。

标签: pdf itext


【解决方案1】:

我假设您希望得到如下所示的结果:

在您的情况下,您将所有内容(所有图像和所有文本)添加到单个单元格中。您应该像 MultipleImagesInTable 示例中那样将它们添加到单独的单元格中:

public void createPdf(String dest) throws IOException, DocumentException {
    Image img1 = Image.getInstance(IMG1);
    Image img2 = Image.getInstance(IMG2);
    Image img3 = Image.getInstance(IMG3);
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(1);
    table.setWidthPercentage(20);
    table.addCell(img1);
    table.addCell("Brazil");
    table.addCell(img2);
    table.addCell("Dog");
    table.addCell(img3);
    table.addCell("Fox");
    document.add(table);
    document.close();
}

您可以轻松更改此概念证明,以便使用循环。只需确保将addCell() 方法inside 放在循环中,而不是outside 循环中。

您还可以显式创建PdfPCell 并将文本和图像组合在同一个单元格中,如下所示:

PdfPCell cell = new PdfPCell();
cell.addElement(img1);
cell.addElement(new Paragraph("Brazil"));
table.addCell(cell);

【讨论】:

  • 我从未见过任何人如此专注于他的项目,每一个 itextsharp 问题都有你的答案。非常感谢所有答案对我有很大帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 2010-12-20
  • 2017-12-19
  • 2016-06-30
  • 2014-05-12
  • 1970-01-01
相关资源
最近更新 更多