Sunday, December 2, 2007

Getting started with iText - Part 2 (DefaultPageSize.pdf)

The second installation in Getting started with iText translates the DefaultPageSize example. This example shows how to work with pre-defined page sizes.

Source: http://itextdocs.lowagie.com/tutorial/general/
Documentation: See Step 1 for more information about page sizes
Example: DefaultPageSize.java


<h1>The default PageSize and some other standard sizes</h1>
<cfscript>
    savedErrorMessage = "";

    // by default outputs to current directory. change as needed
    fullPathToOutputFile = ExpandPath("./DefaultPageSize.pdf");

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

    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
    pageSize = createObject("java", "com.lowagie.text.PageSize");
    //cfSearching: create a single paragraph object and reuse it
    paragraph = createObject("java", "com.lowagie.text.Paragraph");
    document.add(paragraph.init("The default PageSize is DIN A4."));
    document.setPageSize(PageSize.A3);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A3."));
    document.setPageSize(PageSize.A2);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A2."));
    document.setPageSize(PageSize.A1);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A1."));
    document.setPageSize(PageSize.A0);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A0."));
    document.setPageSize(PageSize.A5);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A5."));
    document.setPageSize(PageSize.A6);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A6."));
    document.setPageSize(PageSize.A7);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A7."));
    document.setPageSize(PageSize.A8);
    document.newPage();
    document.add(paragraph.init("This PageSize is DIN A8."));
    document.setPageSize(PageSize.LETTER);
    document.newPage();
    document.add(paragraph.init("This PageSize is LETTER."));
    document.add(paragraph.init("A lot of other standard PageSizes are available."));

    WriteOutput("Finished!");
    }
    catch (com.lowagie.text.DocumentException de) {
    savedErrorMessage = de;
    }
    catch (java.io.IOException ioe) {
    savedErrorMessage = ioe;
    }

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