我想添加两个字符串来对齐同一行的两侧。对于这项任务,我使用一张 table 。我希望这张 table 是无边框的。我尝试了几个例子,但没有任何效果。
这是当前代码:
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);
}
最佳答案
好的,我解决了。 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);
}
关于java - 如何在 iText 中添加无边框表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223594/