Bookmark examples

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

24th October 2015
admin-marketing

Switch code for this example

FetchBookmarkTitles.java
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/28634172/java-reading-pdf-bookmark-names-with-itext
 */
package sandbox.interactive;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.SimpleBookmark;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class FetchBookmarkTitles {
    public static final String SRC = "resources/pdfs/bookmarks.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        new FetchBookmarkTitles().inspectPdf(SRC);
    }
    
    public void inspectPdf(String filename) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(filename);
        List> bookmarks = SimpleBookmark.getBookmark(reader);
        for (int i = 0; i < bookmarks.size(); i++){
            showTitle(bookmarks.get(i));
        }
        reader.close();
    }
    
    public void showTitle(HashMap bm) {
        System.out.println((String)bm.get("Title"));
        List> kids = (List>)bm.get("Kids");
        if (kids != null) {
            for (int i = 0; i < kids.size(); i++) {
                showTitle(kids.get(i));
            }
        }
    }
}
ChangeBookmarks.java
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24217657/set-inherit-zoomaction-property-to-bookmark-in-the-pdf-file
 */
package sandbox.stamper;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.SimpleBookmark;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import sandbox.WrapToTest;

@WrapToTest
public class ChangeBookmarks {

    public static final String SRC = "resources/pdfs/bookmarks.pdf";
    public static final String DEST = "results/stamper/changed_bookmarks.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ChangeBookmarks().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        List> list = SimpleBookmark.getBookmark(reader);
        changeList(list);
        stamper.setOutlines(list);
        stamper.close();
        reader.close();
    }
    
    public void changeList(List> list) {
    	for (HashMap entry : list) {
    		for (String key : entry.keySet()) {
    			if ("Kids".equals(key)) {
    				Object o = entry.get(key);
    				changeList((List>)o);
    			}
    			else if ("Page".equals(key)) {
    				String dest = (String)entry.get(key);
    				entry.put("Page", dest.replaceAll("Fit", "FitV 60"));
    			}
    		}
    	}
    }
}
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