Am 17.02.2015 um 22:53 schrieb Maruan Sahyoun <[email protected]>:

> 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 = (COSDictionary) 
> doc.getDocumentCatalog().getCOSObject().getDictionaryObject(COSName.PAGES);
>        COSArray kids = (COSArray) pages.getDictionaryObject(COSName.KIDS);
> 
> Now you could add/insert/remove items as needed to kids.
> 

As an added note 

use addPage() to add the page which you now know to be the last entry of the 
array and then use the COSArray methods as mentioned above. The benefit is that 
addPage will have updated the page count for you.

BR
Maruan


> Maruan
> 
> Am 17.02.2015 um 22:40 schrieb Ilya Kantor <[email protected]>:
> 
>> My question is actually simpler.
>> 
>> I only need to insert the first page, no messing with nested trees.
>> 
>> ---
>> Best Regards,
>> Ilya Kantor
>> 
>> 2015-02-18 0:37 GMT+03:00 Tilman Hausherr <[email protected]>:
>> 
>>> Don't know, but there is this issue about a similar wish:
>>> https://issues.apache.org/jira/browse/PDFBOX-2400
>>> 
>>> Tilman
>>> 
>>> Am 17.02.2015 um 22:27 schrieb Ilya Kantor:
>>> 
>>>> Hi Maruan,
>>>> 
>>>> Thanks, you were very precise, that's my fault to close the thing twice.
>>>> Now it works.
>>>> 
>>>> Can I insert the new page as the first one?
>>>> 
>>>> For PDFBox 1.8.8 did it this way:
>>>> ======
>>>> doc.getDocumentCatalog().getPages().getKids().add(0, (PDPage)
>>>> cover.getDocumentCatalog().getAllPages().get(0));
>>>> ========
>>>> 
>>>> ---
>>>> Best Regards,
>>>> Ilya Kantor
>>>> 
>>>> 2015-02-18 0:16 GMT+03:00 Maruan Sahyoun <[email protected]>:
>>>> 
>>>> Maruan also meant to tell you not to close coverDoc before saving :-)
>>>>>> 
>>>>>> thx Tilman - as usual I wasn't precise enough :-)
>>>>> 
>>>>> 
>>>>> Tilman
>>>>>> 
>>>>>> Am 17.02.2015 um 22:08 schrieb Ilya Kantor:
>>>>>> 
>>>>>>> Hi,
>>>>>>> 
>>>>>>> Still getting the error
>>>>>>> 
>>>>>>> That's the updated code:
>>>>>>> =================
>>>>>>> PDDocument doc = PDDocument.load(inputFile);
>>>>>>> PDDocument coverDoc = null;
>>>>>>> 
>>>>>>> if (coverFile != null) {
>>>>>>>    coverDoc = PDDocument.load(coverFile);
>>>>>>>    PDPage coverPage = coverDoc.getPage(0);
>>>>>>>    doc.addPage(coverPage);
>>>>>>>    coverDoc.close();
>>>>>>> 
>>>>>> <===================================================
>>>>> 
>>>>>> }
>>>>>>> 
>>>>>>> PDDocumentOutline outline = new PDDocumentOutline();
>>>>>>> // ...
>>>>>>> doc.getDocumentCatalog().setDocumentOutline(outline);
>>>>>>> 
>>>>>>> doc.save(outputFile);
>>>>>>> if (coverDoc != null) coverDoc.close();
>>>>>>> =================
>>>>>>> 
>>>>>>> I'm getting the error in doc.save():
>>>>>>> =================
>>>>>>> Exception in thread "main" java.io.IOException: COSStream has been
>>>>>>> 
>>>>>> closed
>>>>> 
>>>>>> and cannot be read. Perhaps its enclosing PDDocument has been closed?
>>>>>>>  at
>>>>>>> 
>>>>>> org.apache.pdfbox.cos.COSStream.getFilteredStream(COSStream.java:163)
>>>>> 
>>>>>>  at
>>>>>>> 
>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(
>>>>> COSWriter.java:1151)
>>>>> 
>>>>>>  at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:298)
>>>>>>>  at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:156)
>>>>>>>  at
>>>>>>> 
>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:542)
>>>>> 
>>>>>>  at
>>>>>>> 
>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:449)
>>>>> 
>>>>>>  at
>>>>>>> 
>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(
>>>>> COSWriter.java:1035)
>>>>> 
>>>>>>  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
>>>>>>> 
>>>>>>> sun.reflect.NativeMethodAccessorImpl.invoke(
>>>>> NativeMethodAccessorImpl.java:57)
>>>>> 
>>>>>>  at
>>>>>>> 
>>>>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(
>>>>> DelegatingMethodAccessorImpl.java:43)
>>>>> 
>>>>>>  at java.lang.reflect.Method.invoke(Method.java:606)
>>>>>>>  at
>>>>>>> 
>>>>>> com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
>>>>> 
>>>>>> ================
>>>>>>> 
>>>>>>> ---
>>>>>>> Best Regards,
>>>>>>> Ilya Kantor
>>>>>>> 
>>>>>>> 2015-02-18 0:05 GMT+03:00 Maruan Sahyoun <[email protected]>:
>>>>>>> 
>>>>>>> try closing the coverDoc after you've saved doc.
>>>>>>>> 
>>>>>>>> BR
>>>>>>>> Maruan
>>>>>>>> 
>>>>>>>> Am 17.02.2015 um 22:00 schrieb Ilya Kantor <[email protected]>:
>>>>>>>> 
>>>>>>>> Hi,
>>>>>>>>> 
>>>>>>>>> I'm trying to import a page from one document into another one and
>>>>>>>>> 
>>>>>>>> getting
>>>>>>>> 
>>>>>>>>> an error with PDFBox 2.0.0 snapshot.
>>>>>>>>> 
>>>>>>>>> That's the code:
>>>>>>>>> ==================
>>>>>>>>> PDDocument doc = PDDocument.load(inputFile);
>>>>>>>>> 
>>>>>>>>> if (coverFile != null) {
>>>>>>>>>   PDDocument coverDoc = PDDocument.load(coverFile);
>>>>>>>>>   PDPage coverPage = coverDoc.getPage(0);
>>>>>>>>>   doc.importPage(coverPage);
>>>>>>>>>   coverDoc.close();
>>>>>>>>> }
>>>>>>>>> 
>>>>>>>>> PDDocumentOutline outline = new PDDocumentOutline();
>>>>>>>>> // ... I'm also working with the outline
>>>>>>>>> doc.getDocumentCatalog().setDocumentOutline(outline);
>>>>>>>>> 
>>>>>>>>> doc.save(outputFile);
>>>>>>>>> =================
>>>>>>>>> 
>>>>>>>>> And here's the error when saving (the last line):
>>>>>>>>> 
>>>>>>>>> ================
>>>>>>>>> Exception in thread "main" java.io.IOException: COSStream has been
>>>>>>>>> 
>>>>>>>> closed
>>>>> 
>>>>>> and cannot be read. Perhaps its enclosing PDDocument has been closed?
>>>>>>>>> at
>>>>>>>>> 
>>>>>>>> org.apache.pdfbox.cos.COSStream.getFilteredStream(COSStream.java:163)
>>>>> 
>>>>>> at
>>>>>>>>> 
>>>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(
>>>>> COSWriter.java:1151)
>>>>> 
>>>>>> at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:298)
>>>>>>>>> at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:156)
>>>>>>>>> at
>>>>>>>>> 
>>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(
>>>>>>>> COSWriter.java:542)
>>>>>>>> 
>>>>>>>>> at
>>>>>>>>> 
>>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:449)
>>>>> 
>>>>>> at
>>>>>>>>> 
>>>>>>>>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(
>>>>> COSWriter.java:1035)
>>>>> 
>>>>>> 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
>>>>>>>>> 
>>>>>>>>> sun.reflect.NativeMethodAccessorImpl.invoke(
>>>>> NativeMethodAccessorImpl.java:57)
>>>>> 
>>>>>> at
>>>>>>>>> 
>>>>>>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(
>>>>> DelegatingMethodAccessorImpl.java:43)
>>>>> 
>>>>>> at java.lang.reflect.Method.invoke(Method.java:606)
>>>>>>>>> at
>>>>>>>>> 
>>>>>>>> com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
>>>>> 
>>>>>> ======
>>>>>>>>> 
>>>>>>>>> The problem does not happen when I do not import page.
>>>>>>>>> 
>>>>>>>>> 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.
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> ---
>>>>>>>>> Best Regards,
>>>>>>>>> Ilya Kantor
>>>>>>>>> 
>>>>>>>> 
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: [email protected]
>>>>>> For additional commands, e-mail: [email protected]
>>>>>> 
>>>>>> 
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [email protected]
>>>>> For additional commands, e-mail: [email protected]
>>>>> 
>>>>> 
>>>>> 
>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>> 
>>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to