java - 使用 itext 在一个单元格中的同一行中显示图像和文本

标签 java itext

我想仅使用一个单元格在同一行中添加图像和文本。我可以在一个单元格中添加图像和文本,但图像出现在新行中,而不是在文本旁边

最佳答案

引用 iText example :

public static void main(String[] args) throws Exception {
    String dest = "sample.pdf";     
    String img1 = "sample.jpg";

    File file = new File(dest);
    file.getParentFile().mkdirs();

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdfDoc);
    Table table = new Table(UnitValue.createPercentArray(new float[]{1, 2}));
    table.addCell(createImageCell(img1));
    table.addCell(createTextCell("Some Text"));
    doc.add(table);

    doc.close();
}

public static Cell createImageCell(String path) throws MalformedURLException {
    Image img = new Image(ImageDataFactory.create(path));
    img.setWidth(UnitValue.createPercentValue(100));
    Cell cell = new Cell().add(img);
    cell.setBorder(null);
    return cell;
}

public static Cell createTextCell(String text) {
    Cell cell = new Cell();
    Paragraph p = new Paragraph(text);
    p.setTextAlignment(TextAlignment.RIGHT);
    cell.add(p).setVerticalAlignment(VerticalAlignment.BOTTOM);
    cell.setBorder(Border.NO_BORDER);
    return cell;
}

关于java - 使用 itext 在一个单元格中的同一行中显示图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138325/

相关文章:

java - 带有 base64 图像的 HTML 到 PDF 抛出 FileNotFoundException

java - 如何将 XML 输出保存为 PDF

java - 如何在Java中将ppt文件转换成pdf文件?

java - 从 OpenPDF 获取溢出文本

java - 什么是好的 "Error Checking"模式(Java)?

java - java RMI 服务中的通用事务逻辑?

java - 如何在 android 中获得接下来的 7 天?

java - 计算字符 'e'

java - grails: try 抛出意外的标记: try:

c# - 无法为图像 itextsharp 设置固定大小