I have the java code that takes pdf files from a directory copy pages to new pdfs and then saves it to anoter location. But it throws:
org.apache.pdfbox.cos.COSDocument finalize WARNING: Warning: You did not close a PDF Document
but I did close the pdf!!!
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException{
File srcFolder = new File(System.getProperty("user.dir") + "\\source");
File[] srcFiles = srcFolder.listFiles();
Arrays.sort(srcFiles);
for (File srcFile : srcFiles) {
PDDocument srcPDDocument = PDDocument.load(srcFile);
PDDocument destPDDOcument = new PDDocument();
Splitter splitting = new Splitter();
List<PDDocument> pages = splitting.split(srcPDDocument);
for (PDDocument page : pages) {
destPDDOcument.addPage(page.getPage(0));
}
String dest = System.getProperty("user.dir") + "\\dest\\" + srcFile.getName();
destPDDOcument.save(dest);
destPDDOcument.close();
srcPDDocument.close();
System.out.println(srcFile.getName());
}
}
}
You need to close the intermediate documents you get from the splitter (that are confusingly named pages
). Do this AFTER saving destPDDOcument
. So add code like this:
for (PDDocument doc : pages)
{
doc.close();
}