我有以下简化代码:
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格式。
发布于 2020-05-06 19:38:11
PDDocument.load需要一个pdf文件,而不是txt文件。
参见PDDocument:https://pdfbox.apache.org/docs/2.0.2/javadocs/org/apache/pdfbox/pdmodel/PDDocument.html#load(java.io.File)的javadoc
发布于 2021-11-18 07:46:11
在PDFBox 3.0中,您必须使用org.apache.pdfbox.Loader.loadPDF(new File(...))
https://stackoverflow.com/questions/61643627
复制相似问题