Crash on line: PDDocument.load(file)

问题描述 投票:0回答:1

I have the following simplified code:

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();

The Problem

I get the following error: java.io.IOException: Error: End-of-File, expected line on the line:

doc = PDDocument.load(textFile); in the try block.


What I've Tried

I've tried these solutions but none have worked:


预期结果

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

java spring-boot file loading pdfbox
1个回答
2
投票

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

参见PDDocument的javadoc。https:/pdfbox.apache.orgdocs2.0.2javadocsorgapachepdfboxpdmodelPDDocument.html#load(java.io.File)

© www.soinside.com 2019 - 2024. All rights reserved.