Thursday, December 6, 2007

Getting started with iText - Part 10 (

The next installation in Getting started with iText translates the example.

Chunks? This is a good thing ... right?

The name sounds a bit peculiar, but the documentation explains that a Chunk is "the smallest significant part of text that can be added to a document. It is the 'atom' building block of most of the other High Level Text objects." (Now that sounds better).

This example introduces a few new classes like com.lowagie.text.Chunk and java.awt.Color, plus the java primitive type float. You may notice the java code contains a few numbers followed by a letter: 8.0f. The "f" stands for (32-bit float literal). The literal is not used in the CF code.

Now I hit a few stumbling blocks while converting the java code for the java.awt.Color object. The java code calls the constructor with hexadecimal numbers.

   new Color(0xFF, 0xDE, 0xAD)

I checked the API and it does not mention any constructors that accept hexadecimal values. Perhaps java performs some type of implicit conversion? Ultimately I ended up converting hexadecimal values to integers using a tip from one of the functions at I also found some other interesting java methods for converting colors, but more about that next time.

Documentation: The Chunk Object

So where is the code?

<h1>The Chunk object example</h1>
savedErrorMessage = "";

fullPathToOutputFile = ExpandPath("./Chunks.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", "").init(fullPathToOutputFile);
writer = createObject("java", "com.lowagie.text.pdf.PdfWriter").getInstance(document, outStream);

// step 3: we open the document;
// step 4:
//cfSearching: create a single chunk and color object and reuse them
chunk = createObject("java", "com.lowagie.text.Chunk");
color = createObject("java", "java.awt.Color");

fox = chunk.init("quick brown fox");
superscript = javacast("float", "8.0");
// cfSearching: Using the java.awt.Color constructor that accepts primitive integers
// cfSearching: InputBaseN(string, 16) converts a hexadecimal value to decimal
foxColor = color.init( javacast("int", InputBaseN("0xFF", 16)),
javacast("int", InputBaseN("0xDE", 16)),
javacast("int", InputBaseN("0xAD", 16))
jumps = chunk.init(" jumps over ");

dog = chunk.init("the lazy dog");
subscript = javacast("float", "-8.0");
dogColor = color.init( javacast("int", InputBaseN("0xFF", 16)),
javacast("int", InputBaseN("0x00", 16)),
javacast("int", InputBaseN("0x00", 16))
// cfSearching: Must create a PdfContentByte to use one of its constants
pdfContentByte = createObject("java", "com.lowagie.text.pdf.PdfContentByte");
javacast("float", 3.0),
javacast("float", 0.0),
javacast("float", -5.0) + subscript,
javacast("float", 0.0),


} catch (DocumentException de) {
} catch (IOException ioe) {

// step 5: we close the document

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