I solved it by doing as in my first post and removing the reference to
outline.Last and outline.firstChild.Next manually:

boolean removeRootOutlineSibling =
pdf.getDocumentCatalog().getDocumentOutline().getFirstChild().getNextSibling()
== null;
pdfMergerUtility.appendDocument(pdf, attachment);

PDDocumentOutline documentOutline =
pdf.getDocumentCatalog().getDocumentOutline();
PDOutlineItem originalRoot = documentOutline.getFirstChild();
PDOutlineItem appendedRoot = originalRoot.getNextSibling();
originalRoot.appendChild(appendedRoot);
if (removeRootOutlineSibling) {
   documentOutline.getCOSDictionary().setItem("Last", originalRoot);
   originalRoot.getCOSDictionary().removeItem(COSName.NEXT);
}

Thanks for the help!

On Tue, Apr 9, 2013 at 1:07 PM, Maruan Sahyoun <[email protected]> wrote:
> OK - without trying it out :-)
>
> What happens is that as you get the outlines from the attachments and add 
> them to the root of your outlineRoot and overwrite the document catalog the 
> objects the childs are pointing to are not yet part of the document as the 
> actual merging is done afterwards. So you need to rearrange the outline after 
> you have merged the documents.
>
>
> BR
> Maruan Sahyoun
>
> Am 09.04.2013 um 12:48 schrieb "Marvin B. Lillehaug" 
> <[email protected]>:
>
>> Sorry for being ambiguous.
>>
>> I have two pdfs, A and B that have outlines oA and oB. I want to
>> create a pdf C where the contents are A + B and the outline is oA with
>> oB as the child of oAs first child.
>>
>> oA: RootNode with name A
>> oB: RootNode with name B1
>>      RootNode with name B2
>> oC: RootNode with name A
>>                                        \- Node with name B1
>>                                         \ Node with name B2
>>
>> When using PDFMergerUtility A, B1, and B2 end up as siblings, and
>> clicking on their respective nodes in the resulting pdf works. With
>> the code in my first post it is still possible to click on both
>> instances of the outline items.
>> I have managed to make the outline look the way I want by getting the
>> reference to both roots before doing the merge, and setting this
>> outline before save, but when clicking in the outline nothing happens.
>> Code which accomplish this:
>>
>> PDDocument pdf = PDDocument.load(contentAsPdf);
>>
>> PDDocumentCatalog documentCatalog = pdf.getDocumentCatalog();
>> PDDocumentOutline outline = documentCatalog.getDocumentOutline();
>> PDOutlineItem outlineRoot = outline.getFirstChild();
>>
>> PDDocumentOutline newOutline = new PDDocumentOutline();
>> newOutline.appendChild(outlineRoot);
>>
>> PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
>> PDDocument attachment = PDDocument.load(attachmentsAsPdfs);
>>
>> PDDocumentCatalog attachmentDocumentCatalog = 
>> attachment.getDocumentCatalog();
>> PDDocumentOutline attachmentOutline =
>> attachmentDocumentCatalog.getDocumentOutline();
>>
>> PDOutlineItem attachmentOutlineItem = attachmentOutline.getFirstChild();
>>
>> outlineRoot.appendChild(attachmentOutlineItem);
>>
>> documentCatalog.setDocumentOutline(newOutline);
>> attachmentDocumentCatalog.setDocumentOutline(null);
>>
>> pdfMergerUtility.appendDocument(pdf, attachment);
>>
>> pdf.save(new FileOutputStream(contentAsPdf));
>>
>> Do I have to construct a new PDDocument from scratch in order to do this?
>



-- 
mvh,
Marvin B. Lillehaug

Reply via email to