Bookmark examples

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

24th October 2015
admin-marketing

Switch code for this example

FetchBookmarkTitles.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/28634172/java-reading-pdf-bookmark-names-with-itext
  4.  */
  5. package sandbox.interactive;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.SimpleBookmark;
  10.  
  11. import java.io.IOException;
  12. import java.util.HashMap;
  13. import java.util.List;
  14.  
  15. public class FetchBookmarkTitles {
  16.     public static final String SRC = "resources/pdfs/bookmarks.pdf";
  17.    
  18.     public static void main(String[] args) throws IOException, DocumentException {
  19.         new FetchBookmarkTitles().inspectPdf(SRC);
  20.     }
  21.    
  22.     public void inspectPdf(String filename) throws IOException, DocumentException {
  23.         PdfReader reader = new PdfReader(filename);
  24.         List bookmarks = SimpleBookmark.getBookmark(reader);
  25.         for (int i = 0; i < bookmarks.size(); i++){
  26.             showTitle(bookmarks.get(i));
  27.         }
  28.         reader.close();
  29.     }
  30.    
  31.     public void showTitle(HashMap bm) {
  32.         System.out.println((String)bm.get("Title"));
  33.         List kids = (List)bm.get("Kids");
  34.         if (kids != null) {
  35.             for (int i = 0; i < kids.size(); i++) {
  36.                 showTitle(kids.get(i));
  37.             }
  38.         }
  39.     }
  40. }
ChangeBookmarks.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24217657/set-inherit-zoomaction-property-to-bookmark-in-the-pdf-file
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10. import com.itextpdf.text.pdf.SimpleBookmark;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class ChangeBookmarks {
  21.  
  22.     public static final String SRC = "resources/pdfs/bookmarks.pdf";
  23.     public static final String DEST = "results/stamper/changed_bookmarks.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new ChangeBookmarks().manipulatePdf(SRC, DEST);
  29.     }
  30.  
  31.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  32.         PdfReader reader = new PdfReader(src);
  33.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  34.         List list = SimpleBookmark.getBookmark(reader);
  35.         changeList(list);
  36.         stamper.setOutlines(list);
  37.         stamper.close();
  38.         reader.close();
  39.     }
  40.    
  41.     public void changeList(List list) {
  42.         for (HashMap entry : list) {
  43.             for (String key : entry.keySet()) {
  44.                 if ("Kids".equals(key)) {
  45.                     Object o = entry.get(key);
  46.                     changeList((List)o);
  47.                 }
  48.                 else if ("Page".equals(key)) {
  49.                     String dest = (String)entry.get(key);
  50.                     entry.put("Page", dest.replaceAll("Fit", "FitV 60"));
  51.                 }
  52.             }
  53.         }
  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