You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gary Grosso <ga...@oberontech.com> on 2015/10/21 17:52:16 UTC

RE: how to create an entire bookmark (outline) tree in an existing PDF document?

(Before asking a new question, forthcoming) I wanted to report back that with this help I was able to implement the grafting of bookmarks into an existing PDF document. Thanks especially to Gilad and Tilman.

Below is the method doing the actual change. The 2nd parameter is a representation of the desired bookmark data, which has been instantiated by reading from an XML file which was passed to the program.

    private static void processOneDocument(String fileName, BookmarkNode bookmarkData, File logFile) {
        PDDocument document = null;
        String outdoc;
        try {
            document = PDDocument.load(fileName);
        } catch (IOException exc) {
            writeLog(logFile, "Can't open " + fileName);
            problemLevel = Math.max(problemLevel, WARN);
            return;
        }
        outdoc = fileName;
        try {
            if (document.isEncrypted()) {
                writeLog(logFile, "Cannot add bookmarks to encrypted document " + fileName);
                problemLevel = Math.max(problemLevel, WARN);
                return;
            }
            PDDocumentOutline outline =  new PDDocumentOutline();
            document.getDocumentCatalog().setDocumentOutline(outline);
            PDOutlineItem bookmarksOutline = new PDOutlineItem();
            bookmarksOutline.setTitle(bookmarkData.getTitle());
            outline.appendChild(bookmarksOutline);
            bookmarkData.addToOutline(bookmarksOutline);
            bookmarksOutline.openNode();
            outline.openNode();
            document.save( outdoc );
        } catch (Throwable exc) {
            writeLog(logFile, "Failure modifying " + fileName + "; " +  exc.getCause());
            problemLevel = Math.max(problemLevel, WARN);
            return;
        }
        finally {
            if( document != null ) {
                try {
                    document.close();
                } catch (IOException exc) {
                    writeLog(logFile, "Failure closing " + fileName + "; " +  exc.getCause());
                    problemLevel = Math.max(problemLevel, WARN);
                    return;
                }
            }
        }
    }


Gary


-----Original Message-----
From: Gary Grosso [mailto:gary.grosso@oberontech.com] 
Sent: Friday, August 28, 2015 12:32 PM
To: users@pdfbox.apache.org
Subject: RE: how to create an entire bookmark (outline) tree in an existing PDF document?

Hi Tilman,

I see, I would have had trouble trying to call PDOutlineItem.addLast() in 1.8.

I'm beginning to learn my way around pdfbox/examples which should help.

Thanks!
Gary


-----Original Message-----
From: Tilman Hausherr [mailto:THausherr@t-online.de]
Sent: Friday, August 28, 2015 12:20 PM
To: users@pdfbox.apache.org
Subject: Re: how to create an entire bookmark (outline) tree in an existing PDF document?

Am 28.08.2015 um 18:11 schrieb Gary Grosso:
> Thanks, Gilad.
>
> Further extensive Googling also turned up “Source Code of org.apache.pdfbox.examples.pdmodel.CreateBookmarks” at http://massapi.com/source/github/74/69/746955461/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java.html#67 which together with your overview should get me started.

This is for the 2.0 (not released) version. For the 1.8 (release) version, use this:
https://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java?view=markup&sortby=date

Tilman

>
> I’ll report back on my progress.
>
> Gary
>
> From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
> Sent: Friday, August 28, 2015 12:04 PM
> To: users@pdfbox.apache.org
> Cc: Gary Grosso <ga...@oberontech.com>
> Subject: Re: how to create an entire bookmark (outline) tree in an existing PDF document?
>
> Create a new PDDocumentOutline object, and then add all of your PDOutlineNode children under it.
> When done, access the file's PDDocumentCatalog and use the setDocumentOutline method to attach your bookmarks outline object to the document.
> I didn't try doing it from scratch, but I believe it should work.
>
> On Fri, Aug 28, 2015 at 5:54 PM, Gary Grosso <ga...@oberontech.com>> wrote:
> Greetings knowledgeable ones,
>
> I'm new to PDFbox, and am under some time pressure to get back to a customer  with confirmation we can do what they want and a rough estimate.
>
> For background, they have a single PDF file that is a master TOC linking to hundreds of subordinate topic PDFs. They would like each such topic PDF to have bookmarks which would essentially parallel the master TOC, such that an end user can click a bookmark in any topic PDF to navigate to any other topic PDF.
>
> I am creating this master TOC, using proprietary software, so I have the data "in hand" - what the titles and hierarchy are, and what the filenames are each one should link to. This proprietary software does not have the capability to generate the desired bookmarks in the topic PDFs - it can only generate internal links to items within the current PDF document -- but it does have the ability to call a Java application, so that is where I want to use PDFbox, to post-process the topic-PDF output and add the bookmarks.
>
> I've  been browsing through the Javadoc, and I'm pretty sure I need to be working with PDOutlineNode and PDOutlineItem. I can see methods which sound like they could be used to add more items and sub-nodes to the tree, but I'm not seeing how to link the tree into the document to begin with.  There is not much in the way of example code or cookbook-type references, and what I do find is 90% about reading PDFs and very little about modifying/augmenting them. So, I could really use some help knowing how to get started.
>
> I could create these topic PDFs with no bookmarks at all, or generate 
> them with one bookmark to the top of page 1, whichever is easier to 
> work with. (I'm thinking it may be easier if I have a sort of 
> placeholder to start with.)
>
> Thanks for any help,
> Gary
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


RE: how to create an entire bookmark (outline) tree in an existing PDF document?

Posted by Gary Grosso <ga...@oberontech.com>.
I probably should have included this method too.

    public void addToOutline(PDOutlineItem outline) {
        PDOutlineItem targetBookmark = outline;
        Iterator<BookmarkNode> iterator = this.getChildren().iterator();
        while (iterator.hasNext()) {
            BookmarkNode next = iterator.next();
            PDOutlineItem newBookmark = AugmentBookmarks.addBookmark(targetBookmark, next.getTitle(), next.getAction(), false);
            if (next.getChildren() != null) {
                next.addToOutline(newBookmark);
            }
        }
    }


Gary


-----Original Message-----
From: Gary Grosso 
Sent: Wednesday, October 21, 2015 11:52 AM
To: users@pdfbox.apache.org
Cc: Gary Grosso <ga...@oberontech.com>
Subject: RE: how to create an entire bookmark (outline) tree in an existing PDF document?

(Before asking a new question, forthcoming) I wanted to report back that with this help I was able to implement the grafting of bookmarks into an existing PDF document. Thanks especially to Gilad and Tilman.

Below is the method doing the actual change. The 2nd parameter is a representation of the desired bookmark data, which has been instantiated by reading from an XML file which was passed to the program.

    private static void processOneDocument(String fileName, BookmarkNode bookmarkData, File logFile) {
        PDDocument document = null;
        String outdoc;
        try {
            document = PDDocument.load(fileName);
        } catch (IOException exc) {
            writeLog(logFile, "Can't open " + fileName);
            problemLevel = Math.max(problemLevel, WARN);
            return;
        }
        outdoc = fileName;
        try {
            if (document.isEncrypted()) {
                writeLog(logFile, "Cannot add bookmarks to encrypted document " + fileName);
                problemLevel = Math.max(problemLevel, WARN);
                return;
            }
            PDDocumentOutline outline =  new PDDocumentOutline();
            document.getDocumentCatalog().setDocumentOutline(outline);
            PDOutlineItem bookmarksOutline = new PDOutlineItem();
            bookmarksOutline.setTitle(bookmarkData.getTitle());
            outline.appendChild(bookmarksOutline);
            bookmarkData.addToOutline(bookmarksOutline);
            bookmarksOutline.openNode();
            outline.openNode();
            document.save( outdoc );
        } catch (Throwable exc) {
            writeLog(logFile, "Failure modifying " + fileName + "; " +  exc.getCause());
            problemLevel = Math.max(problemLevel, WARN);
            return;
        }
        finally {
            if( document != null ) {
                try {
                    document.close();
                } catch (IOException exc) {
                    writeLog(logFile, "Failure closing " + fileName + "; " +  exc.getCause());
                    problemLevel = Math.max(problemLevel, WARN);
                    return;
                }
            }
        }
    }


Gary


-----Original Message-----
From: Gary Grosso [mailto:gary.grosso@oberontech.com]
Sent: Friday, August 28, 2015 12:32 PM
To: users@pdfbox.apache.org
Subject: RE: how to create an entire bookmark (outline) tree in an existing PDF document?

Hi Tilman,

I see, I would have had trouble trying to call PDOutlineItem.addLast() in 1.8.

I'm beginning to learn my way around pdfbox/examples which should help.

Thanks!
Gary


-----Original Message-----
From: Tilman Hausherr [mailto:THausherr@t-online.de]
Sent: Friday, August 28, 2015 12:20 PM
To: users@pdfbox.apache.org
Subject: Re: how to create an entire bookmark (outline) tree in an existing PDF document?

Am 28.08.2015 um 18:11 schrieb Gary Grosso:
> Thanks, Gilad.
>
> Further extensive Googling also turned up “Source Code of org.apache.pdfbox.examples.pdmodel.CreateBookmarks” at http://massapi.com/source/github/74/69/746955461/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java.html#67 which together with your overview should get me started.

This is for the 2.0 (not released) version. For the 1.8 (release) version, use this:
https://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java?view=markup&sortby=date

Tilman

>
> I’ll report back on my progress.
>
> Gary
>
> From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
> Sent: Friday, August 28, 2015 12:04 PM
> To: users@pdfbox.apache.org
> Cc: Gary Grosso <ga...@oberontech.com>
> Subject: Re: how to create an entire bookmark (outline) tree in an existing PDF document?
>
> Create a new PDDocumentOutline object, and then add all of your PDOutlineNode children under it.
> When done, access the file's PDDocumentCatalog and use the setDocumentOutline method to attach your bookmarks outline object to the document.
> I didn't try doing it from scratch, but I believe it should work.
>
> On Fri, Aug 28, 2015 at 5:54 PM, Gary Grosso <ga...@oberontech.com>> wrote:
> Greetings knowledgeable ones,
>
> I'm new to PDFbox, and am under some time pressure to get back to a customer  with confirmation we can do what they want and a rough estimate.
>
> For background, they have a single PDF file that is a master TOC linking to hundreds of subordinate topic PDFs. They would like each such topic PDF to have bookmarks which would essentially parallel the master TOC, such that an end user can click a bookmark in any topic PDF to navigate to any other topic PDF.
>
> I am creating this master TOC, using proprietary software, so I have the data "in hand" - what the titles and hierarchy are, and what the filenames are each one should link to. This proprietary software does not have the capability to generate the desired bookmarks in the topic PDFs - it can only generate internal links to items within the current PDF document -- but it does have the ability to call a Java application, so that is where I want to use PDFbox, to post-process the topic-PDF output and add the bookmarks.
>
> I've  been browsing through the Javadoc, and I'm pretty sure I need to be working with PDOutlineNode and PDOutlineItem. I can see methods which sound like they could be used to add more items and sub-nodes to the tree, but I'm not seeing how to link the tree into the document to begin with.  There is not much in the way of example code or cookbook-type references, and what I do find is 90% about reading PDFs and very little about modifying/augmenting them. So, I could really use some help knowing how to get started.
>
> I could create these topic PDFs with no bookmarks at all, or generate 
> them with one bookmark to the top of page 1, whichever is easier to 
> work with. (I'm thinking it may be easier if I have a sort of 
> placeholder to start with.)
>
> Thanks for any help,
> Gary
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org