我使用以下方法创建 PDF 文件:
private void createPdf() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(new PDPage());
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(PDType1Font.HELVETICA, 26);
content.showText("Example Text");
content.endText();
content.close();
doc.save("report.pdf");
doc.close();
}
它创建一个带有白页的新文件,但不显示任何文本。怎么了?
我使用 Apache PDFBox 2.0.7。
最佳答案
更改此代码
PDPage page = new PDPage();
doc.addPage(new PDPage());
到此
PDPage page = new PDPage();
doc.addPage(page);
您错误地添加了空无一物的新页面。您所做的操作是在另一个对象上完成的。
您的文本现在应该在页面底部可见。 (y = 0 是 PDF 中的底部)
关于java - Apache PDFBox 2.0 - 创建的 PDF 文件中未显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522010/