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.

Documentation: See Step 1 for more information about page sizes

<h1>The default PageSize and some other standard sizes</h1>
    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", "").init(fullPathToOutputFile);
    writer = createObject("java", "com.lowagie.text.pdf.PdfWriter").getInstance(document, outStream);

    // step 3: we open the document;

    // 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.add(paragraph.init("This PageSize is DIN A3."));
    document.add(paragraph.init("This PageSize is DIN A2."));
    document.add(paragraph.init("This PageSize is DIN A1."));
    document.add(paragraph.init("This PageSize is DIN A0."));
    document.add(paragraph.init("This PageSize is DIN A5."));
    document.add(paragraph.init("This PageSize is DIN A6."));
    document.add(paragraph.init("This PageSize is DIN A7."));
    document.add(paragraph.init("This PageSize is DIN A8."));
    document.add(paragraph.init("This PageSize is LETTER."));
    document.add(paragraph.init("A lot of other standard PageSizes are available."));

    catch (com.lowagie.text.DocumentException de) {
    savedErrorMessage = de;
    catch ( ioe) {
    savedErrorMessage = ioe;

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