Friday, December 7, 2007

Getting started with iText - Part 12 (Paragraphs.java)

Next up in the Getting Started with iText series is a translation of Paragraphs.java.

Hey, don't I know you from somewhere?


If you have been following the Getting Started with iText series you may be thinking "Paragraph? I know him from way back in the HelloWorld days". While there is some repetition, this example also introduces a new object called a Phrase. But we will touch more on that object later in the series.

Also making its debut is the FontFactory.getFont method. What is a factory method? A succinct description from an old article at www.javaworld.com describes it as "just a fancy name for a method that instantiates objects". For a more in depth (and more technical) discussion on factories you might be interested in reading Manufacturing Java Objects with the Factory Method Design Pattern.


Documentation: The Paragraph Object
Example: Paragraphs.java


<h1>The Paragraph object Example</h1>
<cfscript>
savedErrorMessage = "";

//cfSearching: By default, files are output to the current directory
fullPathToOutputFile = ExpandPath("./Paragraphs.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
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:
//cfSearching: create a single objects and reuse them
paragraph = createObject("java", "com.lowagie.text.Paragraph");
chunk = createObject("java", "com.lowagie.text.Chunk");
font = createObject("java", "com.lowagie.text.Font");
phrase = createObject("java", "com.lowagie.text.Phrase");
fontFactory = createObject("java", "com.lowagie.text.FontFactory");

p1 = paragraph.init( chunk.init("This is my first paragraph. ",
FontFactory.getFont(FontFactory.HELVETICA, javacast("float", 10))) );

p1.add("The leading of this paragraph is calculated automagically. ");
p1.add("The default leading is 1.5 times the fontsize. ");
p1.add(chunk.init("You can add chunks "));
p1.add(phrase.init("or you can add phrases. "));
p1.add(phrase.init( "Unless you change the leading with the method setLeading, the leading doesn't change if you add text with another leading. This can lead to some problems.",
FontFactory.getFont(FontFactory.HELVETICA, javacast("float", 18))) );
document.add(p1);
p2 = paragraph.init(phrase.init("This is my second paragraph. ",
FontFactory.getFont(FontFactory.HELVETICA, javacast("float", 12))) );
p2.add("As you can see, it started on a new line.");
document.add(p2);
p3 = paragraph.init("This is my third paragraph.",
FontFactory.getFont(FontFactory.HELVETICA, javacast("float", 12)) );
document.add(p3);

WriteOutput("Finished!");

}
catch (DocumentException de) {
System.err.println(de.getMessage());
}
catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
document.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