我正在阅读 pdf 文件中的一些文本
try {
File pdfFile = new File("ffile.pdf");
PDFParser parser = new PDFParser(new FileInputStream(pdfFile));
parser.parse();
COSDocument cosDoc = parser.getDocument();
PDFTextStripper pdfStripper = new PDFTextStripper();
PDDocument pdDoc = new PDDocument(cosDoc);
//do sth
} catch (Exception e) {
System.err.println("An exception occured in parsing the PDF Document."
+ e.getMessage());
}
有时我会收到此错误:
WARNING [Finalizer] org.apache.pdfbox.cos.COSDocument.finalize Warning: You did not close a PDF Document
我在这里读到了一个类似的问题,它说我必须关闭打开的文件。所以,我添加了
finally{
pdfFile.close(); //<-
}
但是 Netbeans 将其标记为错误 close()
,表示找不到符号。
那么我必须关闭什么?我还尝试了 parser.close()
但此行也标记有来自 Netbeans 的错误。
最佳答案
您正在使用过时的方式打开文件。这是正确的方法(省略异常处理):
File pdfFile = new File("ffile.pdf");
PDDocument pdDoc = PDDocument.load(pdfFile);
PDFTextStripper pdfStripper = new PDFTextStripper();
//....
pdDoc.close();
关于java - 从 java 读取 PDF 文件时关闭它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35697185/