iText in Action: Chapter 7: Making documents interactive

part2.chapter07.ConcatenateBookmarks

If you compile and execute this example, you'll get the following result:

You can download the full source code of ConcatenateBookmarks, or read it here:

/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */
 
package part2.chapter07;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import part1.chapter02.MovieHistory;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.SimpleBookmark;
 
public class ConcatenateBookmarks {
 
    /** The resulting PDF. */
    public static final String RESULT
        = "results/part2/chapter07/concatenated_bookmarks.pdf";
 
    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void manipulatePdf(String[] src, String dest)
        throws IOException, DocumentException {
    	// step 1
        Document document = new Document();
        // step 2
        PdfCopy copy
            = new PdfCopy(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        PdfReader reader;
        int page_offset = 0;
        int n;
        // Create a list for the bookmarks
        ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
        List<HashMap<String, Object>> tmp;
        for (int i  = 0; i < src.length; i++) {
            reader = new PdfReader(src[i]);
            // merge the bookmarks
            tmp = SimpleBookmark.getBookmark(reader);
            SimpleBookmark.shiftPageNumbers(tmp, page_offset, null);
            bookmarks.addAll(tmp);
            // add the pages
            n = reader.getNumberOfPages();
            page_offset += n;
            for (int page = 0; page < n; ) {
                copy.addPage(copy.getImportedPage(reader, ++page));
            }
            copy.freeReader(reader);
            reader.close();
        }
        // Add the merged bookmarks
        copy.setOutlines(bookmarks);
        // step 5
        document.close();
    }
 
    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args)
        throws IOException, DocumentException, SQLException {
        BookmarkedTimeTable.main(args);
        MovieHistory.main(args);
        new ConcatenateBookmarks().manipulatePdf(
            new String[]{BookmarkedTimeTable.RESULT, MovieHistory.RESULT},
            RESULT);
    }
}
 
Powered by GeSHi

The ConcatenateBookmarks example is part of the book iText in Action (ISBN 9781935182610).

It's a small standalone application. You can use this example for inspiration, but please read the book if there's something you don't understand about the example. You'll find more info in Chapter 7. Note that this example depends on MovieHistory, and maybe some other examples.

Keywords for this example: Bookmarks, Concatenate PDFs, Existing PDFs, Outline tree, PdfCopy, PdfReader, Concatenate PDFs > concatenate bookmarks

If you want this example to work, you need the following jars: iText.jar, hsqldb.jar

This example uses the following resources: filmfestival.script.