Running the examples in the book
If you are able to answer the magic question, you have access to an overview of all the book examples, organized per chapter. If you are successfully logged in, and you go to the examples of chapter 1, you see this overview (there are only two examples in the first introductory chapter):
- ant action -Dchapter=01 -Dexample=HelloWorld
- Resulting file:
- ant action -Dchapter=01 -Dexample=HelloWorldBurst
- Resulting files:
The first line is an ANT command line.
You can run the example using the java command, but the benefit of using ANT,
is that you don't need to worry about the CLASSPATH, compiling the code,
using the correct working directory, and so forth. The action target
(one of the targets defined in the build.xml file)
takes care of this. All you need to do, is to provide the correct chapter number
(01, 02, ..., 18) and the classname
(HelloWorld) as demonstrated; for instance:
ant action -Dchapter=01 -Dexample=HelloWorld
A .class-file will be created somewhere in the bin
directory. Resources will be fetched from the resources directory,
and the result will appear in the results directory.
Let's have a closer look at the directory structure.
The directory structure
You can put the examples in any directory you want. Let's pretend we are working
on Windows and that we keep all the source code related to this book in directory
D:/itext-in-action/. This directory is more or less mirrored on
itext.ugent.be/itext-in-action/
D:/itext-in-action/
D:/itext-in-action/bin/
D:/itext-in-action/bin/classes/
The class files of the examples will be generated in directorieschapter01;chapter02, ...,chapter18
D:/itext-in-action/bin/classes/chapter01
D:/itext-in-action/bin/classes/chapter02- ...
D:/itext-in-action/bin/classes/chapter18
- D:/itext-in-action/bin/batik-awt-util.jar
- D:/itext-in-action/bin/batik-bridge.jar
- D:/itext-in-action/bin/batik-css.jar
- D:/itext-in-action/bin/batik-dom.jar
- D:/itext-in-action/bin/batik-ext.jar
- D:/itext-in-action/bin/batik-gvt.jar
- D:/itext-in-action/bin/batik-parser.jar
- D:/itext-in-action/bin/batik-script.jar
- D:/itext-in-action/bin/batik-svg-dom.jar
- D:/itext-in-action/bin/batik-util.jar
- D:/itext-in-action/bin/batik-xml.jar
- D:/itext-in-action/bin/iText.jar (don't forget to remove the version number!)
- D:/itext-in-action/bin/iTextAsian.jar
- D:/itext-in-action/bin/itext-hyph-xml.jar
- D:/itext-in-action/bin/jcommon.jar
- D:/itext-in-action/bin/jfreechart.jar
- D:/itext-in-action/bin/servlet.jar
- D:/itext-in-action/bin/xerces_2_5_0.jar
D:/itext-in-action/examples/
D:/itext-in-action/examples/chapter01
D:/itext-in-action/examples/chapter01/results/
This is where the results will be generated:
D:/itext-in-action/examples/chapter01/results/HelloWorld.pdf
D:/itext-in-action/examples/chapter01/results/HelloWorldRead.pdf
D:/itext-in-action/examples/chapter01/results/HelloWorldRead_1.pdf
D:/itext-in-action/examples/chapter01/results/HelloWorldRead_2.pdf
D:/itext-in-action/examples/chapter01/results/HelloWorldRead_3.pdf
- D:/itext-in-action/examples/chapter01/HelloWorld
- D:/itext-in-action/examples/chapter01/HelloWorldBurst
D:/itext-in-action/examples/chapter02
D:/itext-in-action/examples/chapter02/resources/
Contains the resources needed for some examples in chapter 2; in this case, there's only one:- D:/itext-in-action/examples/chapter02/resources/watermark.jpg
D:/itext-in-action/examples/chapter02/results/
This is where the results of the examples of chapter 2 will be generated (PDF, RTF and HTM files).- D:/itext-in-action/examples/chapter02/HelloWorld.java
- ... (the Java source code of the examples of chapter 2)
- D:/itext-in-action/examples/chapter02/HelloWorldWriter.java
- ...
D:/itext-in-action/examples/chapter18
D:/itext-in-action/examples/chapter18/resources/
Contains the resources needed for some examples in chapter 18
D:/itext-in-action/examples/chapter18/results/
This is where the results of the examples of chapter 18 will be generated (PDF, RTF and HTM files).- ... (the Java source code of the examples of chapter 18)
- D:/itext-in-action/build.xml
You can find zip-files with the jars that go into the bin directory and the sample code (including resources) in the download directory of this site.
Anatomy of an example
When you have installed ANT, downloaded the build.xml, the sample source code and resources, you can consult the chapter-pages on this site, and compile/execute the examples. The 22nd example of chapter 2 is described like this:
- ant action -Dchapter=02 -Dexample=HelloWorldStamper
- Extra resource needed to run this example:
- Resulting files:
You can copy/paste the first line to your shell (DOS, bash,...) like this:ant action -Dchapter=02 -Dexample=HelloWorldStamper
- The ant script will find the source file HelloWorldStamper in directory:
- D:/itext-in-action/examples/chapter02/
- It will be compiled to a file HelloWorldStamper.class in directory:
- D:/itext-in-action/bin/classes/chapter02/
- While executing the class file, the file watermark.jpg will be fetched from directory
- D:/itext-in-action/examples/chapter02/resources/
- Two new files HelloWorldRead.pdf and HelloWorldStamper.pdf will appear in directory:
- D:/itext-in-action/examples/chapter02/results/
The resulting files are indicated with
.
If a file was generated as input for PDF manipulating, the input file is
marked with a rect exclamation point
,
the output files are marked with a blue one
.The fonts directory
Some examples need an extra font program, such as arial.ttf, comic.ttf, and so forth. These fonts are usually shipped with the Operating System. They can get quite large, so they aren't shipped with the iText samples.
Whenever an extra font is needed, it is marked like this example from chapter 8:
- ant action -Dchapter=08 -Dexample=TrueTypeCollections
- IMPORTANT! Extra font needed in the font directory:
- MS Gothic, MS PGothic, and MS UI Gothic (msgothic.ttc)
- Resulting file:
For the sake of simplicity, the examples assume that you have these fonts in a directoryc:/windows/fonts/Please adapt the source code if the fonts reside in another directory on your system. Change it into
c:/winnt/fonts/(NT),/usr/X/lib/X11/fonts/TrueType/,/usr/share/fonts/default/TrueType/(UNIX, Linux),/Library/Fonts/,/System/Library/Fonts(Mac), or whatever directory the required font is in.Most fonts that are used in this book are pretty standard; except maybe for the TrueType collection mentioned in the example above; msgothic.ttc is a font with Japanese glyphs. It is about 8MB, and it isn't installed by default. If you need it, you can install it manually from a Windows Installation CD.
Extra jars
All the jars you can possibly need to run the examples are listing in the directory structure; iText.jar is used in every example, but whenever an extra jar is needed, it will be indicated like in this example from chapter 4:
- ant action -Dchapter=04 -Dexample=DickensHyphenated
- Extra jar needed to run this example:
- Resulting file:
Please contact the iText mailinglist if you experience iText-related troubles with these examples. For problems installing ANT, please visit an ANT forum. Basic Java compilation problems should be reported to a Java forum.






