【问题标题】:need to create multiple rows in a table and place it in a middle of the page in PDF using itext [closed]需要在表格中创建多行并使用 itext 将其放置在 PDF 的页面中间 [关闭]
【发布时间】:2014-06-23 04:37:58
【问题描述】:
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 个单元格的表格。我需要的是,同一张表中的另一行。我该怎么办..?

【问题讨论】:

标签: java itext


【解决方案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");

【讨论】:

  • 我需要一个 2 行 8 列的简单表格。你展示的有点复杂..
  • @Srinivasan:第一部分非常简单。不能再简单了!!!第二组代码供大家理解PDF Features。
  • 第一组代码将 2 行添加到表中..??你能解释一下如何..?? bcoz它没有工作..!!
  • 你必须添加 table.addCell(cell);在每一行之后......这就是为什么,我给了你第二组代码。希望它有帮助并且现在有效!
  • 好的...谢谢4你的解释..
猜你喜欢
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
相关资源
最近更新 更多