Working with the examples in this topic (iText 5)

All the source files, as well as the resources and extra libraries necessary to run the book’s examples, were uploaded to a Subversion (SVN) repository on SourceForge. If you have an SVN client, you can check out of the complete working environment at once. This way, you’ll be able to get the latest updates and new examples, even after the topic has been released. Please consult appendix B for the URL of this repository.

You can find more info about this on the examples page of the site. That’s also the place where you’ll find zipped archives, in case you don’t have an SVN client. You can download these archives and unzip them on your local system.

Before you start experimenting, make sure that you have a recent version of the Java Development Kit (JDK) installed. The examples won’t work for versions of iText that are older than iText 5, and iText 5 is compiled with Java 5, so the minimum requirement for your JVM is Sun’s JDK 1.5. You can use other JDKs, but only the JDK from Sun is supported.

Figure 1.2 shows how I compiled and executed the first example, HelloWorld, on Ubuntu Linux using OpenJDK 6. As you can see, you first change the directory to the examples folder (or whichever folder contains your copy of the project). Then you run this command:

tmp17C2_thumb is the source file; we’ll take a close look at it in the next section. The option -d says that the compiled code should be written to the bin folder. With option -cp you define the classpath. For this simple example, you only need the iText.jar file. For other examples, you might need to add more JARs, such as a JAR with the database driver, encryption JARs, and so forth.

Once you’ve compiled the code, you can execute it:


If you’re working on Windows, you’ll need to replace the colon separating the different parts of the classpath with a semicolon:


Congratulations! You have created your first PDF file using iText. Figure 1.3 shows how everything is organized.

The source code of the examples can be found in the src folder; see, for instance, the file The package names of the examples correspond to the part and topi numbers of the topic. In the lib directory, you’ll find all the JARs you need to compile the examples. There’s also a resources folder containing all the resources you might need to run the examples: database scripts, images, special fonts, and existing PDF files, such as interactive forms.

Compiling and running from the command line

Figure 1.2 Compiling and running from the command line

Organization of the sample files

Figure 1.3 Organization of the sample files

The examples are compiled to the bin folder. The HelloWorld.class file will appear as soon as you run the javac command. When you execute the java command, you’ll see the hello.pdf file appear in the results directory. Figure 1.4 shows the end result: a PDF file containing the text "Hello World!"

It’s certainly possible to compile and execute all the examples from the command line, but it’s more likely that you’ll prefer using an integrated development environment (IDE). Figure 1.5 shows what the project looks like in Eclipse—you’ll recognize the same folders. Observe that Eclipse puts the src folder on top. The bin directory is hidden; you’ll find the JARs under Referenced Libraries. You can view and update the list of registered JARs by selecting Project > Properties > Java Build Path > Libraries.

A "Hello World" PDF

Figure 1.4 A "Hello World" PDF

The project opened in Eclipse

Figure 1.5 The project opened in Eclipse

Figure 1.5 already gives you a peek at the source code. The hello.pdf file is created in five steps. The next section discusses every step in detail.

Next post:

Previous post: