Skip to main content
Skip table of contents

Bookmark examples

Examples written in answer to questions such as:


fetchbookmarktitles

JAVA

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<HashMap<String,Object>> bookmarks = SimpleBookmark.getBookmark(reader);
        for (int i = 0; i < bookmarks.size(); i++){
            showTitle(bookmarks.get(i));
        }
        reader.close();
    }
    
    public void showTitle(HashMap<String, Object> bm) {
        System.out.println((String)bm.get("Title"));
        List<HashMap<String,Object>> kids = (List<HashMap<String,Object>>)bm.get("Kids");
        if (kids != null) {
            for (int i = 0; i < kids.size(); i++) {
                showTitle(kids.get(i));
            }
        }
    }
}


changebookmarks

JAVA

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

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.