PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

Merge PDF Documents and Create Bookmarks for Each Document

Create bookmarks that tell which bookmark belongs to which document.
By Santhanam L.

PDFOne Java has an interface PdfBookmarkMergeHandler that was introduced specifically for creating bookmarks identifying merged documents.

When a PDF document is merged with another document, bookmarks from both documents are placed in the same level. However, some users might want to retain some form of identification for bookmarks that came from another document.

The PdfBookmarkMergeHandler interface defines a run-time event (onBookmarkMerge(PdfDocument d, PdfBookmark b)) that occurs when a document is merged by the PdfDocument.merge() method.

If the user class that implements the interface modifies the bookmark b, then

  1. b will be added after all bookmarks in the current document, and
  2. all bookmarks from the other document (which is being merged with the current document) will be placed as child bookmarks under b.

If the user class does not implement the interface, handle the event, or modify b, then bookmarks from the other document will be simply added after the bookmarks in the current document.

A sample user class that implements PdfBookmarkMergeHandler is listed below.

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfBookmark;
import com.gnostice.pdfone.PdfBookmarkMergeHandler;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfWriter;

public class PdfBookmarkMergeHandler_Example 
  implements PdfBookmarkMergeHandler
{
 // Document count - incremented each time 
 // onBookmarkMerge() event is called
 static int mergedDocumentCount = 1;

 static
 {
  PDFOne.activate(
     "T95VZE:W8HBPVA:74VQ8QV:LO4V8",
     "9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
 }

 public static void main(String[] args) 
    throws IOException, PdfException
 {
  // Create a document with 4 pages with a
  // bookmark for each page
  PdfDocument doc1 = new PdfDocument();
  PdfPage page1 = new PdfPage();
  PdfPage page2 = new PdfPage();
  PdfPage page3 = new PdfPage();
  PdfPage page4 = new PdfPage();
  doc1.add(page1);
  doc1.add(page2);
  doc1.add(page3);
  doc1.add(page4);
  doc1.addBookmark(
    "Page 1 of bookmarks_doc1.pdf", 
    doc1 .getBookmarkRoot(), 
    1);
  doc1.addBookmark(
    "Page 2 of bookmarks_doc1.pdf", 
    doc1.getBookmarkRoot(), 
    2);
  doc1.addBookmark(
    "Page 3 of bookmarks_doc1.pdf", 
    doc1.getBookmarkRoot(), 
    3);
  doc1.addBookmark(
    "Page 4 of bookmarks_doc1.pdf", 
    doc1.getBookmarkRoot(), 
    4);
  // Write page numeber to all pages 
  doc1.writeText(
    "This is page# <% pageno %> of bookmarks_doc1.pdf.",
    "1-4"); // pageno is a pre-defined custom placeholder
  doc1.setOpenAfterSave(false);
  doc1.save("bookmarks_doc1.pdf");
  doc1.close();

  // Create another document with 4 pages with a
  // bookmark for each page
  PdfDocument doc2 = new PdfDocument();

  PdfPage page5 = new PdfPage();
  PdfPage page6 = new PdfPage();
  PdfPage page7 = new PdfPage();
  
  doc2.add(page5);
  doc2.add(page6);
  doc2.add(page7);

  doc2.addBookmark(
    "Page 1 of bookmarks_doc2.pdf", 
    doc2.getBookmarkRoot(), 
    1);
  doc2.addBookmark(
    "Page 2 of bookmarks_doc2.pdf", 
    doc2.getBookmarkRoot(), 
    2);
  doc2.addBookmark(
    "Page 3 of bookmarks_doc2.pdf", 
    doc2.getBookmarkRoot(), 
    3);
  doc2.writeText(
    "This is page# <% pageno %> of bookmarks_doc2.pdf.",
    "1-3");
  
  doc2.setOpenAfterSave(false);
  doc2.save("bookmarks_doc2.pdf");
  doc2.close();

  // Read the first document
  PdfDocument doc3 = new PdfDocument();
  doc3.load("bookmarks_doc1.pdf");

  // Specify the event handler for merging
  doc3.setOnBookmarkMerge(
     new PdfBookmarkMergeHandler_Example());
  
  // Merge the first document with the second
  doc3.merge("bookmarks_doc2.pdf");

  doc3.setOpenAfterSave(true);
  doc3.save("bookmarks_doc3.pdf");
  doc3.close();
 }

 // Place all bookmarks from the second document under a
 // new bookmark and modify the text of that bookmark
 public void onBookmarkMerge(PdfDocument d, PdfBookmark b)
 {
  b.setTitle("from document " + ++mergedDocumentCount);
 }
}

The above class creates two PDF documents with 4 pages each. Both documents will have a bookmark from each page. Then, the first document is loaded and the second document is merged with it.

As this class implements the PdfBookmarkMergeHandler interface, a special bookmark is created for the second document and all bookmarks from the second document are placed under this new bookmark.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2023 Gnostice Information Technologies Private Limited. All rights reserved.