Sunday, December 2, 2007

Getting started with iText - Part 4 (CustomPageSize.java)

Update: This example was originally switched with DefaultPageSize.java. It is now updated for the archives

The next installation in Getting started with iText translates CustomPageSize.java. That example shows how to generate a document with a custom page size and background color.

Source: http://itextdocs.lowagie.com/tutorial/general/
Documentation: See Step 1 for more information
Example: CustomPageSize.java


<h1>Custom PageSize and BackgroundColor Example</h1>
<cfscript>
    savedErrorMessage = "";
    
    // by default outputs to current directory. change as needed
    fullPathToOutputFile = ExpandPath("./CustomPageSize.pdf");
    
    // step 1: creation of a document-object
    // cfSearching: The com.lowagie.text.Rectangle constructor that accepts primitive floats
    pageSize = createObject("java", "com.lowagie.text.Rectangle").init(
        javacast("float", 216),
        javacast("float", 720)
    );
    // cfSearching: Using the java.awt.Color constructor that accepts primitive integers
    // cfSearching: InputBaseN(string, 16) converts a hexadecimal value to decimal
    pageColor = createObject("java", "java.awt.Color").init(
     javacast("int", InputBaseN("0xFF", 16)),
     javacast("int", InputBaseN("0xFF", 16)),
     javacast("int", InputBaseN("0xDE", 16))
     );

    pageSize.setBackgroundColor (pageColor);
    document = createObject("java", "com.lowagie.text.Document").init(pageSize);

    try {
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
        writer = createObject("java", "com.lowagie.text.pdf.PdfWriter").getInstance(document, outStream);

        // step 3: we open the document
        document.open();
            
        // step 4: we add some paragraphs to the document
        //cfSearching: create a single paragraph object and reuse it
        paragraph = createObject("java", "com.lowagie.text.Paragraph");

        document.add(paragraph.init("The size of this page is 216x720 points."));
        document.add(paragraph.init("216pt / 72 points per inch = 3 inch"));
        document.add(paragraph.init("720pt / 72 points per inch = 10 inch"));
        document.add(paragraph.init("The size of this page is 3x10 inch."));
        document.add(paragraph.init("3 inch x 2.54 = 7.62 cm"));
        document.add(paragraph.init("10 inch x 2.54 = 25.4 cm"));
        document.add(paragraph.init("The size of this page is 7.62x25.4 cm."));
        //cfSearching: add extra ## sign to prevent CF error
        document.add(paragraph.init("The backgroundcolor of the Rectangle used for this PageSize, is ##FFFFDE."));
        document.add(paragraph.init("That's why the background of this document is yellowish..."));
        
        WriteOutput("Finished!");
    }
    catch (com.lowagie.text.DocumentException de) {
        savedErrorMessage = de;
    }
    catch (java.io.IOException ioe) {
        savedErrorMessage = ioe;
    }
    catch (coldfusion.runtime.Cast$NumberConversionException nce) {
        savedErrorMessage = nce;
    }
    
    // step 5: we close the document
    document.close();
    // close the output stream
    outStream.close();
</cfscript>

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

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep