You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Alan Thomas <ja...@verizon.net> on 2010/04/17 04:11:56 UTC

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());
    }


 


Re: Question about Reloading File (help!)

Posted by Alan Thomas <ja...@verizon.net>.
Andreas,

Thanks.  I will take a look.

I want to avoid closing and reopening the "output" file,
because this will be done many times and, thus, be
time-consuming.

Alan

--------------------------------------------------
From: "Andreas Lehmkuehler" <an...@lehmi.de>
Sent: Monday, April 19, 2010 12:43 PM
To: <us...@pdfbox.apache.org>
Subject: Re: Question about Reloading File (help!)

> 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 


Re: Question about Reloading File (help!)

Posted by Andreas Lehmkuehler <an...@lehmi.de>.
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