Can anyone help me out with this question?
--------------------------------------------------
From: "Alan Thomas" <jalantho...@verizon.net>
Sent: Friday, April 16, 2010 10:11 PM
To: <users@pdfbox.apache.org>
Subject: Question about Reloading File (help!)

   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?

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.

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.

I can send the complete code, but I do not know if attachments
are OK for the mailing list.

If anyone can explain this better to me, I would appreciate it.

Thanks, Alan

           // Load first input file
           InputDoc1 = PDDocument.load(InFile1Name);

           // Create output PDF document
           OutputDoc = new PDDocument();

           // Get a list of pages from the input PDF document
           List pages = InputDoc1.getDocumentCatalog().getAllPages();

           // Process each page
            for (Object obj : pages)
           {
               PDPage page = (PDPage)obj;
. . .

               // If no content was found, add the original page
               // to the output PDF document
               if (Content == null)
               {
                   copyPage(OutputDoc, page);
                   OutputDoc.save(OutPath);
               }
               else
               {
. . .
                   // Otherwise, get the content from a 2nd file
                   InputDoc2 = PDDocument.load(File2);
List pages2 = InputDoc2.getDocumentCatalog().getAllPages();
                   for (Object obj2 : pages2)
                   {
                       PDPage page2 = (PDPage)obj2;
                       copyPage(OutputDoc, page2);
                   }
               }
           }
           if (OutputDoc != null)
           {
               OutputDoc.save(OutPath);
               OutputDoc.close();
           }
           if( InputDoc2 != null )
               InputDoc2.close();

           if( InputDoc1 != null )
               InputDoc1.close();
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
   }

   private void copyPage( PDDocument doc, PDPage page) throws Exception
   {
       PDPage imported = doc.importPage(page);
       imported.setCropBox(page.findCropBox());
       imported.setMediaBox(page.findMediaBox());
       imported.setResources(page.findResources());
       imported.setRotation(page.findRotation());
   }




Reply via email to