Tuesday, December 4, 2007

Getting started with iText - Part 7 (HelloWorldMultiple)

This next installation in Getting started with iText was supposed to translate the HelloWorldMultiple.java example. It demonstrates how to generate multiple files using a single Document object.

While the attached CF code runs, it produces binary garbage for everything but the PDF file and I am not certain why. I will have to come back to this one and try and determine the source of the problem: the iText.jar version or my interpretation of the code.

I think a better title for this would be "GoodbyeHelloWorldMultiple"

    savedErrorMessage = "";
    // cfSearching: By default files are output to the current directory
    fullPathToPDFFile = ExpandPath("./HelloWorldPdf.pdf");
    fullPathToRTFFile = ExpandPath("./HelloWorldRtf.rtf");
    fullPathToHTMLFile = ExpandPath("./HelloWorldHtml.html");

    // step 1: creation of a document-object
    document = createObject("java", "com.lowagie.text.Document").init();

    try {
        // step 2:
        // we create 3 different writers that listen to the document
        // cfSearching: First create the reusable stream an writer objects
        outStream = createObject("java", "java.io.FileOutputStream");
        writer = createObject("java", "com.lowagie.text.pdf.PdfWriter");

        // cfSearching: Now Create the 3 writers
        pdf = writer.getInstance(document, outStream.init(fullPathToPDFFile));
        rtf = writer.getInstance(document, outStream.init(fullPathToRTFFile));
        html = writer.getInstance(document, outStream.init(fullPathToHTMLFile));
        // step 3: we open the document

        // step 4: we add a paragraph to the document
        paragraph = createObject("java", "com.lowagie.text.Paragraph").init("Hello World");
        // we make references
        // cfSearching: create Anchor object and reuse it
        anchor = createObject("java", "com.lowagie.text.Anchor");
        pdfRef = anchor.init("see Hello World in PDF.");
        pdfRef.setReference("./"& fullPathToPDFFile);
        rtfRef = anchor.init("see Hello World in RTF.");
        rtfRef.setReference("./"& fullPathToRTFFile);
        // we add the references, but only to the HTML page:
        chunk = createObject("java", "com.lowagie.text.Chunk");
    catch (com.lowagie.text.DocumentException de) {
        savedErrorMessage = de;
    catch (java.io.IOException ioe) {
        savedErrorMessage = ioe;
    // step 5: we close the document
    // close the output streams

<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
    Error - unable to create document
    <cfdump var="#savedErrorMessage#">


  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep