Bookmark examples

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

FetchBookmarkTitles.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/28634172/java-reading-pdf-bookmark-names-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.interactive;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfOutline;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.Assert;
  20. import org.junit.Test;
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.IOException;
  24. import java.util.List;
  25.  
  26. @Category(SampleTest.class)
  27. public class FetchBookmarkTitles {
  28.     public static final String SRC = "./src/test/resources/pdfs/bookmarks.pdf";
  29.     public static final String RESULT = "2011-10-12\n" +
  30.             "2011-10-13\n" +
  31.             "2011-10-14\n" +
  32.             "2011-10-15\n" +
  33.             "2011-10-16\n" +
  34.             "2011-10-17\n" +
  35.             "2011-10-18\n" +
  36.             "2011-10-19\n";
  37.  
  38.     @Test
  39.     public void manipulatePdf() throws IOException {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
  41.  
  42.         PdfOutline outlines = pdfDoc.getOutlines(false);
  43.         List bookmarks = outlines.getAllChildren().get(0).getAllChildren();
  44.         StringBuffer stringBuffer = new StringBuffer();
  45.         for (PdfOutline bookmark : bookmarks) {
  46.             showTitle(bookmark, stringBuffer);
  47.         }
  48.         pdfDoc.close();
  49.  
  50.         System.out.println(stringBuffer.toString());
  51.  
  52.         Assert.assertArrayEquals(RESULT.getBytes(), stringBuffer.toString().getBytes());
  53.     }
  54.  
  55.     public void showTitle(PdfOutline outline, StringBuffer stringBuffer) {
  56.         System.out.println(outline.getTitle());
  57.         stringBuffer.append(outline.getTitle() + "\n");
  58.         List kids = outline.getAllChildren();
  59.         if (kids != null) {
  60.             for (PdfOutline kid : kids) {
  61.                 showTitle(kid, stringBuffer);
  62.             }
  63.         }
  64.     }
  65. }
ChangeBookmarks.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/24217657/set-inherit-zoomaction-property-to-bookmark-in-the-pdf-file
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21. import java.util.List;
  22.  
  23. @Category(SampleTest.class)
  24. public class ChangeBookmarks extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/stamper/change_bookmarks.pdf";
  26.     public static final String SRC = "./src/test/resources/pdfs/bookmarks.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new ChangeBookmarks().manipulatePdf(DEST);
  32.     }
  33.  
  34.     public void changeList(List list) {
  35.         for (PdfOutline entry : list) {
  36.             PdfArray array = ((PdfArray) entry.getContent().get(PdfName.Dest));
  37.             for (int i = 0; i < array.size(); i++) {
  38.                 if (PdfName.Fit.equals(array.get(i))) {
  39.                     // one should be more cautious but since we know the source file...
  40.                     array.set(i, PdfName.FitV);
  41.                     array.add(new PdfNumber(60));
  42.                 }
  43.             }
  44.         }
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  50.         PdfOutline outlines = pdfDoc.getOutlines(false);
  51.         List children = outlines.getAllChildren().get(0).getAllChildren();
  52.         changeList(children);
  53.         pdfDoc.close();
  54.     }
  55. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now