Apache PDFBox 2.0 - 创建的 PDF 文件中未显示文本

Apache PDFBox 2.0 - Text is not shown in created PDF file

我正在使用以下方法创建 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 底部)