Bookmark examples

Tags: Bookmarksinspect PDFPdfReaderiText 5

Examples written in answer to questions such as:

Files: 
/**
 * 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));
            }
        }
    }
}
/**
 * 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"));
    			}
    		}
    	}
    }
}
File nameRaw URLUpdated
FetchBookmarkTitles.javaFetchBookmarkTitles.java2015-10-24 11:08 pm
ChangeBookmarks.javaChangeBookmarks.java2015-11-08 3:43 pm
Resources: 
File nameRaw URLUpdated
bookmarks.pdfbookmarks.pdf2015-10-24 11:09 pm
Results: 
File nameRaw URLUpdated
cmp_changed_bookmarks.pdfcmp_changed_bookmarks.pdf2015-11-08 3:43 pm