|
Geek-Tutorials.com
|
|
Geek Tutorials Home > Java PDF & Reporting - iText Tutorials > Setup iText for project & 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:
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
. Download latest source code for this project.