pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Brockett <NBrock...@datasys.co.uk>
Subject PDDocumentOutline Help
Date Wed, 21 Oct 2015 13:00:29 GMT
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();

                }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message