Thursday, December 6, 2007

Getting started with iText - Part 10 (Chunks.java)

The next installation in Getting started with iText translates the Chunks.java 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 www.cflib.org. I also found some other interesting java methods for converting colors, but more about that next time.


Documentation: The Chunk Object
Example: Chunks.java

So where is the code?




<h1>The Chunk object example</h1>
<cfscript>
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", "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 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");
fox.setTextRise(superscript);
// 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))
);
fox.setBackground(foxColor);
jumps = chunk.init(" jumps over ");

dog = chunk.init("the lazy dog");
subscript = javacast("float", "-8.0");
dog.setTextRise(subscript);
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");
dog.setUnderline(dogColor,
javacast("float", 3.0),
javacast("float", 0.0),
javacast("float", -5.0) + subscript,
javacast("float", 0.0),
PdfContentByte.LINE_CAP_ROUND);
document.add(fox);
document.add(jumps);
document.add(dog);

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