Java error close PDF

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 it away:

 11:45:07,017 WARN [COSDocument] Warning: You did not close a PDF Document 

And I don’t know why, but throw it 1, 2, 3 or more.

I find that COSDocument is a class and has a close () method, but I don't use this class anywhere.

I have this import:

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

Thanks:)

+6
java pdf pdfbox
source share
4 answers

The PDDocument , but does not close. I suspect you need to do:

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

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

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

Since PDDocument implements Closeable , the try block will automatically execute its close() method at the end.

+7
source share

This warning is issued when the PDF has been completed and has not 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 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 must explicitly call close in the document when you're done with it.

+4
source share

The PDDocument resource PDDocument does not implement java.lang.AutoCloseable so we cannot try to use the resource for the current script.

0
source share

All Articles