需要在表中创建多个行,并使用itext将其放在PDF的页面中间

时间:2014-06-23 04:37:59

标签: java itext

Document document = new Document();
PdfPTable table = new PdfPTable(8);
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));
PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 6"));
PdfPCell cell7 = new PdfPCell(new Paragraph("Cell 7"));
PdfPCell cell8 = new PdfPCell(new Paragraph("Cell 8"));
document.add(table);

此代码添加一个包含8个单元格的表。我需要的是,同一个表中的另一行。我该怎么办..?

1 个答案:

答案 0 :(得分:1)

Document document = new Document();
PdfPTable table = new PdfPTable(8); <-- This is a constructor. Above 8 cells, would automatically move to a new row.
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 5"));
PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 6"));
PdfPCell cell7 = new PdfPCell(new Paragraph("Cell 7"));
PdfPCell cell8 = new PdfPCell(new Paragraph("Cell 8"));

cell1 = new PdfPCell(new Paragraph("Cell 1"));
cell2 = new PdfPCell(new Paragraph("Cell 2"));
cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell4 = new PdfPCell(new Paragraph("Cell 4"));
cell5 = new PdfPCell(new Paragraph("Cell 5"));
cell6 = new PdfPCell(new Paragraph("Cell 6"));
cell7 = new PdfPCell(new Paragraph("Cell 7"));
cell8 = new PdfPCell(new Paragraph("Cell 8"));
document.add(table);

//样本/示例:

    // a table with three columns
    PdfPTable table = new PdfPTable(3);
    // the cell object
    PdfPCell cell;
    // we add a cell with colspan 3
    cell = new PdfPCell(new Phrase("Cell with colspan 3"));
    cell.setColspan(3);
    table.addCell(cell);
    // now we add a cell with rowspan 2
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
    cell.setRowspan(2);
    table.addCell(cell);
    // we add the four remaining cells with addCell()
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");