Trying to add a bookmark to existing PDF using iText
I have copied code I located on cfsearching for concatenating pdfs and including any existing bookmarks ( http://cfsearching.blogspot.com/2007/12/getting-started-with-itext-part-17.html ), have purchased the iTextinAction book, but am stuck when trying to add a new bookmark. I keep getting the message '
java.util.ArrayList cannot be cast to java.util.HashMap.
I suspect I am not adding my own 'custom' bookmarks to the list of existing bookmarks correctly. I tried changing the list of existing bookmarks from an explicit vector (this was how the example was written) to an array instead, but still have this issue when I try to close my document. Following is the code I have modified. I would appreciate any help. I am not a Java programmer and am really struggling to understand how this works and how to make changes to it.
<cfscript>
savedErrorMessage = "";
// cfSearching: All file paths are relative to the current directory
fullPathToOutputFile = ExpandPath("./Concatenated.pdf");
arrayOfInputFiles = arrayNew(1);
arrayAppend(arrayOfInputFiles, ExpandPath("./Version control.pdf"));
arrayAppend(arrayOfInputFiles, ExpandPath("./iTextinAction,SecondEdition.pdf"));
try {
pageOffset = 0;
PdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader");
SimpleBookmark = createObject("java", "com.lowagie.text.pdf.SimpleBookmark");
// cfSearching: Internally CF stores arrays as Vectors. So I chose to use an explict vector
// cfSearching: here, but you could use an array and CF array functions instead
allBookmarks = createObject("java", "java.util.Vector");
outlines = arrayNew(1);
arrayAppend(outlines, 'Pirate Jack');
for ( fileIndex = 1; fileIndex LTE arrayLen(arrayOfInputFiles); fileIndex = fileIndex + 1) {
// we create a reader for a certain document
reader = pdfReader.init( arrayOfInputFiles[fileIndex] );
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
totalPages = reader.getNumberOfPages();
bookmarks = SimpleBookmark.getBookmark(reader);
if (IsDefined("bookmarks")) {
if (pageOffset neq 0) {
SimpleBookmark.shiftPageNumbers(bookmarks, javacast("int", pageOffset), javacast("null", 0));
}
allBookmarks.addAll(bookmarks);
}
pageOffset = pageOffset + totalPages;
if (fileIndex EQ 1) {
// step 1: creation of a document-object
document = createObject("java", "com.lowagie.text.Document");
document = document.init( reader.getPageSizeWithRotation( javacast("int", 1)) );
// step 2: we create a writer that listens to the document
outStream = createObject("java", "java.io.FileOutputStream").init( fullPathToOutputFile );
pdfWriter = createObject("java", "com.lowagie.text.pdf.PdfCopy").init(document, outStream);
// step 3: we open the document
document.open();
}
// step 4: we add content
for (pageIndex = 1; pageIndex LTE totalPages; pageIndex = pageIndex + 1) {
page = pdfWriter.getImportedPage(reader, javacast("int", pageIndex) );
pdfWriter.addPage(page);
}
formFields = reader.getAcroForm();
if (IsDefined("formFields")) {
pdfWriter.copyAcroForm(reader);
}
}
if (arraylen(outlines) gt 0) {
allBookmarks.addAll(outlines);
}
if (NOT allBookmarks.isEmpty()) {
pdfWriter.setOutlines( allBookmarks );
}
// step 5: we close the document
document.close();
WriteOutput("Finished!");
}
catch (java.language.Exception de) {
savedErrorMessage = de;
}
// cfSearching: close document and output stream objects
if (IsDefined("document")) {
document.close();
}
if (IsDefined("outputStream")) {
outputStream.close();
}
</cfscript>
