I have some trouble to get this code working. The goal is to merge pdf with a loaded pdf in a PDDocument object. I don't want to use the mergeUtility of PdfBox because it implies to closed the PDDocument object. I have a lot of data to process and I use a loop to process it. Load and close a PDDocument will take too much time and resource (maybe I'm wrong but that the way it feel it).
Here is my way to do it :
for (String path:pathList) {
/* ... */
if(path.endsWith("pdf")){
File pdfToMerge = new File(path);
try(PDDocument pdfToMergeDocument = PDDocument.load(pdfToMerge)){
for (int pageIndex = 0; pageIndex < pdfToMergeDocument.getNumberOfPages(); pageIndex++){
PDPage page = pdfToMergeDocument.getPage(pageIndex);
doc.addPage(page);
}
}catch (IOException e){
System.out.println("Pdf : " + path + ANSI_RED + " [FAILED]" + ANSI_RESET);
continue;
}finally {
System.out.println("Pdf : " + path + ANSI_GREEN +" [OK]" + ANSI_RESET);
}
}
}
doc.save("src/Kairos/OutPut/"+pdfName[pdfName.length - 1]+".pdf");
doc.close();
The error happen when I try to save the document, on line 65.
I get this error message :
Exception in thread "main" java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
at org.apache.pdfbox.cos.COSStream.checkClosed(COSStream.java:83)
at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:133)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1214)
at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:402)
at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:521)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:459)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:443)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1108)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:449)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1381)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1268)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1334)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1305)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1293)
at Kairos.Main.main(Main.java:65)
doc
is declared at the begin of the file, it's created with the CreatePDFA class of apache's example. I check if the code work without this part and all is fine I get no error and I can save the document. The problem is really in this block. If you want I can edit my post to add the full code. – Hugo ChittaropdfToMergeDocument
only after savingdoc
. – Tilman Hausherrdoc
at the end of the first iteration. On the second iterationsave
fails becausedoc
is closed. – Federico klez CullocaPDDocument::addPage
method. It doesn't make it clear, but it does not make a copy. I'll amend my answer with a solution to this later. Thanks for your comments – Federico klez Culloca