public interface PdfBookmarkMergeHandler
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 number 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);
}
}
| Modifier and Type | Method and Description |
|---|---|
void |
onBookmarkMerge(PdfDocument d,
PdfBookmark b)
Called by an overloaded
PdfDocument.merge()
method when it tries to merge document d with
current document. |
void onBookmarkMerge(PdfDocument d, PdfBookmark b)
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.d - the other document that is being mergedb - new bookmark under which all bookmarks from the
other document will be placed