[ https://issues.apache.org/jira/browse/PDFBOX-3207?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Frank Becker updated PDFBOX-3207: --------------------------------- Description: Currently it is not possible to add a PDOutlineItem with siblings to another node with addFirst or addLast. Imagine you want to merge two documents. Each document has an outline. For the destination you want an outline where each document is represented with the title of the Document as outline item where the original outline nodes are appended to. To achieve that you need to: * add a new outline to the document. * create a new outline item with the title of the document * iterate over each child of the previous outline ** create a deep copy of the child to get rid of the previous and next sibling ** add the copy of the child to the new bookmark Little example code in groovy: {code} def bookmark() { if ( document != null ) { if (document.getPages().size() > 0) { def originalOutline = this.document.documentCatalog.documentOutline def newOutline = new PDDocumentOutline() def destination = new PDPageFitWidthDestination() def bookmark = new PDOutlineItem() destination.setPage(document.getPages().get(0)) bookmark.title = bookmarkTitle bookmark.destination = destination if(originalOutline) { appendOriginalBookmarks(originalOutline, bookmark) } newOutline.addLast(bookmark) document.documentCatalog.documentOutline = newOutline } } } private Iterable<PDOutlineItem> appendOriginalBookmarks(PDOutlineNode originalOutline, PDOutlineItem bookmark) { originalOutline.children().each { oldBookmark -> PDOutlineItem bookmarkClone = cloneBookmark(oldBookmark) if(oldBookmark.hasChildren()){ appendOriginalBookmarks(oldBookmark,bookmarkClone) } bookmark.addLast(bookmarkClone) } } private PDOutlineItem cloneBookmark(PDOutlineItem bookmark) { def bookmarkClone = new PDOutlineItem() bookmarkClone.title = bookmark.title bookmarkClone.destination = bookmark.destination bookmarkClone.action = bookmark.action bookmarkClone.structureElement = bookmark.structureElement bookmarkClone.bold = bookmark.bold bookmarkClone.italic = bookmark.italic bookmarkClone.textColor = bookmark.textColor bookmarkClone } {code} That is a bit inconvenient. It should look like that: {code:java} def originalOutline = this.document.documentCatalog.documentOutline def newOutline = new PDDocumentOutline() def destination = new PDPageFitWidthDestination() def bookmark = new PDOutlineItem() destination.setPage(document.getPages().get(0)) bookmark.title = bookmarkTitle bookmark.destination = destination if(originalOutline) { bookmark.addLast(originalOutline.getFirstChild()); } newOutline.addLast(bookmark) document.documentCatalog.documentOutline = newOutline {code} I'll add a patch that should make it possible. was: Currently it is not possible to add a PDOutlineItem with siblings to another node with addFirst or addLast. Imagine you want to merge two documents. Each document has an outline. For the destination you want an outline where each document is represented represented with the title of the Document as outline item where the original outline nodes are appended to. To achieve that you need to: * add a new outline to the document. * create a new outline item with the title of the document * iterate over each child of the previous outline ** create a deep copy of the child to get rid of the previous and next sibling ** add the copy of the child to the new bookmark Little example code in groovy: {code} def bookmark() { if ( document != null ) { if (document.getPages().size() > 0) { def originalOutline = this.document.documentCatalog.documentOutline def newOutline = new PDDocumentOutline() def destination = new PDPageFitWidthDestination() def bookmark = new PDOutlineItem() destination.setPage(document.getPages().get(0)) bookmark.title = bookmarkTitle bookmark.destination = destination if(originalOutline) { appendOriginalBookmarks(originalOutline, bookmark) } newOutline.addLast(bookmark) document.documentCatalog.documentOutline = newOutline } } } private Iterable<PDOutlineItem> appendOriginalBookmarks(PDOutlineNode originalOutline, PDOutlineItem bookmark) { originalOutline.children().each { oldBookmark -> PDOutlineItem bookmarkClone = cloneBookmark(oldBookmark) if(oldBookmark.hasChildren()){ appendOriginalBookmarks(oldBookmark,bookmarkClone) } bookmark.addLast(bookmarkClone) } } private PDOutlineItem cloneBookmark(PDOutlineItem bookmark) { def bookmarkClone = new PDOutlineItem() bookmarkClone.title = bookmark.title bookmarkClone.destination = bookmark.destination bookmarkClone.action = bookmark.action bookmarkClone.structureElement = bookmark.structureElement bookmarkClone.bold = bookmark.bold bookmarkClone.italic = bookmark.italic bookmarkClone.textColor = bookmark.textColor bookmarkClone } {code} That is a bit inconvenient. It should look like that: {code:java} def originalOutline = this.document.documentCatalog.documentOutline def newOutline = new PDDocumentOutline() def destination = new PDPageFitWidthDestination() def bookmark = new PDOutlineItem() destination.setPage(document.getPages().get(0)) bookmark.title = bookmarkTitle bookmark.destination = destination if(originalOutline) { bookmark.addLast(originalOutline.getFirstChild()); } newOutline.addLast(bookmark) document.documentCatalog.documentOutline = newOutline {code} I'll add a patch that should make it possible. > Allow addFirst and addLast to add Items with siblings > ----------------------------------------------------- > > Key: PDFBOX-3207 > URL: https://issues.apache.org/jira/browse/PDFBOX-3207 > Project: PDFBox > Issue Type: Improvement > Components: PDModel > Affects Versions: 2.0.0 > Reporter: Frank Becker > Priority: Minor > > Currently it is not possible to add a PDOutlineItem with siblings to another > node with addFirst or addLast. > Imagine you want to merge two documents. Each document has an outline. > For the destination you want an outline where each document is represented > with the title of the Document as outline item where the original outline > nodes are appended to. > To achieve that you need to: > * add a new outline to the document. > * create a new outline item with the title of the document > * iterate over each child of the previous outline > ** create a deep copy of the child to get rid of the previous and next sibling > ** add the copy of the child to the new bookmark > Little example code in groovy: > {code} > def bookmark() { > if ( document != null ) { > if (document.getPages().size() > 0) { > def originalOutline = > this.document.documentCatalog.documentOutline > def newOutline = new PDDocumentOutline() > def destination = new PDPageFitWidthDestination() > def bookmark = new PDOutlineItem() > destination.setPage(document.getPages().get(0)) > bookmark.title = bookmarkTitle > bookmark.destination = destination > if(originalOutline) { > appendOriginalBookmarks(originalOutline, bookmark) > } > newOutline.addLast(bookmark) > document.documentCatalog.documentOutline = newOutline > } > } > } > private Iterable<PDOutlineItem> appendOriginalBookmarks(PDOutlineNode > originalOutline, PDOutlineItem bookmark) { > originalOutline.children().each { oldBookmark -> > PDOutlineItem bookmarkClone = cloneBookmark(oldBookmark) > if(oldBookmark.hasChildren()){ > appendOriginalBookmarks(oldBookmark,bookmarkClone) > } > bookmark.addLast(bookmarkClone) > } > } > private PDOutlineItem cloneBookmark(PDOutlineItem bookmark) { > def bookmarkClone = new PDOutlineItem() > bookmarkClone.title = bookmark.title > bookmarkClone.destination = bookmark.destination > bookmarkClone.action = bookmark.action > bookmarkClone.structureElement = bookmark.structureElement > bookmarkClone.bold = bookmark.bold > bookmarkClone.italic = bookmark.italic > bookmarkClone.textColor = bookmark.textColor > bookmarkClone > } > {code} > That is a bit inconvenient. > It should look like that: > {code:java} > def originalOutline = > this.document.documentCatalog.documentOutline > def newOutline = new PDDocumentOutline() > def destination = new PDPageFitWidthDestination() > def bookmark = new PDOutlineItem() > destination.setPage(document.getPages().get(0)) > bookmark.title = bookmarkTitle > bookmark.destination = destination > if(originalOutline) { > bookmark.addLast(originalOutline.getFirstChild()); > } > newOutline.addLast(bookmark) > document.documentCatalog.documentOutline = newOutline > {code} > I'll add a patch that should make it possible. -- This message was sent by Atlassian JIRA (v6.3.4#6332) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org For additional commands, e-mail: dev-h...@pdfbox.apache.org