Andreas,
When you said "copy the page", were you referring to something
like this?
PDPage imported = OutputDoc.importPage(page);
imported.setCropBox(page.findCropBox());
imported.setMediaBox(page.findMediaBox());
imported.setResources(page.findResources());
imported.setRotation(page.findRotation());
I took this from the processNextPage method in
org.apache.pdfbox.util.Splitter,
I think I'm still missing the idea, as I still get the same error.
Thanks, Alan
--------------------------------------------------
From: "Andreas Lehmkühler" <andr...@lehmi.de>
Sent: Saturday, July 04, 2009 10:11 AM
To: <pdfbox-users@incubator.apache.org>
Subject: Re: java.io.IOException: The handle is invalid
Hi Alan,
Alan Thomas schrieb:
I replaced the following line of code:
OutputDoc.addPage(page); // ***** test only *****
with these lines:
PDPage BlankPage = new PDPage();
OutputDoc.addPage(BlankPage );
and that worked fine.
However, it does not line me adding pages from another document.
Do I have to do something to the catalog first or . . . ? (I did not
see this in the examples.)
In your first example you just use the page from the input-doc and if
you close the input-doc all resources connected to that page are lost
and finally an exception is thrown. The solution is to copy the page
before adding it to the new document. It is a little bit more
complicated than just to call a copy or clone method, but you'll find a
good example how to do that in the command line tool
org.apache.pdfbox.PDFSplit. The copy algorithm starts at
org.apache.pdfbox.util.Spliiter.split()
HTH
Andreas Lehmkühler
From: Alan Thomas
Sent: Friday, July 03, 2009 12:24 PM
To: pdfbox-users@incubator.apache.org
Subject: java.io.IOException: The handle is invalid
What might cause the runtime error: "java.io.IOException: The handle is
invalid" in COSVisitorException?
All I did in my code with the problem object "OutputDoc" was:
PDDocument InputDoc = null;
PDDocument OutputDoc = new PDDocument();
try
{
InputDoc = PDDocument.load( pdfInFile );
. . . . .
List pages = InputDoc.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
while( iter.hasNext() )
{
PDPage page = (PDPage)iter.next();
OutputDoc.addPage(page); // ***** test only *****
. . . . .
}
finally
{
if( InputDoc != null )
{
InputDoc.close();
OutputDoc.save(pdfOutFile);
OutputDoc.close();
}
}
Here is the stack trace, below. Thanks, Alan
Exception in thread "main"
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)
at Drawings.Drawings(Drawings.java:113)
at Drawings.main(Drawings.java:39)
java.io.IOException: The handle is invalid
at java.io.RandomAccessFile.seek(Native Method)
at
org.apache.pdfbox.io.RandomAccessFile.seek(RandomAccessFile.java:59)
at
org.apache.pdfbox.io.RandomAccessFileInputStream.read(RandomAccessFileInputStream.java:96)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at
java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at
org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:926)
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)
at Drawings.Drawings(Drawings.java:113)
at Drawings.main(Drawings.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)