Sunday, December 2, 2007

Getting started with iText - Part 3 (LandscapePortrait.java)

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

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


<h1>Documents in Landscape and Portrait format</h1>
<cfscript>
    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", "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 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.setPageSize(PageSize.A4);
        document.newPage();
        document.add(paragraph.init("This is portrait again"));
        
        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