This tutorial will show you how to install and run all the software necessary for you to create PDF files in Java. We will be using the Eclipse editor to write the Java code. Of course there are many other Java editors, but we consider Eclipse to be particularly convenient. This tutorial focuses on the Windows platform. You will work through the following five steps:
- Installing Java on your computer
- Install iText on your Computer
- Install and configure Eclipse on your Computer
- Write and run a “HelloWorld” program
The order of these steps is very important. Please go through them one by on. In addition to this tutorial you may also want to consider reading these books that are of particular use:
|iText in Action
by Bruno Lowagie
view at Amazon.com
1. Install Java on your Computer
Download and install Java SE (Standard Edition) JRE (Java Runtime Environment). You do NOT need to install the JDK (Java Developer Kit). We will install Eclipse later, which does already include all the tools you need to write and compile your programs. After the installation you also do NOT need to set any Path or Classpath variables for Java itself. Java will be installed in “C:\Program Files\Java”
If you already have Java installed you need to verify that you have at least version 5, preferable of the JRE, but the JDK will work as well. We will install Eclipse, which does require at least this version.
Download iText jar file. Create a directory called “ProgramFiles” on your hard disk. The already existing directory “Program Files” does contain a space character between “Program” and “Files” that sometimes causes problems in the usage of Java technology. You will later also install Eclipse into the “ProgramFiles” directory. Place the iText jar file in your “ProgramFiles” directory.
3. Install and configure Eclipse on your Computer
Download Eclipse IDE for Java Developers. Eclipse itself is written in Java and does not contain an installer (setup.exe). It comes in a simple ZIP file that you should unzip into the previously created “ProgramFiles” directory. You may want to create a shortcut to “eclipse.exe” on your desktop for your own convenience.
When you start Eclipse you will be asked to choose a workspace. The workspace will contain all the files that you create. Again, you should create a directory in a location so that the path does not contain any space characters ” “. In our example we use the directory “d:\programming\workspace”. You will then be greeted with the welcome screen.
We will now create a Hello World program. First, you need to create a new Java project. Click on “File => New => Java Project”. Give the project a new name, but avoid space characters. We called ours “iTextTest”
Next, you need to turn this project into an iText project. Right-click onto the project and select “Properties”. Select “Java Build Path” on the left and click on the “Libraries” tab. Afterwards, click on “Add External JARs…” and locate the directory “ProgramFiles”. Select the iText.jar file and press “Open”. The iText library will now be listed. Click “OK” to leave the properties.
4. Write and run a “Hello world” program
First, we need to create the main class of your program. Click on “New => Class”. Name it “HelloWorld” and check the box “public static void(String args)”. You will receive an almost empty class. Eclipse is a very smart Java editor that tries to compile your program while you write. Similar to M$ Word, it will underline errors with a red line. Sometimes this automatic compilation does not work and you want to force Eclipse to compile. You can achieve this by simply saving the file.
You now need to copy iText’s example HelloWorld java file which is available here. Replace all the text in the class with the example file. Make sure that you comment out the line containing pachage:
// package com.lowagie.examples.general;
Now you can safe and run the program. The program will create a pdf file in your project directory inside of your workspace directory.