【问题标题】:Java iText: text below imageJava iText:图像下方的文本
【发布时间】:2013-06-18 17:09:20
【问题描述】:

我想在 PdfPCell 中放置多个图像,并在每个图像下方放置一个文本。 我试过这段代码:

   private PdfPTable tabellaRighe() throws BadElementException, MalformedURLException, IOException, DocumentException {
    int[] cellWidth = {500, 95};
    PdfPTable table = new PdfPTable(2);
    table.setWidths(cellWidth);
    table.setTotalWidth(PageSize.A4.getWidth() - 45);
    table.setLockedWidth(true);
    PdfPCell cell;
    cell = new PdfPCell();
    cell.setBorderWidth(0);
    Paragraph p = new Paragraph();
    for (int i = 0; i < 4; i++) {
        Image image = Image.getInstance(imgNd);
        image.scaleToFit(300, 135);
        Phrase ph = new Phrase();
        ph.add(new Chunk(image, 0, 0, true));
        ph.add("CIAO");
        p.add(ph);
    }
    cell.addElement(p);
    table.addCell(cell);
    cell = new PdfPCell();
    cell.setBorderWidthBottom(1);
    cell.setBorderWidthLeft(1);
    cell.setBorderWidthRight(1);
    cell.setBorderWidthTop(1);
    table.addCell(cell);
  }

但文字不在图像下方,而是向右移动。 如何将文字放在每张图片下方?

【问题讨论】:

    标签: java itext pdfptable


    【解决方案1】:

    在文本前后添加\n

    ph.add("\nCIAO\n");
    

    \n 产生新的一行。

    【讨论】:

    • 同时,感谢您的回复。问题是图像必须是内联的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2012-10-12
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多