Hi, Gesendet: Mo, 25. Okt 2010 Von: Hesham G.<[email protected]>
> Okay ... Here you go a sample code : > > I brought a PDF file that has no bookmarks at all: > http://www.4shared.com/document/YhsIX4Fg/pdf_with_no_bookmarks.html > Then using the following code i defined 3 bookmarks nodes to it. I then > opened the output PDF in windows Adobe reader & the bookmarks worked fine, > then I tried it on Mac's "Preview" program, but the bookmarks do not > appear. > > Here is the output PDF: > http://www.4shared.com/document/wK8SzJsW/pdf_after_adding_bookmarks.html > > My code : > * > > private void noBookmarksButtonActionPerformed() { > List<PDPage> allPages = new ArrayList<PDPage>(); > > // Load the PDF bookmarks in the From list : > try { > String pdfPath = "C:\\pdf_with_no_bookmarks.pdf"; > PDDocument pdfFile = PDDocument.load( pdfPath ); > PDDocumentOutline pdfBookmarks = new > PDDocumentOutline(); // Create a > new bookmarks tree. > pdfFile.getDocumentCatalog().getPages().getAllKids( > allPages ); // All > kids will be added to allPages. > > // Initialize some bookmarks : > PDOutlineItem bookmark1 = new PDOutlineItem(); > PDOutlineItem bookmark2 = new PDOutlineItem(); > PDOutlineItem bookmark3 = new PDOutlineItem(); > > // Define the bookmarks properties : > bookmark1.setTitle( "Go to page 3" ); > bookmark1.setDestination( allPages.get( 2 ) ); > This is only a guess, but the default implementation within "setDestination(PDPage page)" leads to the following entry in the pdf: /Title (Go to page 20) /Dest [37 0 R /XYZ null null null] /Parent 6 0 R /Prev 16 0 R /Next 17 0 R Probably the acrobat reader is more lenient than apples preview about the "null" values within the "Dest" array. Try something like this: PDPageXYZDestination dest = new PDPageXYZDestination(); dest.setPage( allPages.get( 2 ) ); dest.setLeft(0); dest.setTop(0); dest.setZoom(0); bookmark1.setDestination( dest ); BR Andreas Lehmkühler

