You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Nick Brockett <NB...@datasys.co.uk> on 2015/10/21 15:00:29 UTC

PDDocumentOutline Help

Hi
Please can somebody explain why the following code executes without error, but the resultant PDF has nothing added or appended to it.
I am of course expecting to have a list of page links.
Thanks in anticipation of help.

   static void addIndex(String filename) throws IOException {

       PDDocument masterDoc = null;

                try {

                                File masterFile = new File(filename);
                                masterDoc = PDDocument.load(masterFile);
                                if( masterDoc.isEncrypted() )
                                {
                                    System.err.println( "Error: Cannot add bookmarks to encrypted document." );
                                    System.exit( 1 );
                                }

                                                PDDocumentOutline outline =  new PDDocumentOutline();
                                                masterDoc.getDocumentCatalog().setDocumentOutline( outline );
                                                PDOutlineItem pagesOutline = new PDOutlineItem();
                                                pagesOutline.setTitle( "All Pages" );
                                                outline.appendChild( pagesOutline );
                                                @SuppressWarnings("unchecked")
                                                List<PDPage> pages = masterDoc.getDocumentCatalog().getAllPages();
                                     for( int i=0; i<pages.size(); i++ )
                                     {
                                         PDPage page = (PDPage)pages.get( i );
                                         PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
                                         dest.setPage( page );
                                         PDOutlineItem bookmark = new PDOutlineItem();
                                         bookmark.setDestination( dest );
                                        bookmark.setTitle( "Page " + (i+1) );
                                         pagesOutline.appendChild( bookmark );
                                     }
                                     pagesOutline.openNode();
                                     outline.openNode();
                                                masterDoc.save(filename);

                                } catch (COSVisitorException e) {

                                                e.printStackTrace();
                                }
                finally {
                                masterDoc.close();

                }
}

RE: PDDocumentOutline Help

Posted by Nick Brockett <NB...@datasys.co.uk>.
Hi Tilman

Many thanks for the reply.
Shortly after posting I discovered shall we say my ignorance.
I had confused "bookmarks" with what I thought were a listing of page contents.
Your time to reply is appreciated. Yes, I can see that I have created bookmarks.
What I really was after was an initial page (as part of the document) with links to navigate to pages.
Since posting I have achieved this below (snippet)
PDPage target = (PDPage) masterDoc.getDocumentCatalog().getAllPages().get(0);
              PDPageDestination dest = new PDPageFitWidthDestination();
              dest.setPage(target);

              PDActionGoTo action = new PDActionGoTo();
              action.setDestination(dest);

              PDAnnotationLink link = new PDAnnotationLink();
              link.setAction(action);
              link.setDestination(dest);

              PDRectangle rect = new PDRectangle();
              // just making these x,y coords up for sample

              rect.setLowerLeftX(72);
              rect.setLowerLeftY(600);
              rect.setUpperRightX(144);
              rect.setUpperRightY(620);
              link.setRectangle(rect);
              page.getAnnotations().add(link);


So I am ok for now.
Cheers
Nick
From: Tilman Hausherr [mailto:THausherr@t-online.de]
Sent: 21 October 2015 19:53
To: users@pdfbox.apache.org
Subject: Re: PDDocumentOutline Help

Your code works fine with 1.8.11. (Should work with 1.8.10 too) What version did you use? Here's what I get:

[cid:image001.png@01D10FC5.3FBFBF80]

Tilman

Am 21.10.2015 um 15:00 schrieb Nick Brockett:

Hi

Please can somebody explain why the following code executes without error, but the resultant PDF has nothing added or appended to it.

I am of course expecting to have a list of page links.

Thanks in anticipation of help.



   static void addIndex(String filename) throws IOException {



       PDDocument masterDoc = null;



                try {



                                File masterFile = new File(filename);

                                masterDoc = PDDocument.load(masterFile);

                                if( masterDoc.isEncrypted() )

                                {

                                    System.err.println( "Error: Cannot add bookmarks to encrypted document." );

                                    System.exit( 1 );

                                }



                                                PDDocumentOutline outline =  new PDDocumentOutline();

                                                masterDoc.getDocumentCatalog().setDocumentOutline( outline );

                                                PDOutlineItem pagesOutline = new PDOutlineItem();

                                                pagesOutline.setTitle( "All Pages" );

                                                outline.appendChild( pagesOutline );

                                                @SuppressWarnings("unchecked")

                                                List<PDPage> pages = masterDoc.getDocumentCatalog().getAllPages();

                                     for( int i=0; i<pages.size(); i++ )

                                     {

                                         PDPage page = (PDPage)pages.get( i );

                                         PDPageFitWidthDestination dest = new PDPageFitWidthDestination();

                                         dest.setPage( page );

                                         PDOutlineItem bookmark = new PDOutlineItem();

                                         bookmark.setDestination( dest );

                                        bookmark.setTitle( "Page " + (i+1) );

                                         pagesOutline.appendChild( bookmark );

                                     }

                                     pagesOutline.openNode();

                                     outline.openNode();

                                                masterDoc.save(filename);



                                } catch (COSVisitorException e) {



                                                e.printStackTrace();

                                }

                finally {

                                masterDoc.close();



                }

}




Re: PDDocumentOutline Help

Posted by Tilman Hausherr <TH...@t-online.de>.
Your code works fine with 1.8.11. (Should work with 1.8.10 too) What 
version did you use? Here's what I get:



Tilman

Am 21.10.2015 um 15:00 schrieb Nick Brockett:
> Hi
> Please can somebody explain why the following code executes without error, but the resultant PDF has nothing added or appended to it.
> I am of course expecting to have a list of page links.
> Thanks in anticipation of help.
>
>     static void addIndex(String filename) throws IOException {
>
>         PDDocument masterDoc = null;
>
>                  try {
>
>                                  File masterFile = new File(filename);
>                                  masterDoc = PDDocument.load(masterFile);
>                                  if( masterDoc.isEncrypted() )
>                                  {
>                                      System.err.println( "Error: Cannot add bookmarks to encrypted document." );
>                                      System.exit( 1 );
>                                  }
>
>                                                  PDDocumentOutline outline =  new PDDocumentOutline();
>                                                  masterDoc.getDocumentCatalog().setDocumentOutline( outline );
>                                                  PDOutlineItem pagesOutline = new PDOutlineItem();
>                                                  pagesOutline.setTitle( "All Pages" );
>                                                  outline.appendChild( pagesOutline );
>                                                  @SuppressWarnings("unchecked")
>                                                  List<PDPage> pages = masterDoc.getDocumentCatalog().getAllPages();
>                                       for( int i=0; i<pages.size(); i++ )
>                                       {
>                                           PDPage page = (PDPage)pages.get( i );
>                                           PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
>                                           dest.setPage( page );
>                                           PDOutlineItem bookmark = new PDOutlineItem();
>                                           bookmark.setDestination( dest );
>                                          bookmark.setTitle( "Page " + (i+1) );
>                                           pagesOutline.appendChild( bookmark );
>                                       }
>                                       pagesOutline.openNode();
>                                       outline.openNode();
>                                                  masterDoc.save(filename);
>
>                                  } catch (COSVisitorException e) {
>
>                                                  e.printStackTrace();
>                                  }
>                  finally {
>                                  masterDoc.close();
>
>                  }
> }
>