Most Read Latest News Blog Resources

I Text, You Text, We All Text for iText




January 15, 2006 — 
I have a soft place in my heart for typesetting systems. I’ve built several over the ages, and have typeset many of my own books. My monthly C Chest column in Dr. Dobb’s Journal was canned in 1987 largely because of my “wasting” three months presenting the source code for my version of a markup-language interpreter that did layout: troff. (The editor insisted that both Unix and markup languages were irrelevant, obsolete technologies. For what it’s worth, Bill Gates was saying exactly the same thing about HTML at the time.)

I’m going to try my luck again: In my Dec. 15 column, I promised that I’d report back on a typesetting system: iText, Bruno Lowagie and Paulo Soares’ PDF-creation library. Let’s pick up that thread.

To refresh your memory, a project required me to create a few simple printed reports—the complete contents of which weren’t known until runtime—in CSV, HTML and PDF formats. I rejected JasperReports, which I found to be an overly complicated, poorly documented nonsolution to this problem. You know something’s wrong when it’s harder to use the tool than it is not to use the tool.

The iText library—the PDF-generation library that JasperReports uses—is, on the other hand, very good. iText is a full-blown typesetting system that lets you do in Java pretty much everything you can do with PostScript. You can use iText to lay out complex documents containing both text and images to produce a PDF representation. (Note that iText also can produce HTML and RTF files, but it doesn’t do as good a job in these formats as it does with PDF. The table-layout classes, essential for reporting applications, work only with PDF output, for example.)

Though it does have all the low-level APIs that you need to build a real typesetting system, the real strength of iText is the high-level objects that let you manipulate the document at the level of paragraph and chunk. (A chunk is part of a paragraph.) You assemble a paragraph simply by adding chunks of text to it, and can easily modify paragraph attributes like font size and margins. There’s also a very nice set of table-creation objects. The vast majority of applications can get by with nothing but these simple-to-use APIs.

On the output side, iText uses the Builder design pattern. You attach a “writer” as defined by that pattern to the document you’re creating, and that writer takes care of the mechanics of building a particular output format. That is, you can create a document without needing to know whether the ultimate output format will be pdf, rtf, html and so on.iText documentation is a mixture of the usual JavaDoc and an online tutorial called “iText by Example.” The fact that this tutorial is not printable is a definite negative, but it’s well organized and easier to use than many online tutorials. The tutorial presents the material in a linear way that guides you through the entire library, as compared with a blob of links that force you to bounce around in the material more or less randomly. There are a few minor linguistic anomalies (the author likes to say “Remark that” instead of “Note that,” for example), but they don’t get in the way of readability.

The main problem with the tutorial is that it’s not complete. The PDF-Form-generation mechanisms are not covered, for example.

Very much on the plus side, the tutorial is laid out using a use-case approach that I’ve advocated in the past. It focuses on tasks that you are likely to perform, then tells you how to accomplish those tasks. It’s also organized in such a way that you can get to work immediately if you’re doing simple stuff, relegating the gory details that you’re not likely to need to later sections. There are plenty of coding examples.

Consequently, I’ve decided to code directly to the iText APIs rather than using a report-generation package like Jasper. The resulting report will be both better looking and easier to code than the Jasper equivalent. Moreover, though iText can create HTML output, it is easily integrated into a Servlet to deliver PDF directly to the browser. Since this way of working gives me a better-looking page than the HTML, I plan to skip the HTML entirely and just present PDF to the user. And the icing on the cake is that I can do iText without any XML whatever. I’m happy. And I still have my column! z

Allen Holub is an architect, consultant and instructor in C/C++, Java and OO Design. Reach him at www.holub.com.


Share this link: http://www.sdtimes.com/link/29077
 

Add comment


Name*
Email*  
Country     


  • Comment
  • Preview
Loading



 
 
 
 
News on Monday
more>>
SharePoint Tech Report
more>>


   

 
 
Download Current Issue
ISSUE 3/15/2010 PDF

Need Back Issues?
DOWNLOAD HERE

Receive the print Edition?


 
blogs tab
Google Code turns 5
Google Code Turns 5, and adds a Paxos Algorithm to make the system more stable and reliable.
03/17/2010 11:16 AM EST

Test your Visual Studio 2010 know-how
Microsoft is offering free beta certification exams for Visual Studio 2010.
03/17/2010 11:08 AM EST

Microsoft lifts the hood on IE9
Microsoft is previewing IE9.
03/16/2010 01:10 PM EST

 

Events calendar tab
3/22/2010 to 3/25/2010
Santa Clara, Calif.
The Eclipse Foundation

4/12/2010 to 4/14/2010
Las Vegas
Penton Media

4/12/2010 to 4/15/2010
Santa Clara, Calif.
O'Reilly Media

4/19/2010
New York City
Flagg Management

4/25/2010 to 4/28/2010
Overland Park, Kans.
IIUG