Sunday, December 2, 2007

Getting started with iText - Part 3 (

The next installation in Getting started with iText translates the LandscapePortrait example. This simple example shows how to change the page orientation.

Documentation: See Step 1 for more information

<h1>Documents in Landscape and Portrait format</h1>
    savedErrorMessage = "";

    // by default outputs to current directory. change as needed
    fullPathToOutputFile = ExpandPath("./LandscapePortrait.pdf");
    // step 1: creation of a document-object
    pageSize = createObject("java", "com.lowagie.text.PageSize");
    document = createObject("java", "com.lowagie.text.Document").init(PageSize.A4.rotate());
    try {
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        outStream = createObject("java", "").init(fullPathToOutputFile);
        writer = createObject("java", "com.lowagie.text.pdf.PdfWriter").getInstance(document, outStream);

        // step 3: we open the document;

        // step 4: we add some content
        //cfSearching: create a single paragraph object and reuse it
        paragraph = createObject("java", "com.lowagie.text.Paragraph");
        document.add(paragraph.init("To create a document in landscape format, just make the height smaller than the width. For instance by rotating the PageSize Rectangle: PageSize.A4.rotate()"));
        document.add(paragraph.init("This is portrait again"));
    catch (com.lowagie.text.DocumentException de) {
        savedErrorMessage = de;
    catch ( ioe) {
        savedErrorMessage = ioe;
    catch (coldfusion.runtime.Cast$NumberConversionException nce) {
        savedErrorMessage = nce;
    // step 5: we close the document
    // close the output stream

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


  © Blogger templates The Professional Template by 2008

Header image adapted from atomicjeep