[ 
https://issues.apache.org/jira/browse/PDFBOX-497?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12737027#action_12737027
 ] 

Andreas Lehmkühler commented on PDFBOX-497:
-------------------------------------------

Alan,

thanks for your feedback. 

The problem is that copyPage() doesn't really copy all the data, it just uses 
references to them. If the source doc is closed before the target doc is saved 
some of these references get lost and the described exception is thrown. If we 
want to change that behaviour we have to improve the copy mechanism to really 
copy the whole content of a page. AFAIK the code of the importPage method 
already copies all data, but the other 4 findXXX methods are just returning 
references and not a copy of the data.

I'm afraid that the pdfbox documentation is as complete and up-to-date as many 
other project documentations all over the world. ;-))



> COSVisitorException: The handle is invalid
> ------------------------------------------
>
>                 Key: PDFBOX-497
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-497
>             Project: PDFBox
>          Issue Type: Bug
>          Components: PDModel
>         Environment: Windows Vista
>            Reporter: James A. Thomas
>             Fix For: 0.8.0-incubator
>
>
>     If I save a PDF document in a method of an application class, I get a 
> COSVisitorException: The handle is invalid.  
> --------------------------------------------------------------------------------------------------
>  
>        I found that I no longer get this exception if I do not close the 
> PDDocument "DrawingDoc" in the method "addDrawing".
>         However, I do get a warning:
> java.lang.Throwable: Warning: You did not close the PDF Document
>         at org.apache.pdfbox.cos.COSDocument.finalize(COSDocument.java:391)
>         at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
>         at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
>         at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
>         at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
>                                             Alan
> --------------------------------------------------
> From: "Alan Thomas" <[email protected]>
> Sent: Monday, July 27, 2009 6:13 AM
> To: <[email protected]>
> Subject: Re: COSVisitorException: The handle is invalid
> > It was created by adding pages from other PDF documents, like below.
> >
> > Is there anything wrong with returning a PDDocument from the method 
> > addDrawing like I am doing?
> >
> > 
> > Alan
> >
> > PDDocument OutputDoc = null;
> > . . .
> >
> > OutputDoc = new PDDocument();
> > . . .
> > copyPage(OutputDoc, page);
> > . . .
> > OutputDoc = addDrawing(OutputDoc,
> >                                DrawingsDir.getName(), Barcode);
> > . . .
> >            finally
> >            {
> >                try
> >                {
> >                    // Save and close the output file
> >                    if (OutputDoc != null)
> >                    {
> >                        System.out.println("OutPath = " + OutPath);
> >                        OutputDoc.save(OutPath);
> >                    }
> >                    OutputDoc.close();
> > . . .
> >
> > using these methods I created:
> >
> >    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());
> >    }
> >
> >    private PDDocument addDrawing( PDDocument OutDoc, String 
> > DrawingsDirName,
> >                                        String barcode)
> >    {
> >        PDDocument DrawingDoc = null;
> >
> >      try
> >      {
> >            .  .  .
> >          List pages = DrawingDoc.getDocumentCatalog().getAllPages();
> >
> >          for (Object obj : pages)
> >          {
> >              PDPage page = (PDPage)obj;
> >              copyPage(OutDoc, page);
> >          }
> >      }
> >      catch (Exception e)
> >      {
> >          e.printStackTrace();
> >      }
> >      finally
> >      {
> >        try
> >        {
> >          DrawingDoc.close();
> >          return OutDoc;
> >        }
> >        catch (Exception e)
> >        {
> >            return OutDoc;
> >        }
> >      }
> >    }
> >
> >
> > --------------------------------------------------
> > From: "Andreas Lehmkühler" <[email protected]>
> > Sent: Monday, July 27, 2009 5:15 AM
> > To: <[email protected]>
> > Subject: Re: COSVisitorException: The handle is invalid
> >
> >> Hi Alan,
> >>
> >> how did you create your pdf?
> >>
> >> BR
> >> Andreas Lehmkühler
> >>
> >> ----- original Nachricht --------
> >>
> >> Betreff: COSVisitorException: The handle is invalid
> >> Gesendet: Mo, 27. Jul 2009
> >> Von: Alan Thomas<[email protected]>
> >>
> >>> Hi.  When I save a PDF document in my application, I get a
> >>> COSVisitorException: "The handle is invalid".
> >>>
> >>>         However, I do not see anything wrong with it.  I show the error, 
> >>> the
> >>> string I am giving the PDDocument.save() method, and the code that 
> >>> attempts
> >>> to save the file.
> >>>
> >>>         Any idea what I am doing wrong?                Thanks, Alan
> >>>
> >>> OutPath = C:\Users\Alan\Documents\My Stuff\X Y Z Stuff\Application\Java 
> >>> App
> >>> Test\New Folder\InsertedPages 2.pdf
> >>>
> >>> org.apache.pdfbox.exceptions.COSVisitorException: The handle is invalid
> >>>         at
> >>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:939)
> >>>         at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:201)
> >>>         at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:206)
> >>>         at
> >>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:430)
> >>>         at
> >>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:361)
> >>>         at
> >>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:768)
> >>>         at 
> >>> org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:361)
> >>>         at 
> >>> org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1070)
> >>>         at 
> >>> org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:787)
> >>>         at 
> >>> org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:768)
> >>>
> >>>
> >>>                     // Save and close the output file
> >>>                     if (OutputDoc != null)
> >>>                     {
> >>>                         System.out.println("OutPath = " + OutPath);
> >>>                         OutputDoc.save(OutPath);
> >>>                     }
> >>>                     OutputDoc.close();
> >>>
> >>
> >> --- original Nachricht Ende ----
> >>
> >>
> >

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to