如何在iText中添加无边框表

时间:2018-01-12 09:51:38

标签: java itext

我想添加两个字符串来对齐同一行的两边。对于这个任务我正在使用一个表。我希望这张桌子不含边框。我尝试了一些例子,但没有任何效果。

这是当前的代码:

    public void footer(Document document, String date) throws DocumentException {

    document.add(new Paragraph("footer"));
    Paragraph paragraph = new Paragraph(date);
    paragraph.setAlignment(Element.ALIGN_RIGHT);
    Paragraph paragraph2 = new Paragraph("ABC Holdings (pvt) Ltd");


    PdfPTable table = new PdfPTable(2);
    PdfPCell cellOne = new PdfPCell(new Phrase(paragraph));
    PdfPCell cellTwo = new PdfPCell(new Phrase(paragraph2));
    table.getDefaultCell().setBorder(0);

    table.addCell(cellOne);
    table.addCell(cellTwo);

    document.add(table);

}

1 个答案:

答案 0 :(得分:2)

好的,我解决了。 iText提供了操作表格的方法。

    public void footer(Document document, String date) throws DocumentException {

    document.add(new Paragraph("footer"));
    Paragraph paragraph = new Paragraph(date);
    paragraph.setAlignment(Element.ALIGN_RIGHT);
    Paragraph paragraph2 = new Paragraph("ABC Holdings (pvt) Ltd");


    PdfPTable table = new PdfPTable(2);
    PdfPCell cellOne = new PdfPCell(new Phrase(paragraph));
    PdfPCell cellTwo = new PdfPCell(new Phrase(paragraph2));

    cellOne.setBorder(Rectangle.NO_BORDER);
    cellTwo.setBorder(Rectangle.NO_BORDER);
    cellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
    cellTwo.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.setWidthPercentage(100);

    table.addCell(cellOne);
    table.addCell(cellTwo);

    document.add(table);

}