1
votes

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.

1
What do you want to do? PDDocument.load expects a pdf file, not a txt file. See javadoc pdfbox.apache.org/docs/2.0.2/javadocs/org/apache/pdfbox/pdmodel/… - TomStroemer
@TomStroemer Well well, thanks for that. I was under the assumption that it could load text files. Fixing now... - Compiler v2
@TomStroemer It worked! Thank you! Please add that as an answer to my question and I will give you the rep. - Compiler v2

1 Answers