What I'm 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".
Code:
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");
}
解决方案
Try this:
PdfPTable table = new PdfPTable(8);
PdfPCell cell;
for(int aw=0;aw<8;aw++)
{
cell = new PdfPCell(new Paragraph("hi"));
table.addCell(cell );
}
EDIT:
// 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();
See SimpleTable for the full sample code and the resulting PDF:
As you can see in the screen shot, the table has 8 columns and 2 rows (as expected).
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");
}
Now the result looks like this:
Again 8 columns and two rows, but now the cell in the first column spans two rows.