Setup iText for project

iText Hello World


We will be using eclipse for all iText tutorials in this site. You can check out Java, Beginner Guide, Install Java JDK, IDE & Build your first Java Program - Hello World for tutorial on how to install and write a simple program on eclipse.

First download iText library from http://sourceforge.net/projects/itext/ . Extract the downloaded file and find the iText library, the time of this tutorial writing was iText version 1.4.8 so the library name will be itext-1.4.8.jar.

Next create a eclipse project and add itext-1.4.8.jar to the project as shown below:

  • We assume you know how to create eclipse project, else please read the Beginnner Guide listed above. Now create a project name iText Tutorial.
  • Add a lib folder into the project if you not already done. On the eclipse Package Explorer, right click the iText Tutorial project name-> New -> Folder. Then name the new folder as "lib". Copy and paste itext-1.4.8.jar into the created lib folder. When this is done, the package explorer should be structure as shown below:
  • Although the itext-1.4.8.jar are now in the project lib folder, you still need to explicitly add it to the project. Select the project name iText Tutorial and press alt+enter to bring up the Properties dialog. Select Libraries Tab and click Add JARs.. button, on the pop-up JAR Selection dialog, expand the project iText Tutorial and select itext-1.4.8.jar then click OK button. When you added the library correctly, you can see the itext-1.4.8.jar is under the project root instead of lib folder as shown below:

iText Hello World


Now you are ready to create a simple iText Hello World program. Right click on the src folder on the Package Explorer then select New -> Class, enter the class name as ITextHelloWorld and click OK.

Write the following code into the ITextHelloWorld.java.

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Paragraph;
import java.io.FileOutputStream;

public class ITextHelloWorld {
	
	public ITextHelloWorld() throws Exception{
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("HelloWorld.pdf"));
		document.open();
		document.add(new Paragraph("Hello World"));
		document.close();
			
	}
	
	public static void main(String args[]){
		try{
			new ITextHelloWorld();
		}catch(Exception e){
			System.out.println(e);
		}
	}
}

           

On the Package Explorer, right click on the ITextHelloWorld.java -> Run As -> Java Application. Now the HelloWorld.pdf will be generated on your project root directory, you may need to refresh your project on the Package Explorer to see the new file. You must have Adobe Reader to be install on your machine to display the generated PDF file.

To display the HelloWorld.pdf, right click on the HellowWorld.pdf on the Package Explorer and then Open With -> System Editor.

Your donation will be use for this project's site maintainance and further development of the content. Your support can help us provide higher quality of free tutorials and services to everyone in future Support This Project. Download latest source code for this project.

 

This is the project of
SourceForge.net Logo