我有这个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是一个类并且有关闭()方法,但我不使用这个课程。

我有这个导入:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
.

谢谢:)

有帮助吗?

解决方案

您正在加载一个生成的gentacicetagcode但不关闭它。我怀疑你需要做:

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

其他提示

刚刚出现了这个问题。使用Java 7您可以执行以下操作:

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

因为世代odicetagcode,PDDocument implements Closeable块将在末尾自动调用其try方法。

当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();
    }
}
. 要摆脱此警告,您应该在与之完成时明确地调用文档上的生成finalize

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top