首页 > 解决方案 > PDFBox | 在线崩溃: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块中。


我试过的

我已经尝试了这些解决方案,但都没有奏效:


预期成绩

我想正确加载文本文件并使用 PDFBox 将其显示为 PDF。

标签: javaspring-bootfileloadingpdfbox

解决方案


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)


推荐阅读