Hi,
Alan Thomas schrieb:
I have a question about loading a PDDocument in a loop.
If I have a statement like this:
InputDoc2 = PDDocument.load(File2);
in a loop that loads different files (values for File2) each loop,
and then wait until the loop ends before calling a .close(),
am I leaving files open, or is this OK?
All files should be closed.
I am trying to avoid problems getting Cos Invalid Handle exceptions,
which I get when I close an input file before the output file that uses
its resources.
You have to close the destination file first. After that it is save to close
all input files you have used to create the output file.
In my application, I have to open multiple input files for every output
file,
to copy their pages to the output file.
A more complete file fragment may be found below. Please note that
the copyPage() method is one of my own, and it uses .importPage(),
not the copy method from PDFBox.
Probably you have to clone some of the contents instead of just copying the
reference to it. Have a look at the PDFMergerUtility [1] as an example
for merging pdfs. You should also have a look at PDFBOX-515 [2] as a link
to the solved issues we had in earlier versions.
BR
Andreas Lehmkühler
[1]
http://svn.apache.org/repos/asf/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java
[2] https://issues.apache.org/jira/browse/PDFBOX-515