PdfPTable / PdfPCell 中的 Colspan 调整

Colspan adjustment in a PdfPTable / PdfPCell

我正在尝试使用 iText PdfPTable :

创建类似的东西
--------------------------------------------------------------
|                         Content 1                          |
--------------------------------------------------------------
|              Content 2          |  Content 3  | Content 4  |
--------------------------------------------------------------
|                         Content 5                          |
--------------------------------------------------------------
|           Content 7           |       Content 7            | 
-------------------------------------------------------------- 

更新:

我曾尝试创建类似的东西但失败了,这表明我确实需要学习 more.Could 有人建议我如何构建相同的东西并帮助我学习。任何建议表示赞赏。

谢谢

你需要的比你用来激发灵感的例子要简单得多。为每一行添加注释行是一种很好的做法,这样您就可以轻松地进行数学运算来计算所需的 colspans:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    // I see 3 columns in your example
    PdfPTable table = new PdfPTable(3);
    // The first column appears to have double the width of the other columns
    table.setWidths(new int[]{ 2, 1, 1});
    // the first row consists of 1 cell that spans the 3 columns
    PdfPCell c1 = new PdfPCell(new Phrase("Content 1"));
    c1.setColspan(3);
    table.addCell(c1);
    // Then follows a row with normal cells
    table.addCell("Content 2");
    table.addCell("Content 3");
    table.addCell("Content 4");
    // Again we have a row with 1 cell that spans 3 columns
    PdfPCell c5 = new PdfPCell(new Phrase("Content 5"));
    c5.setColspan(3);
    table.addCell(c5);
    // Now we have a row with 1 normal cell and 1 that spans 2 columns
    table.addCell("Content 7.1");
    PdfPCell c7 = new PdfPCell(new Phrase("Content 7.2"));
    c7.setRowspan(2);
    table.addCell(c7);
    // now we can add the table
    document.add(table);
    document.close();
}