Java PdfPTable/PdfPCell中的Colspan调整

Java PdfPTable/PdfPCell中的Colspan调整,java,itext,Java,Itext,我正在尝试使用iText PdfPTable创建类似的内容: -------------------------------------------------------------- | Content 1 | -------------------------------------------------------------- | Content 2

我正在尝试使用
iText PdfPTable
创建类似的内容:

--------------------------------------------------------------
|                         Content 1                          |
--------------------------------------------------------------
|              Content 2          |  Content 3  | Content 4  |
--------------------------------------------------------------
|                         Content 5                          |
--------------------------------------------------------------
|           Content 7           |       Content 7            | 
-------------------------------------------------------------- 
更新:

我曾尝试创建一个类似的东西,但失败了,这表明我确实需要学习更多。有谁能建议我如何构建同样的东西,并帮助我学习。如有任何建议,我们将不胜感激


谢谢

你所需要的比你用来激发灵感的例子要简单得多。为每行添加注释行是一种很好的做法,这样您就可以轻松地计算所需的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();
}

你所需要的比你用来激发灵感的例子要简单得多。为每行添加注释行是一种很好的做法,这样您就可以轻松地计算所需的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();
}

它怎么不起作用了?它起作用了,但是。。我无法掌握创建iText PdfTable的想法,尤其是row和colspan之类的东西。你能帮我个忙吗?这个问题和你贴的完全不同。删除问题并询问新问题,或者编辑此问题以反映实际问题。请记住,如果你确切地问你刚才对我做了什么,它将(很可能)关闭,因为我正在尝试创建所提到的结构。但我无法理解在iText Pdf表中创建行跨度和列跨度所实现的概念。我写的代码是错误的,但这就是我所尝试的。所以,我请求帮助。你能建议我如何构造给定的表结构吗@再次,请看我的第一条评论。你得到的结构有什么问题?它怎么不起作用?它在起作用,但是。。我无法掌握创建iText PdfTable的想法,尤其是row和colspan之类的东西。你能帮我个忙吗?这个问题和你贴的完全不同。删除问题并询问新问题,或者编辑此问题以反映实际问题。请记住,如果你确切地问你刚才对我做了什么,它将(很可能)关闭,因为我正在尝试创建所提到的结构。但我无法理解在iText Pdf表中创建行跨度和列跨度所实现的概念。我写的代码是错误的,但这就是我所尝试的。所以,我请求帮助。你能建议我如何构造给定的表结构吗@再次,请看我的第一条评论。你得到的结构有什么问题吗?欣赏它。这不仅解决了所有问题,而且对跨越有了一个公平的想法。谢谢;-)很高兴这个例子有帮助。我的问题很琐碎,作为一个初学者,我不能做更多。但是有你这样的人在身边,我可以很容易地从一个业余选手提升到一个职业选手:-)。谢谢。这不仅解决了所有问题,而且对跨越有了一个公平的想法。谢谢;-)很高兴这个例子有帮助。我的问题很琐碎,作为一个初学者,我不能做更多。但是有你这样的人在身边,我可以很容易地从一个业余选手提升到一个职业选手:-)。