pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Marvin B. Lillehaug" <marvin.lilleh...@gmail.com>
Subject Re: Merge PDFs, add second pdf as child in outline
Date Tue, 09 Apr 2013 10:48:08 GMT
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?

Mime
View raw message