使用iText在PDF中向表格添加行 [英] Adding a row to a table in PDF using iText
问题描述
我在这段代码中得到的是一个有8列3行的表.
What I'm getting in this code is a table with 8 columns and 3 rows.
我应该怎么做才能得到2行?并且三行中的第一列为空,但其余单元格都用"hi"填充.
What should I do to get just 2 rows? And the 1st column in the 3 rows are empty, but the remaining cells are filled with "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 ./itextpdf.com/sites/default/files/simple_table.pdf"rel =" nofollow noreferrer>生成PDF :
See SimpleTable for the full sample code and the resulting PDF:
如您在屏幕快照中所见,该表有8列和2行(按预期).
As you can see in the screen shot, the table has 8 columns and 2 rows (as expected).
在阅读原始问题时,我看到第一列有一个带有colspan 2的单元格.考虑到这一点,这只是一个很小的改变:
Reading the original question, I see that the first column has a cell with colspan 2. It's only a small change to take this into account:
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");
}
现在结果如下:
再次有8列和两行,但是现在第一列中的单元格跨越了两行.
Again 8 columns and two rows, but now the cell in the first column spans two rows.
这篇关于使用iText在PDF中向表格添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!