Gnostice PDFOne Java
Std. Ed. v2.10


com.gnostice.pdfone
Interface PdfBookmarkMergeHandler


public interface PdfBookmarkMergeHandler

This interface defines a run-time event generated by overloaded PdfDocument.merge() methods.

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.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
{
    static int mergedDocumentCount = 1;

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

    static void main() 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 = (PdfPage) page1.clone();
        PdfPage page3 = (PdfPage) page1.clone();
        PdfPage page4 = (PdfPage) page1.clone();
        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);
        doc1.writeText(
              "This is page# <% pageno %> of bookmarks_doc1.pdf.",
              "1-4");
        doc1.setOpenAfterSave(false);
        doc1.write();
        writer1.dispose();

        // Create another document with 4 pages with a
        // bookmark for each page
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        File file = new File("bookmarks_doc2.pdf");
        PdfWriter writer2 = PdfWriter.memoryWriter(baos);
        PdfDocument doc2 = new PdfDocument(writer2);
        PdfPage page5 = new PdfPage();
        PdfPage page6 = (PdfPage) page5.clone();
        PdfPage page7 = (PdfPage) page5.clone();
        PdfPage page8 = (PdfPage) page5.clone();
        doc2.add(page5);
        doc2.add(page6);
        doc2.add(page7);
        doc2.add(page8);
        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.addBookmark(
              "Page 4 of bookmarks_doc2.pdf", 
              doc2.getBookmarkRoot(), 
              4);
        doc2.writeText(
              "This is page# <% pageno %> of bookmarks_doc2.pdf.",
              "1-4");
        doc2.setOpenAfterSave(false);
        doc2.write();
        byte[] ba = baos.toByteArray();
        BufferedOutputStream bos = new BufferedOutputStream(
            new FileOutputStream(file));
        bos.write(ba);
        bos.close();
        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)
    {
        mergedDocumentCount++;
        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

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 Java
Std. Ed. v2.10

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