我面临的问题是表格之间的间距不一致。我正在使用easytable(v0.8.4)和ApachePDFBox(v2.0.23)在pdf中绘制多个表。我希望在跨越多个页面的表之间有10f的一致空间差,但这并没有发生。有时我会得到一张间距正确的表格,有时会在新页面上绘制下一张表格,在pdf中留下两张表格之间的空白。请让我知道如何解决这个问题。以下是绘制表格的代码。以下是突出显示所面临问题的图像:
public ByteArrayInputStream createPDF(String fileName, List<PDFElementsDTO> pdfElementsDTOList) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
float startY = page.getMediaBox().getHeight() - 50f;
List<Table> tables = new ArrayList<>();
float[] columns = new float[1];
float width = page.getMediaBox().getWidth() - 2 * 50f;
Arrays.fill(columns, width / 1);
// table metadata
TableBuilder typeBuilder = Table.builder().addColumnsOfWidth(columns).fontSize(12).font(HELVETICA).width(width);
LOGGER.info("starting pdf generation process for : " + fileName);
typeBuilder.addRow(
Row.builder().add(TextCell.builder().text("PDF- Header").horizontalAlignment(CENTER).build())
.backgroundColor(Color.BLUE).textColor(Color.WHITE).font(HELVETICA_BOLD).fontSize(12)
.horizontalAlignment(CENTER).padding(5f).build());
tables.add(typeBuilder.build());
addContentToTheTable(page, pdfElementsDTOList, tables);
try (final PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
for (final Table table : tables) {
RepeatedHeaderTableDrawer.builder().page(page).contentStream(contentStream).table(table).startX(50f)
.startY(startY).endY(50f).build().draw(() -> document, () -> new PDPage(PDRectangle.A4), 50f);
startY -= (table.getHeight() + 10f);
if (startY < 0) {
startY = (page.getMediaBox().getHeight() - 50f) - ((table.getHeight() + 10f));
}
}
}
/*addHeader(document, fileNames);*/
document.save(out);
document.close();
return new ByteArrayInputStream(out.toByteArray());
}
我尝试做的第二件事是在pdf的每一页上添加页眉/页脚,但同样的内容也不可见。如果可能的话,我将非常感谢您的帮助。这是我试过的,但什么也没做。
private void addHeader(PDDocument document, String fileName) throws IOException {
for (int i = 0; i < document.getNumberOfPages(); i++) {
PDPage page = (PDPage) document.getPage(i);
PDRectangle pageSize = page.getMediaBox();
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true);
PDFont font = PDType1Font.TIMES_ROMAN;
float fontSize = 15.0f;
contentStream.beginText();
// set font and font size
contentStream.setFont( font, fontSize);
contentStream.newLineAtOffset(700, 1150);
contentStream.showText( "header text");
contentStream.endText();
contentStream.close();
document.save( "file path" );
}
}
暂无答案!
目前还没有任何答案,快来回答吧!