简体   繁体   中英

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

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:


Expected Results

I want to load the text file without error and display it as a PDF with PDFBox.

PDDocument.load expects a pdf file, not a txt file.

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

In PDFBox 3.0, you have to use org.apache.pdfbox.Loader.loadPDF(new File(...))

For more information, check https://pdfbox.apache.org/3.0/migration.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM