Bookmark examples

Tags: Bookmarksinspect PDFPdfReaderiText 7

Examples written in answer to questions such as:

Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.sandbox.interactive;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfOutline;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
 
import java.io.IOException;
import java.util.List;
 
@Category(SampleTest.class)
public class FetchBookmarkTitles {
    public static final String SRC = "./src/test/resources/pdfs/bookmarks.pdf";
    public static final String RESULT = "2011-10-12\n" +
            "2011-10-13\n" +
            "2011-10-14\n" +
            "2011-10-15\n" +
            "2011-10-16\n" +
            "2011-10-17\n" +
            "2011-10-18\n" +
            "2011-10-19\n";
 
    @Test
    public void manipulatePdf() throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
 
        PdfOutline outlines = pdfDoc.getOutlines(false);
        List<PdfOutline> bookmarks = outlines.getAllChildren().get(0).getAllChildren();
        StringBuffer stringBuffer = new StringBuffer();
        for (PdfOutline bookmark : bookmarks) {
            showTitle(bookmark, stringBuffer);
        }
        pdfDoc.close();
 
        System.out.println(stringBuffer.toString());
 
        Assert.assertArrayEquals(RESULT.getBytes(), stringBuffer.toString().getBytes());
    }
 
    public void showTitle(PdfOutline outline, StringBuffer stringBuffer) {
        System.out.println(outline.getTitle());
        stringBuffer.append(outline.getTitle() + "\n");
        List<PdfOutline> kids = outline.getAllChildren();
        if (kids != null) {
            for (PdfOutline kid : kids) {
                showTitle(kid, stringBuffer);
            }
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24217657/set-inherit-zoomaction-property-to-bookmark-in-the-pdf-file
 */
package com.itextpdf.samples.sandbox.stamper;
 
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.util.List;
 
@Category(SampleTest.class)
public class ChangeBookmarks extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/stamper/change_bookmarks.pdf";
    public static final String SRC = "./src/test/resources/pdfs/bookmarks.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ChangeBookmarks().manipulatePdf(DEST);
    }
 
    public void changeList(List<PdfOutline> list) {
        for (PdfOutline entry : list) {
            PdfArray array = ((PdfArray) entry.getContent().get(PdfName.Dest));
            for (int i = 0; i < array.size(); i++) {
                if (PdfName.Fit.equals(array.get(i))) {
                    // one should be more cautious but since we know the source file...
                    array.set(i, PdfName.FitV);
                    array.add(new PdfNumber(60));
                }
            }
        }
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
        PdfOutline outlines = pdfDoc.getOutlines(false);
        List<PdfOutline> children = outlines.getAllChildren().get(0).getAllChildren();
        changeList(children);
        pdfDoc.close();
    }
}
File nameRaw URLUpdated
FetchBookmarkTitles.javaFetchBookmarkTitles.java2016-08-12 2:40 pm
ChangeBookmarks.javaChangeBookmarks.java2016-08-12 2:41 pm
Resources: 
File nameRaw URLUpdated
bookmarks.pdfbookmarks.pdf2016-08-11 11:10 am
Results: 
File nameRaw URLUpdated
cmp_change_bookmarks.pdfcmp_change_bookmarks.pdf2016-08-12 2:46 pm