Java关闭PDF错误

发布于 2024-10-16 20:33:03 字数 744 浏览 3 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖公主 2024-10-23 20:33:03

您正在加载 PDDocument 但未关闭它。我怀疑你需要这样做:

String textOfPdf;
PDDocument doc = PDDocument.load("doc");
try {
    textOfPdf = pdfs.getText(doc);
} finally {
    doc.close();
}

You're loading a PDDocument but not closing it. I suspect you need to do:

String textOfPdf;
PDDocument doc = PDDocument.load("doc");
try {
    textOfPdf = pdfs.getText(doc);
} finally {
    doc.close();
}
颜漓半夏 2024-10-23 20:33:03

刚刚也遇到这个问题了使用 Java 7,您可以执行以下操作:

try(PDDocument document = PDDocument.load(input)) {
  // do something  
} catch (IOException e) {
  e.printStackTrace();
}

由于 PDDocument 实现 Closeable,因此 try 块将在最后自动调用其 close() 方法。

Just had this issue, too. With Java 7 you can do this:

try(PDDocument document = PDDocument.load(input)) {
  // do something  
} catch (IOException e) {
  e.printStackTrace();
}

Because PDDocument implements Closeable, the try block will automagically call its close() method at the end.

抚你发端 2024-10-23 20:33:03

当 pdf 文档完成且尚未关闭时,会发出此警告。

这是来自 COSDocument

/**
 * Warn the user in the finalizer if he didn't close the PDF document. The method also
 * closes the document just in case, to avoid abandoned temporary files. It's still a good
 * idea for the user to close the PDF document at the earliest possible to conserve resources.
 * @throws IOException if an error occurs while closing the temporary files
 */
protected void finalize() throws IOException
{
    if (!closed) {
        if (warnMissingClose) {
            log.warn( "Warning: You did not close a PDF Document" );
        }
        close();
    }
}

要消除此警告,您应该在使用完文档后显式调用 close

This warning is emitted when the pdf document is finalised and hasn't been closed.

Here is the finalize method from COSDocument:

/**
 * Warn the user in the finalizer if he didn't close the PDF document. The method also
 * closes the document just in case, to avoid abandoned temporary files. It's still a good
 * idea for the user to close the PDF document at the earliest possible to conserve resources.
 * @throws IOException if an error occurs while closing the temporary files
 */
protected void finalize() throws IOException
{
    if (!closed) {
        if (warnMissingClose) {
            log.warn( "Warning: You did not close a PDF Document" );
        }
        close();
    }
}

To get rid of this warning, you should explicitly call close on the document when you are done with it.

始于初秋 2024-10-23 20:33:03

对于任何使用 Kotlin 的人来说,扩展函数 use 会为你处理任何 Closable:

val textOfPdf = PDDocument.load("doc").use { pdfs.getText(it) }

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:

val textOfPdf = PDDocument.load("doc").use { pdfs.getText(it) }

cf. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文