Saturday, December 22, 2007

Getting started with iText - Part 17 (Concatenating PDF Files)

In Part 17, we translate the Concatenate.java example. As usual the title is a spoiler: the example demonstrates how to concatenate (or merge) multiple PDF files.


You can run this example with MX7 or CF8. Theoretically it should work with MX6 as well if you have installed an iText jar in the CF classpath. It is worth noting here that ColdFusion 8 provides the cfpdf tag which can merge PDF files. I have included a ColdFusion 8 example for comparison purposes.

What you will need for this example


Download the following (3) files from the iText site and place them in the same directory as your .cfm script


Code


Documentation: Manipulating existing PDF documents
Source: Concatenate.java

iText Example

<h1>Concatenate (Merge) PDF files example</h1>
<cfscript>
savedErrorMessage = "";

// cfSearching: All file paths are relative to the current directory
fullPathToOutputFile = ExpandPath("./Concatenated.pdf");
arrayOfInputFiles = arrayNew(1);
arrayAppend(arrayOfInputFiles, ExpandPath("./ChapterSection.pdf"));
arrayAppend(arrayOfInputFiles, ExpandPath("./Destinations.pdf"));
arrayAppend(arrayOfInputFiles, ExpandPath("./SimpleAnnotations1.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");

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 (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>


<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
Error. Unable to create file
<cfdump var="#savedErrorMessage#">
</cfif>


ColdFusion 8 example

<h1>ColdFusion 8 Merge PDF files example</h1>

<cfset listOfInputFiles = ExpandPath("./ChapterSection.pdf")>
<cfset listOfInputFiles = listAppend(listOfInputFiles, ExpandPath("./Destinations.pdf"))>
<cfset listOfInputFiles = listAppend(listOfInputFiles, ExpandPath("./SimpleAnnotations1.pdf"))>
<cfset fullPathToOutputFile = ExpandPath("./Concatenated.pdf")>

<cfpdf action="merge"
source="#listOfInputFiles#"
destination="#fullPathToOutputFile#"
keepbookmark="true"
overwrite="true">

Finished!

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep