java - 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。
解决方案
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)
推荐阅读
- python - 按日期分组以获取一天、过去 7 天和一个月的点击次数
- mysql - #1072 - 表中不存在键列“国家”
- stream - 在 Modelica 模型中使用流变量时如何处理奇点?
- c# - 要列出的 C# 数据列
- android - Buildozer 编译错误:没有为 hostpython2 找到匹配的发行版(来自 -r requirements.txt(第 2 行))
- java - 如何使用 OpenPDF 添加目录
- angular - 如何让属性文件控制整个应用程序并实现服务器条件
- python - Pyplot 将 X 比例轴除以数字
- json - Wiremock BodyPattern 匹配包含这个或那个
- python - 如何拆分 JSON 列