使用iText将行添加到PDF表格中 [英] adding row to a table in PDF using iText
问题描述
我在这段代码中得到的是一个包含8列和3行的表。
我该怎么办才能获得2行?并且3行中的第1列是空的,但剩余的单元格用hi填充。
What am I getting in this code is a table with 8 columns and 3 rows. 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 ,用于完整的示例代码和生成的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屋!