Return-Path: X-Original-To: apmail-pdfbox-users-archive@www.apache.org Delivered-To: apmail-pdfbox-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7313510624 for ; Tue, 17 Feb 2015 21:53:47 +0000 (UTC) Received: (qmail 83722 invoked by uid 500); 17 Feb 2015 21:53:41 -0000 Delivered-To: apmail-pdfbox-users-archive@pdfbox.apache.org Received: (qmail 83698 invoked by uid 500); 17 Feb 2015 21:53:41 -0000 Mailing-List: contact users-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@pdfbox.apache.org Delivered-To: mailing list users@pdfbox.apache.org Received: (qmail 83686 invoked by uid 99); 17 Feb 2015 21:53:40 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Feb 2015 21:53:40 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [213.133.104.168] (HELO www168.your-server.de) (213.133.104.168) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Feb 2015 21:53:14 +0000 Received: from [88.198.220.131] (helo=sslproxy02.your-server.de) by www168.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.80.1) (envelope-from ) id 1YNq4u-0000GH-Ud for users@pdfbox.apache.org; Tue, 17 Feb 2015 22:53:12 +0100 Received: from [79.242.118.74] (helo=mbp001.intern) by sslproxy02.your-server.de with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.80) (envelope-from ) id 1YNq4r-00087K-Ja for users@pdfbox.apache.org; Tue, 17 Feb 2015 22:53:09 +0100 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: The importPage leads to error in PDFBox 2.0.0 From: Maruan Sahyoun In-Reply-To: Date: Tue, 17 Feb 2015 22:53:05 +0100 Content-Transfer-Encoding: quoted-printable Message-Id: References: <54E3AF14.7010804@t-online.de> <1F91CEAD-BAEC-4975-B31B-563CF160A16A@fileaffairs.de> <54E3B4A4.30508@t-online.de> To: users@pdfbox.apache.org X-Mailer: Apple Mail (2.1878.6) X-Authenticated-Sender: sahyoun@fileaffairs.de X-Virus-Scanned: Clear (ClamAV 0.98.5/20070/Tue Feb 17 19:37:33 2015) X-Virus-Checked: Checked by ClamAV on apache.org AFAIK there is currently no easy way as addPage() always adds to the = end. Now what you could do is change the order using the COS model COSDictionary pages =3D (COSDictionary) = doc.getDocumentCatalog().getCOSObject().getDictionaryObject(COSName.PAGES)= ; COSArray kids =3D (COSArray) = pages.getDictionaryObject(COSName.KIDS); Now you could add/insert/remove items as needed to kids. Maruan Am 17.02.2015 um 22:40 schrieb Ilya Kantor : > My question is actually simpler. >=20 > I only need to insert the first page, no messing with nested trees. >=20 > --- > Best Regards, > Ilya Kantor >=20 > 2015-02-18 0:37 GMT+03:00 Tilman Hausherr : >=20 >> Don't know, but there is this issue about a similar wish: >> https://issues.apache.org/jira/browse/PDFBOX-2400 >>=20 >> Tilman >>=20 >> Am 17.02.2015 um 22:27 schrieb Ilya Kantor: >>=20 >>> Hi Maruan, >>>=20 >>> Thanks, you were very precise, that's my fault to close the thing = twice. >>> Now it works. >>>=20 >>> Can I insert the new page as the first one? >>>=20 >>> For PDFBox 1.8.8 did it this way: >>> =3D=3D=3D=3D=3D=3D >>> doc.getDocumentCatalog().getPages().getKids().add(0, (PDPage) >>> cover.getDocumentCatalog().getAllPages().get(0)); >>> =3D=3D=3D=3D=3D=3D=3D=3D >>>=20 >>> --- >>> Best Regards, >>> Ilya Kantor >>>=20 >>> 2015-02-18 0:16 GMT+03:00 Maruan Sahyoun : >>>=20 >>> Maruan also meant to tell you not to close coverDoc before saving = :-) >>>>>=20 >>>>> thx Tilman - as usual I wasn't precise enough :-) >>>>=20 >>>>=20 >>>> Tilman >>>>>=20 >>>>> Am 17.02.2015 um 22:08 schrieb Ilya Kantor: >>>>>=20 >>>>>> Hi, >>>>>>=20 >>>>>> Still getting the error >>>>>>=20 >>>>>> That's the updated code: >>>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>> PDDocument doc =3D PDDocument.load(inputFile); >>>>>> PDDocument coverDoc =3D null; >>>>>>=20 >>>>>> if (coverFile !=3D null) { >>>>>> coverDoc =3D PDDocument.load(coverFile); >>>>>> PDPage coverPage =3D coverDoc.getPage(0); >>>>>> doc.addPage(coverPage); >>>>>> coverDoc.close(); >>>>>>=20 >>>>> <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D >>>>=20 >>>>> } >>>>>>=20 >>>>>> PDDocumentOutline outline =3D new PDDocumentOutline(); >>>>>> // ... >>>>>> doc.getDocumentCatalog().setDocumentOutline(outline); >>>>>>=20 >>>>>> doc.save(outputFile); >>>>>> if (coverDoc !=3D null) coverDoc.close(); >>>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>>=20 >>>>>> I'm getting the error in doc.save(): >>>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>> Exception in thread "main" java.io.IOException: COSStream has = been >>>>>>=20 >>>>> closed >>>>=20 >>>>> and cannot be read. Perhaps its enclosing PDDocument has been = closed? >>>>>> at >>>>>>=20 >>>>> = org.apache.pdfbox.cos.COSStream.getFilteredStream(COSStream.java:163) >>>>=20 >>>>> at >>>>>>=20 >>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream( >>>> COSWriter.java:1151) >>>>=20 >>>>> at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:298) >>>>>> at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:156) >>>>>> at >>>>>>=20 >>>>> = org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:542) >>>>=20 >>>>> at >>>>>>=20 >>>>> = org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:449) >>>>=20 >>>>> at >>>>>>=20 >>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument( >>>> COSWriter.java:1035) >>>>=20 >>>>> at = org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:464) >>>>>> at = org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1312) >>>>>> at = org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1220) >>>>>> at = org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:950) >>>>>> at = org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:922) >>>>>> at PdfBookPolisher.run(PdfBookPolisher.java:129) >>>>>> at PdfBookPolisher.main(PdfBookPolisher.java:69) >>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) >>>>>> at >>>>>>=20 >>>>>> sun.reflect.NativeMethodAccessorImpl.invoke( >>>> NativeMethodAccessorImpl.java:57) >>>>=20 >>>>> at >>>>>>=20 >>>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke( >>>> DelegatingMethodAccessorImpl.java:43) >>>>=20 >>>>> at java.lang.reflect.Method.invoke(Method.java:606) >>>>>> at >>>>>>=20 >>>>> = com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) >>>>=20 >>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>>=20 >>>>>> --- >>>>>> Best Regards, >>>>>> Ilya Kantor >>>>>>=20 >>>>>> 2015-02-18 0:05 GMT+03:00 Maruan Sahyoun = : >>>>>>=20 >>>>>> try closing the coverDoc after you've saved doc. >>>>>>>=20 >>>>>>> BR >>>>>>> Maruan >>>>>>>=20 >>>>>>> Am 17.02.2015 um 22:00 schrieb Ilya Kantor : >>>>>>>=20 >>>>>>> Hi, >>>>>>>>=20 >>>>>>>> I'm trying to import a page from one document into another one = and >>>>>>>>=20 >>>>>>> getting >>>>>>>=20 >>>>>>>> an error with PDFBox 2.0.0 snapshot. >>>>>>>>=20 >>>>>>>> That's the code: >>>>>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>>>> PDDocument doc =3D PDDocument.load(inputFile); >>>>>>>>=20 >>>>>>>> if (coverFile !=3D null) { >>>>>>>> PDDocument coverDoc =3D PDDocument.load(coverFile); >>>>>>>> PDPage coverPage =3D coverDoc.getPage(0); >>>>>>>> doc.importPage(coverPage); >>>>>>>> coverDoc.close(); >>>>>>>> } >>>>>>>>=20 >>>>>>>> PDDocumentOutline outline =3D new PDDocumentOutline(); >>>>>>>> // ... I'm also working with the outline >>>>>>>> doc.getDocumentCatalog().setDocumentOutline(outline); >>>>>>>>=20 >>>>>>>> doc.save(outputFile); >>>>>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>>>>=20 >>>>>>>> And here's the error when saving (the last line): >>>>>>>>=20 >>>>>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>>>> Exception in thread "main" java.io.IOException: COSStream has = been >>>>>>>>=20 >>>>>>> closed >>>>=20 >>>>> and cannot be read. Perhaps its enclosing PDDocument has been = closed? >>>>>>>> at >>>>>>>>=20 >>>>>>> = org.apache.pdfbox.cos.COSStream.getFilteredStream(COSStream.java:163) >>>>=20 >>>>> at >>>>>>>>=20 >>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream( >>>> COSWriter.java:1151) >>>>=20 >>>>> at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:298) >>>>>>>> at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:156) >>>>>>>> at >>>>>>>>=20 >>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject( >>>>>>> COSWriter.java:542) >>>>>>>=20 >>>>>>>> at >>>>>>>>=20 >>>>>>> = org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:449) >>>>=20 >>>>> at >>>>>>>>=20 >>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument( >>>> COSWriter.java:1035) >>>>=20 >>>>> at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:464) >>>>>>>> at = org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1312) >>>>>>>> at = org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1220) >>>>>>>> at = org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:950) >>>>>>>> at = org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:922) >>>>>>>> at PdfBookPolisher.run(PdfBookPolisher.java:128) >>>>>>>> at PdfBookPolisher.main(PdfBookPolisher.java:69) >>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) >>>>>>>> at >>>>>>>>=20 >>>>>>>> sun.reflect.NativeMethodAccessorImpl.invoke( >>>> NativeMethodAccessorImpl.java:57) >>>>=20 >>>>> at >>>>>>>>=20 >>>>>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke( >>>> DelegatingMethodAccessorImpl.java:43) >>>>=20 >>>>> at java.lang.reflect.Method.invoke(Method.java:606) >>>>>>>> at >>>>>>>>=20 >>>>>>> = com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) >>>>=20 >>>>> =3D=3D=3D=3D=3D=3D >>>>>>>>=20 >>>>>>>> The problem does not happen when I do not import page. >>>>>>>>=20 >>>>>>>> P.S. Can I insert the new page as the first one, not the last = one? >>>>>>>> Something like = doc.getDocumentCatalog().getPages().getKids().add(0, >>>>>>>> (PDPage) cover.getDocumentCatalog().getAllPages().get(0)); >>>>>>>> But for 2.0.0. >>>>>>>>=20 >>>>>>>>=20 >>>>>>>> --- >>>>>>>> Best Regards, >>>>>>>> Ilya Kantor >>>>>>>>=20 >>>>>>>=20 >>>>> = --------------------------------------------------------------------- >>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org >>>>> For additional commands, e-mail: users-help@pdfbox.apache.org >>>>>=20 >>>>>=20 >>>> = --------------------------------------------------------------------- >>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org >>>> For additional commands, e-mail: users-help@pdfbox.apache.org >>>>=20 >>>>=20 >>>>=20 >>=20 >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org >> For additional commands, e-mail: users-help@pdfbox.apache.org >>=20 >>=20 --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org For additional commands, e-mail: users-help@pdfbox.apache.org