Gnostice PDFOne
Pro. Ed. v5.0.0


com.gnostice.pdfone
Interface PdfBookmarkMergeHandler


public interface PdfBookmarkMergeHandler

This interface defines a run-time event that occurs when a document is merged by an overloaded PdfDocument.merge() method.

By handling this event, user classes can choose to place all bookmarks from the other document placed as child bookmarks under a new bookmark.

The new bookmark is exposed by the event handler and its properties can be modified by an implementing user class. The new bookmark is placed after all existing bookmarks of this document.

If the event is not handled, the bookmarks from the other document will be placed under the root bookmark of this document.

import java.io.File;
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
  PdfWriter writer1 = PdfWriter.fileWriter(new File(
                        "bookmarks_doc1.pdf"));
  PdfDocument doc1 = new PdfDocument(writer1);
  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.write();
  writer1.dispose();

  // Create another document with 4 pages with a
  // bookmark for each page
  PdfWriter writer2 = PdfWriter.fileWriter(new File(
                          "bookmarks_doc2.pdf"));
  PdfDocument doc2 = new PdfDocument(writer2);

  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.write();
  writer2.dispose();

  // Read the first document
  PdfReader reader1 = PdfReader.fileReader(
     "bookmarks_doc1.pdf", "bookmarks_doc3.pdf");
  PdfDocument doc3 = new PdfDocument(reader1);

  // 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.write();
  reader1.dispose();
 }

 // 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);
 }
}


Method Summary
 void onBookmarkMerge(PdfDocument d, PdfBookmark b)
          Called by an overloaded PdfDocument.merge() method when it tries to merge document d with current document.
 

Method Detail

onBookmarkMerge

public void onBookmarkMerge(PdfDocument d,
                            PdfBookmark b)
Called by an overloaded PdfDocument.merge() method when it tries to merge document d with current document. User classes handling this event will place all bookmarks from document d under a new bookmark b.

Parameters:
d - the other document that is being merged
b - new bookmark under which all bookmarks from the other document will be placed

Gnostice PDFOne
Pro. Ed. v5.0.0

To contact our support team, send an e-mail to support@gnostice.com.
 
© 2010 Gnostice Information Technologies Private Limited. All rights reserved.
www.gnostice.com