about this book

This book will teach you about PDF, Adobe’s Portable Document Format, from a Java developer’s point of view. You’ll learn how to use iText in a Java/J2EE application for the production and/or manipulation of PDF documents. Along the way, you’ll become acquainted with lots of interesting PDF features and discover e-document functionalities you may not have known about before.

In addition to the many small code samples, this book includes lots of XML-based, ready-made solutions that can easily be adapted and integrated into your projects.

If you’re a .NET developer using the C# or J# port of iText, iTextSharp or iText.NET, you can also benefit from this book, but you’ll have to adapt the examples.

How to use this book

You can read this book chronologically, starting with the introductory part 1. Part 2 describes useful basic building blocks, and part 3 gets into iText’s core PDF functionality. You’ll finish with part 4, which discusses the interactive features of PDF.

If you haven’t convinced your project manager yet that PDF is the way to go, you’ll certainly benefit from reading chapters 1 and 3. It sums up some reasonable arguments that will help you help your manager make policy decisions regarding e-documents. Section 1.3 contains a roadmap to the ready-made solutions that are demonstrated throughout the book. The main function of this section is to offer you a menu composed of a series of screenshots, showing all kinds of documents: documents with flowing text, graphics, bookmarks, and so on. If you see something you like, you can use this book as a kind of 'cookbook’ and jump to the 'recipe’ that was used to create a similar document.

Readers who are new to iText will need to take the “Hello World” crash course in chapter 2. This chapter shows that iText can be used in many different ways. The first three chapters often refer to sections in parts 2, 3, and 4, where you’ll find an in-depth explanation of the specific functionality that is being introduced in one of the many “Hello World” examples.

You can also read the book in random order or thematically, starting from the table of contents or the roadmap in chapter 1. Once you’re well acquainted with iText, you’ll probably use the book as a reference manual, browsing for the many small standalone code samples that can be applied directly to your own code.


Part 1 consists of three chapters which introduce the history of iText and the basics of creating and manipulating PDF documents. These chapters give you a bird’s-eye view of PDF in general and iText in particular. You’ll get acquainted with different aspects of PDF by first looking at different screenshots and then making a series of small “Hello, World” files demonstrating the concept of PDF creation and manipulation using iText. Chapter 1 also discusses in greater detail how to use and navigate the book.

Part 2 consists of four chapters that explain the building blocks which are used to construct a document, such as phrases, paragraphs, chapters, and sections. A document can also contain images, tables, and columns. Chapters 4 through 7 explain how iText implements these structures, and the examples at the end of each chapter demonstrate how they fit together.

Part 3 goes to the core of iText and PDF. This part is meant to serve as a reference manual for the reader, explaining how to create the actual content of a document and answering many practical questions: How do I choose a font? How do I draw a dashed line? How do I make an image transparent? How do I translate a Swing component to PDF? Chapters 8 through 12 answer these and many other questions, further illustrating them with plenty of examples.

The last six chapters of the book make up part 4, “Interactive PDF,” and they deal with meta content. The following questions are answered: How do I add bookmarks to a file? How do I add headers, footers, or a watermark? How do I add comments or a file attachment? How do I create and fill a form? And above all, how do I create a PDF file in a web application? The syntax and design of PDF are discussed.

Who should read this book?

This book is intended for Java developers who want to enhance their projects with dynamic PDF document generation and/or manipulation. It assumes you have some background in Java programming.

For reasons of convenience, most of the examples are constructed as standalone command-line applications. If you want to run these examples in a web application, you should know how to set up an application server, where to put the necessary Java archive files (jars) and resources, and how to deploy a servlet.

The same goes for XML. Although this book could have used database tables, XML was preferred as the technology-independent format to store the data needed for the ready-made solutions. You should be familiar with Simple API for XML (SAX) parsers and how to use them.

Knowledge of the Portable Document Format isn’t necessary, because this book will explain a good deal of the PDF functionality and syntax where needed. The PDF Reference (Adobe Systems Inc.) is a good companion for this book, for those who want to know every detail about PDF internals.

Code conventions

First use of technical terms is in italic. The same goes for emphasized terms and mathematical variables. Source code in listings or in text is in a fixed width font. Java packages, method names, directories, parameters, and XML elements and attributes are also presented using fixed width font. Some code lines can be in bold fixed width font for emphasis. Code that appears in italic fixed width font is a placeholder, and you should replace it according to your needs.

Code annotations accompany many of the source code listings, highlighting important concepts. In some cases, annotations correspond to explanations that follow the listing.

Software requirements and downloads

iText is a Free/Open Source Software library created by Bruno Lowagie and Paulo Soares, protected by the Mozilla Public License (MPL). You can download it from http://www.sourceforge.net/projects/itext/ or http://www.lowagie.com/iText/.

All jars are compiled with the Java Development Kit (JDK) 1.4. If you need iText to run in another Java Runtime Environment (JRE), it’s safest to download the source code and recompile the library with the corresponding JDK.

You can download the source code of the small standalone examples, as well as the ready-made solutions, from itext.ugent.be/itext-in-action/. You can also download the source code for the examples in the book from www.manning.com/lowagie. All examples have been tested with iText 1.4.

Author Online

Your purchase of iText in Action includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to www.manning. com/lowagie. This page provides information on how to get onto the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue among individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the author some challenging questions, lest his interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.

Although no one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, re-telling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action guide is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.

There is another, more mundane, reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.

About the cover illustration

The figure on the cover of iText in Action is a “Dorobautz Valachia” or a Rumanian from Wallachia, a historical region of southeast Romania between the Transylvanian Alps and the Danube River. Founded as a principality in the late thirteenth century, Wallachia was ruled by Turkey from 1387 until it was united with Moldavia to form Romania in 1861. The illustration is taken from a collection of costumes of the Ottoman Empire published on January 1, 1802, by William Miller of Old Bond Street, London. The title page is missing from the collection and we have been unable to track it down to date. The book's table of contents identifies the figures in both English and French, and each illustration bears the names of two artists who worked on it, both of whom would no doubt be surprised to find their art gracing the front cover of a computer programming book...two hundred years later.

The collection was purchased by a Manning editor at an antiquarian flea market in the “Garage” on West 26th Street in Manhattan. The seller was an American based in Ankara, Turkey, and the transaction took place just as he was packing up his stand for the day. The Manning editor did not have on his person the substantial amount of cash that was required for the purchase and a credit card and check were both politely turned down. With the seller flying back to Ankara that evening the situation was getting hopeless. What was the solution? It turned out to be nothing more than an old-fashioned verbal agreement sealed with a handshake. The seller simply proposed that the money be transferred to him by wire and the editor walked out with the bank information on a piece of paper and the portfolio of images under his arm. Needless to say, we transferred the funds the next day, and we remain grateful and impressed by this unknown person’s trust in one of us. It recalls something that might have happened a long time ago.

The pictures from the Ottoman collection, like the other illustrations that appear on our covers, bring to life the richness and variety of dress customs of two centuries ago. They recall the sense of isolation and distance of that period—and of every other historic period except our own hyperkinetic present.

Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.

We at Manning celebrate the inventiveness, the initiative, and, yes, the fun of the computer business with book covers based on the rich diversity of regional life of two centuries ago‚ brought back to life by the pictures from this collection.