【问题标题】:Apache PDFBox 2.0 - Text is not shown in created PDF fileApache PDFBox 2.0 - 创建的 PDF 文件中不显示文本
【发布时间】:2017-08-05 12:39:59
【问题描述】:

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

【问题讨论】:

  • 你看过页面底部了吗?
  • Vega,@Tilman 暗示的是您没有为文本指明任何位置。因此,它在 (0,0) 处绘制,如果您的代码位于页面的左下角。
  • 我仔细检查过,但页面上什么也没有。感谢您的提示。还有其他想法吗?

标签: java pdfbox


【解决方案1】:

更改此代码

PDPage page = new PDPage();
doc.addPage(new PDPage());

到这里

PDPage page = new PDPage();
doc.addPage(page);

您错误地添加了一个没有任何内容的新页面。您所做的操作是在另一个对象上完成的。

您的文字现在应该在页面底部可见。 (y = 0 在 PDF 中是底部)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2014-07-11
    • 2021-01-05
    • 2013-03-22
    • 2011-10-30
    • 1970-01-01
    相关资源
    最近更新 更多