PDF框 |在线崩溃:PDDocument.load(文件)

PDFBox | Crash on line: PDDocument.load(file)

我有以下简化代码:

PDFTextStripper pdfStripper = new PDFTextStripper();
PDDocument doc;
String text = "";

try {
    File textFile = new File("C:/Users/user/Desktop/PDF-test.txt");
    doc = PDDocument.load(textFile);
    text = pdfStripper.getText(doc);
} finally {
   ...
}

...

PDPageContentStream content = new PDPageContentStream(doc, page);

content.setFont(font, 12);

content.beginText();
// Write to page using a text file
content.showText(text);
content.endText();
content.close();

问题

我收到以下错误:java.io.IOException: Error: End-of-File, expected line 行:

doc = PDDocument.load(textFile);try 块中。


我试过的

我试过这些解决方案,但 none 有效:


预期结果

我想无误地加载文本文件并使用 PDFBox 将其显示为 PDF。

PDDocument.load 需要 pdf 文件,而不是 txt 文件。

查看 PDDocument 的 javadoc:https://pdfbox.apache.org/docs/2.0.2/javadocs/org/apache/pdfbox/pdmodel/PDDocument.html#load(java.io.File)

在 PDFBox 3.0 中,您必须使用 org.apache.pdfbox.Loader.loadPDF(new File(...))

有关详细信息,请查看 https://pdfbox.apache.org/3.0/migration.html