如何在 iText 中并排显示两个 PDFTable

How to show two PDFTables next to each other in iText

我需要你的帮助才能在 iText 中并排显示两个 PDFTable。现在第一个 table 在第二个 table 之上,现在我需要用一个短的 space 让它们彼此相邻。这是我的代码:

//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);

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

    // 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);

使用

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

代替

doc.add(Chunk.NEWLINE);

绝对适合您。 :)