Java关闭PDF错误
我有这个 java 代码:
try {
PDFTextStripper pdfs = new PDFTextStripper();
String textOfPDF = pdfs.getText(PDDocument.load("doc"));
doc.add(new Field(campo.getDestino(),
textOfPDF,
Field.Store.NO,
Field.Index.ANALYZED));
} catch (Exception exep) {
System.out.println(exep);
System.out.println("PDF fail");
}
并抛出这个:
11:45:07,017 WARN [COSDocument] Warning: You did not close a PDF Document
我不知道为什么但抛出这个 1、2、3 或更多。
我发现 COSDocument 是一个类并且有 close() 方法,但我没有在任何地方使用这个类。
我有这个进口:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
谢谢:)
I have this java code:
try {
PDFTextStripper pdfs = new PDFTextStripper();
String textOfPDF = pdfs.getText(PDDocument.load("doc"));
doc.add(new Field(campo.getDestino(),
textOfPDF,
Field.Store.NO,
Field.Index.ANALYZED));
} catch (Exception exep) {
System.out.println(exep);
System.out.println("PDF fail");
}
And throws this:
11:45:07,017 WARN [COSDocument] Warning: You did not close a PDF Document
And I don't know why but throw this 1, 2, 3, or more.
I find that COSDocument is a class and have close() method, but I don't use this class nowhere.
I have this imports:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在加载
PDDocument
但未关闭它。我怀疑你需要这样做:You're loading a
PDDocument
but not closing it. I suspect you need to do:刚刚也遇到这个问题了使用 Java 7,您可以执行以下操作:
由于
PDDocument 实现 Closeable
,因此try
块将在最后自动调用其close()
方法。Just had this issue, too. With Java 7 you can do this:
Because
PDDocument implements Closeable
, thetry
block will automagically call itsclose()
method at the end.当 pdf 文档完成且尚未关闭时,会发出此警告。
这是来自 COSDocument:
要消除此警告,您应该在使用完文档后显式调用
close
。This warning is emitted when the pdf document is finalised and hasn't been closed.
Here is the
finalize
method from COSDocument:To get rid of this warning, you should explicitly call
close
on the document when you are done with it.对于任何使用 Kotlin 的人来说,扩展函数 use 会为你处理任何 Closable:
cf. https://kotlinlang.org/api/latest/jvm/ stdlib/kotlin.io/use.html
For anybody using Kotlin, the extension function use takes care of any Closable for you:
cf. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html