java - 在 PDF 表格中引入跨两行的单元格

标签 java itext

我正在使用iText(Java版本)创建PDF文档,如下图所示:

enter image description here

我想创建如突出显示部分所示的内容。我完成了 PDF 的所有其他部分的开发,除了突出显示的部分。

参见:

enter image description here

最佳答案

您可以通过在 iText 中正确使用 Colspan 和 rowspan 来实现此目的。 下面是一个示例:

https://developers.itextpdf.com/examples/tables/colspan-and-rowspan

我为我认为您会遇到问题的部分添加了一个小代码块:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(3);
    table.setWidths(new int[]{ 1, 1, 1});
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("8"));
    cell.setColspan(2);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("10"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("15"));
    cell.setColspan(1);
    cell.setRowspan(2);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("16"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("17"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("24"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("25"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("mm"));
    cell.setColspan(2);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("mm"));
    cell.setColspan(1);
    table.addCell(cell);
    document.add(table);
    document.close();
}

生成的 pdf 如下所示: enter image description here

我使用的是iText版本5.0.6

关于java - 在 PDF 表格中引入跨两行的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47034896/

相关文章:

java - 如何使用 JNA 创建同一个库的多个实例?

java - 如何在方法调用期间使用同一类的两个对象?

java - 我无法使用 keytool 获取 SHA1 证书

java - 如何使用 iText 1.5.4 添加多行页脚?

itext - iText(任何版本)是否适用于 Windows Azure 网站?

java - 如何在 onCreate 中使用 glide 获取位图?

java - 是否可能/需要在同一条目上加速 HashMap 操作?

java - 在浏览器中编辑PDF并将其保存到服务器Java

java - 类 "org.bouncycaSTLe.asn1.ASN1ObjectIdentifier"的签名者信息与同一包中其他类的签名者信息不匹配

android - 如何在 android 中将图像转换为 pdf?