在Apache PDFBox中使用Boxable进行表格/行对齐需要注意以下几点:
com.github.dhorions
boxable
1.5
org.apache.pdfbox
pdfbox
2.0.24
Table table = new Table(numColumns, width, margin, pageSize.getWidth() - 2 * margin, pageSize.getHeight() - 2 * margin);
Row row = table.createRow(10);
Cell cell1 = row.createCell(20, "LeftAlignedText", HorizontalAlignment.LEFT, VerticalAlignment.MIDDLE);
Cell cell2 = row.createCell(20, "CenterAlignedText", HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE);
Cell cell3 = row.createCell(20, "RightAlignedText", HorizontalAlignment.RIGHT, VerticalAlignment.MIDDLE);
其中HorizontalAlignment和VerticalAlignment分别是水平和垂直对齐方式,可以是LEFT、CENTER、RIGHT、JUSTIFY、TOP、MIDDLE或BOTTOM。
PDPageContentStream contentStream = new PDPageContentStream(document, page);
table.draw();
contentStream.close();
完整代码示例:
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
Table table = new Table(3, 500, 50, page.getMediaBox().getWidth() - 100, page.getMediaBox().getHeight() - 100);
Row