gpt4 book ai didi

java - 使用 iText 在 PDF 表格中添加一行

转载 作者:行者123 更新时间:2023-12-01 17:09:22 26 4
gpt4 key购买 nike

我在这段代码中得到的是一个 8 列 3 行的表格。

我应该怎么做才能只得到两行? 3 行中的第一列是空的,但其余单元格填充有“hi”。

代码:

<小时/>
PdfPTable table = new PdfPTable(8);

PdfPCell cell;

cell = new PdfPCell();
cell.setRowspan(2);
table.addCell(cell);

for(int aw=0;aw<8;aw++){
table.addCell("hi");
}

最佳答案

试试这个:

PdfPTable table = new PdfPTable(8);

PdfPCell cell;
for(int aw=0;aw<8;aw++)
{
cell = new PdfPCell(new Paragraph("hi"));
table.addCell(cell );
}

编辑:

    // Step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4

PdfPTable table = new PdfPTable(8);
for(int aw=0;aw<16 ; aw++){
table.addCell("hi");
}



// Step 5
document.add(table);
// step 6
document.close();

参见SimpleTable完整的示例代码和 resulting PDF :

enter image description here

正如您在屏幕截图中看到的,该表有 8 列和 2 行(如预期)。

阅读原始问题,我发现第一列有一个 colspan 2 的单元格。考虑到这一点,这只是一个小小的更改:

PdfPTable table = new PdfPTable(8);
PdfPCell cell = new PdfPCell(new Phrase("hi"));
cell.setRowspan(2);
table.addCell(cell);
for(int aw = 0; aw < 14; aw++){
table.addCell("hi");
}

现在结果如下所示:

enter image description here

同样是 8 列和 2 行,但现在第一列中的单元格跨越两行。

关于java - 使用 iText 在 PDF 表格中添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24359321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com