如何在iText中相互显示两个PDFTables

时间:2015-07-27 13:18:56

标签: java pdf-generation itext

我需要你的帮助才能在iText中相互显示两个PDFTable。现在第一个表位于第二个表的上方,现在我需要用短空格将它们彼此相邻。这是我的代码:

//First Table
dfPTable table = new PdfPTable(2);
Font earningsTitlefont = new Font(Font.TIMES_ROMAN,12, Font.BOLD);
PdfPCell c1 = new PdfPCell(new Phrase("Earnings Description",earningsTitlefont));
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earnings Amount",earningsTitlefont));
table.addCell(c1);

for (int i = 0; i < listEarnings.size(); i++) {
    String temp1 = listEarnings.get(i).getEarningsDescriptionSS();
    String temp2 = listEarnings.get(i).getEarningsAmountSS();

    table.addCell(temp1); 
    table.addCell(temp2);
}

//Second Table
dfPTable tableDeductions = new PdfPTable(2);
Font fontTitleDeductions = new Font(Font.TIMES_ROMAN,12, Font.BOLD);
PdfPCell c2= new PdfPCell(new Phrase("Deductions Description",fontTitleDeductions ));
tableDeductions.addCell(c2);
c2 = new PdfPCell(new Phrase("Deductions Amount",fontTitleDeductions));
tableDeductions.addCell(c2);

for (int i = 0; i < listDeductionss.size(); i++) {
    String temp3 = listDeductions.get(i).getDeductionssDescriptionSS();
    String temp4 = listDeductions.get(i).getDeductionssAmountSS();

    tableDeductions.addCell(temp3); 
    tableDeductions.addCell(temp4);
}
doc.add(table);
doc.add(Chunk.NEWLINE);
doc.add(tableDeductions);

2 个答案:

答案 0 :(得分:2)

(并排表)我不确定只是检查一下。

    // Main table
    PdfPTable mainTable = new PdfPTable(2);
    mainTable.setWidthPercentage(100.0f);

    // First table
    PdfPCell firstTableCell = new PdfPCell();
    firstTableCell.setBorder(PdfPCell.NO_BORDER);
    PdfPTable firstTable = new PdfPTable(2);
    //......... add some cells here ...........
    firstTableCell.addElement(firstTable);
    mainTable.addCell(firstTableCell);

    // Second table
    PdfPCell secondTableCell = new PdfPCell();
    secondTableCell.setBorder(PdfPCell.NO_BORDER);
    PdfPTable secondTable = new PdfPTable(2);
    //......... add some cells here ...........
    secondTableCell.addElement(secondTable);
    mainTable.addCell(secondTableCell);

    paragraph.add(mainTable);
    document.add(paragraph);

答案 1 :(得分:0)

使用

doc.add(new Phrase("\n"));

取代

doc.add(Chunk.NEWLINE);

肯定这对你有用。 :)