iText Java PDF Generator | PDF Reader
The PDF Reader
The iText library provides a PdfReader class that reads PDF files and converts them into new PDF documents. This is useful when new pages needs to be added to an existing PDF file or if you need to add page numbers and headers into a new file. PdfReader reads an entire page from an existing PDF document and copies it to the new PDF document.
PdfReader cannot parse the text within the existing PDF document or provide editing features for existing PDF documents.
One of the drawbacks of the iText PDF generator is the construction of the PDF document. All the PDF data needs to be added into either the Document or Writer object at creation time. This makes it difficult to modify PDF documents dynamically or to add content at runtime without recompiling the code. For instance, if you need to add a logo to the top of each page in a PDF document, you'll have to add the logo at the start of each new page. To alleviate this problem, iText defines a template class called PdfTemplate. PdfTemplate allows you to define common elements across pages into a common template. PdfTemplate can then be included anywhere into the PDF document.
As discussed previously, the approach of having static content generators can be a handicap for applications that use dynamically changing data. iText allows you to generate content from XML files and use them to create PDF documents directly. Support for the XML format is not present in the default iText libraries. To include XML support, you need to download a separate jar file. This includes various XML parser libraries necessary to parse an XML file into the desired iText format. These XML libraries also allow you to parse an XML file, extract the data, and use it to generate a PDF file.
Many applications require barcodes to be included in PDF documents. The iText library provides built-in API support for creating barcodes within a PDF document. Barcodes can be generated directly into the PDF document or as an image, which in turn can be embedded into the document. iText supports Barcode39, Barcode128, BarcodeEAN, BarcodeInter25, and BarcodePostnet.
The iText API's encryption package provides a command line utility for encrypting PDF documents with a username and password. The encrypted PDF can only be opened using the same username and password. Additionally, security settings can be provided to prevent modification, printing, or copying of the encrypted PDF document.
The API Components
- Document: This class is the heart of the iText API. It acts as a container for all elements that need to be embedded into a PDF document. You also need to instantiate this class first—before any content is added is the document object. You create a new document either using the default constructor or through an over loaded constructor, which takes the page size as a parameter. If no page size is specified, the default page size is assumed. The default page size in iText is DIN A4, which is the European A4 size. In the US, it is recommended to set the page size to PageSize.A4. All documents are in portrait format by default. To create a document in Landscape format, make the height smaller than the width and then rotate the document.
- Writer: This object does the actual job of writing content into a document. All writers extend from the abstract class com.lowagie.text.DocWriter. As of this release, iText supports three writers: PdfWriter, HtmlWriter, and RtfWriter. All these writers extend from the DocWriter class and are instantiated using a singleton method. The specific writer object is instantiated by passing the Document object to an output stream where the finished document needs to be written. The writer can write to any java output stream object. Multiple instances of the writer can be instantiated for the same document. This allows you to create a PDF document and an HTML document simultaneously. So a program using iText can generate a PDF document for display on the browser, create another copy to be stored on the server, and also create a RTF document to be delivered by email—all simultaneously.
- Tables: A table is a rectangular box with rows and columns defined in it. In the iText API, a table can be defined using the Table class. Cells can be added to the table using the addCell() method. Most of the other properties associated with regular HTML tables can be used in an iText table. Properties associated with HTML tables, like cell padding, cell spacing, row spans, column spans, borders, and width, can be set for iText tables. Tables are generally used when rows of data from a database table need to be displayed on a PDF. iText automatically splits tables that span multiple pages. It's up to you to repeat the headers on each page the table is spanned. iText allows for nested tables within a large table.
- Chunk Object: A Chunk is a unit of text that can be added to a Document. The contents of a chunk will all share the same properties like font, size, and color. The Chunk class supports functions like adding fonts and adding color to text. Chunk provides interesting features like underlining, strike through text, skewed text, subscript, and superscript. Apart from these, the Chunk object also provides custom functionality by asllowing you to create your own tag implementations.
- List: This object mimics the functionality of the HTML's ordered list (<OL>) and unordered list (<UL>). It allows you to display text as bullet points with numbering or without numbering.
- Paragraph Object: This object acts as a container for chunks, lists, and other high-level elements that can be added to a document. Each paragraph starts text on a new line. The Paragraph object has properties to align text, indent, and create spacing between paragraphs.
iText is an open source PDF generator API developed by Bruno Lowagie and Paulo Soares. It was initially developed just to create PDF documents and was later enhanced to include HTML and RTF formats. iText provides a simple and easy to use library to generate PDF documents. At the time of writing iText was being managed by sourceforge.net
There are a few other products both commercial and open source in the market which do the same job of generating PDFs. Notable and popular among them is the XSL-FO from Apache. You can find a tutorial on XSL-FO in JavaBoutique's tutorials section. Unlike iText, XSL-FO generates PDF using XML and XSL.