Bookmark examples

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

31st May 2016
admin-marketing

Switch code for this example

FetchBookmarkTitles.java
/*

    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 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 kids = outline.getAllChildren();
        if (kids != null) {
            for (PdfOutline kid : kids) {
                showTitle(kid, stringBuffer);
            }
        }
    }
}
ChangeBookmarks.java
/*

    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 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 children = outlines.getAllChildren().get(0).getAllChildren();
        changeList(children);
        pdfDoc.close();
    }
}
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