How do I write to a PDF file using iText?
Author: Deron Eriksson
Description: This Java tutorial describes how to write to a PDF file using the iText library.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

The iText project, located at http://www.lowagie.com/iText/, is a JavaSW library that lets you generate PDF documents. It is very easy to use and features a high degree of functionality. In addition, the documentation is quite good and examples of iText on the web abound, such as Tools of the Trade, Part 1: Creating PDF documents with iText .

In this tutorial we'll create a Java class that writes some data to a PDF file. The iText jarW file can be downloaded from the iText website mentioned above and placed in a project, as shown below.

'testing' project

The ITextWritePdfFile class creates a file called 'itext-test.pdf' and creates an output stream to write to this file. It creates an itext document object and associates this with the output stream to the file. It adds an author ("Me") and a title ("My iText Test") to the document metadata.

Following this, it opens the document to write content to the document. A text 'chunk' object is created as is formatted with the Courier font, italics, underlining, and a cyan background color. This chunk is added to the document. Following this, a paragraph object is created with some center-aligned text, and the paragraph is added to the document. Next, an image is read in from the root level of the project ('world.gif'), and the image is added to the document. As a further example of the various types of classes offered by the iText library, a list object is created and two items are added to the list, and then the list is added to the document. Following this, the document is closed.

ITextWritePdfFile.java

package test;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class ITextWritePdfFile {

	public static void main(String[] args) {
		try {
			File file = new File("itext-test.pdf");
			FileOutputStream fileout = new FileOutputStream(file);
			Document document = new Document();
			PdfWriter.getInstance(document, fileout);
			document.addAuthor("Me");
			document.addTitle("My iText Test");

			document.open();

			Chunk chunk = new Chunk("iText Test");
			Font font = new Font(Font.COURIER);
			font.setStyle(Font.UNDERLINE);
			font.setStyle(Font.ITALIC);
			chunk.setFont(font);
			chunk.setBackground(Color.CYAN);
			document.add(chunk);

			Paragraph paragraph = new Paragraph();
			paragraph.add("Hello World");
			paragraph.setAlignment(Element.ALIGN_CENTER);
			document.add(paragraph);

			Image image;
			try {
				image = Image.getInstance("world.gif");
				image.setAlignment(Image.MIDDLE);
				document.add(image);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			List list = new List(true, 15);
			list.add("ABC");
			list.add("DEF");
			document.add(list);

			document.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}

(Continued on page 2)

Page:    1 2 >