Getting started with iText - Part 11 (DifferentFonts.java)
The next installation in Getting started with iText translates the DifferentFonts.java example. It demonstrates how to use different fonts like ZapfDingbats. (I am not even going to ask ...) It also uses the Chunk object introduced in Getting started with iText - Part 10
And now a brief intermission ..
You may notice this example uses the JavaCast function in several places. I have discovered that CF and Java are polar opposites when it comes to typing. CF is very laid-back and loosely typed. Whereas Java is strict, conservative and strongly typed. Deviation from the rules will not be tolerated! There are advantages to both, but as a result it is wise to use JavaCast() whenever passing values between CF and Java. The JavaCast() function ensures the values are properly converted from CF to the correct Java type.
As always, these are my own highly unofficial observations as I learn more about combining iText, CF and Java ;) Comments/corrections/suggestions are always welcome.
Documentation: See Fonts for more information
Example: DifferentFonts.java
<h1>Different Fonts Example</h1>
<cfscript>
savedErrorMessage = "";
fullPathToOutputFile = ExpandPath("./DifferentFonts.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 paragraph, chunk and font object and reuse them
paragraph = createObject("java", "com.lowagie.text.Paragraph");
chunk = createObject("java", "com.lowagie.text.Chunk");
font = createObject("java", "com.lowagie.text.Font");
//cfSearching: Now create an empty paragraph and add chunks to it
//cfSearching: Yes, that does sound a bit odd
p = paragraph.init();
p.add( chunk.init("This text is in Times Roman. This is ZapfDingbats: ",
font.init(Font.TIMES_ROMAN, javacast("float", 12))) );
p.add( chunk.init("abcdefghijklmnopqrstuvwxyz",
font.init(Font.ZAPFDINGBATS, javacast("float", 12))) );
p.add( chunk.init(". This is font Symbol: ",
font.init(Font.TIMES_ROMAN, javacast("float", 12))) );
p.add( chunk.init("abcdefghijklmnopqrstuvwxyz",
font.init(Font.SYMBOL, javacast("float", 12))) );
document.add(paragraph.init(p));
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:
Post a Comment