Monday, March 10, 2014

iText


iText is known to be an open source free library to create and manipulate PDF files in Java programming language. This useful library is originally developed by Bruno Lowagie and others since 2001. In this tutorial, I am going to show you how to configure iText library in Eclipse and create a PDF file to display the Hello World text.
To get start, you need to create a Java project in Eclipse. After the process of project creation  completes, you will download the latest iText library from the URL: http://sourceforge.net/projects/itext/files/latest/download. You will get the iText library in zip file format. Simply extract the zip file to a folder in your computer. In the folder, you will see a lot of jar files. However, the main one is itextpdf-5.5.0.jar. You need to add this jar file to the Java project before you begin writing Java code to create a PDF file or manipulate the file. To add the jar file to your current Java project in Eclipse, you will select Properties... from the Project menu. When the Properties dialog displays, click Java Build Path->Libraries. Then click Add External Libraries...A file dialog is open. Navigate to the folder that contains the itextpdf-5.5.0.jar. Select the file and click OK. Now the iText library is added to your project.

create pdf in itext

To create a PDF file, first you need to create a class in the project. You can name the class whatever you want. However, in this tutorial, the name of the class is PdfItext. Below is an example Java code to create a PDF file by using iText library.

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfItext {
public static void main(String[] args){
createPdf();
}

static void createPdf(){
try{
//create a document object
Document doc = new Document();
//FileOutputStream object to write the pdf file
FileOutputStream fos=new FileOutputStream("pdfdoc.pdf");
//get PdfWriter object
PdfWriter.getInstance(doc,fos);
//open the document for writing
doc.open();
//write a paragraph to the document
doc.add(new Paragraph("Hello World"));
//close the document
doc.close();
//view the result pdf file
Desktop dt=Desktop.getDesktop();
if(Desktop.isDesktopSupported()){
dt.open(new File("pdfdoc.pdf"));
}
}catch(Exception e){e.printStackTrace();}
}
}

The Java code above creates a PDF file called pdfdoc.pdf in the current working folder of your project. iText does five steps to create the PDF file you:
-Create a PDF document object.
-Get PDFWriter object.
-Open the document.
-Write content to the document.
-Close the document.





25 comments:

  1. It's really a cool and useful piece of information. I'm satisfied that you simply shared this useful info with us. Please keep us informed like this. Thanks for sharing. Thank you so much for the post very interesting! I like your writing style. I have just read it and I am so excited and curious about getting some more posts from you in future. Visit here: Best essay writing service

    ReplyDelete
  2. It is interesting not only for those who manipulates PDF files in Java programming language but for owner—č of an online business. Thank you for sharing.

    ReplyDelete
  3. Bosan dengan menghasilkan uang yang membosankan? kami akan membantu anda untuk mencari uang tambahan dengan mudah,Anda bisa Klik di agen s128
    Dapatkan informasi penting dalam dunia sabung ayam di website bawah ini
    https://siswaburung.tumblr.com/post/176441311821/lihat-lebih-dekat-ayam-filipina-yang-spartan

    ReplyDelete
  4. nice article in your blog.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete
  5. Really I Appreciate The Effort You Made To Share The Knowledge. This Is Really A Great Stuff For Sharing. Keep It Up . Thanks For Sharing.

    ReplyDelete
  6. This Is Great Information For Students. This Article Is Very Helpful I Really Like This Blog Thanks. I Also Have Some Information Relevant For Online Dissertation Help.

    ReplyDelete
  7. It's a useful open source free library to create and manipulate PDF files

    ReplyDelete
  8. Admiring the time and effort you put into your site and detailed information you present. It's awesome to come across a blog every once in a while that isn't the same old rehashed information. Great read! I've bookmarked your site and I'm including your RSS feeds to my Google account. google ads singapore

    ReplyDelete
  9. You really make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it! physics tuition

    ReplyDelete
  10. Good day! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot! A maths tuition

    ReplyDelete
  11. I have fun with, cause I found exactly what I used to be looking for. You have ended my four day long hunt! God Bless you man. Have a great day. Byeorganisational culture

    ReplyDelete
  12. Hello! Would you mind if I share your blog with my facebook group? There's a lot of folks that I think would really appreciate your content. Please let me know. Thanks money lender open on sunday

    ReplyDelete
  13. I'm curious to find out what blog system you're using? I'm having some small security issues with my latest website and I would like to find something more safe. Do you have any solutions? search engine marketing

    ReplyDelete
  14. I'm curious to find out what blog platform you happen to be working with? I'm having some small security issues with my latest website and I would like to find something more secure. Do you have any solutions? digital marketing information

    ReplyDelete
  15. This is the appropriate blog for anyone who wants to seek out out about this topic. You notice so much its virtually hard to argue with you (not that I actually would need…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Great stuff, just great! how to start an online business

    ReplyDelete
  16. I wanted to compose a brief message so as to appreciate you for those fantastic points you are giving at this site. My long internet research has at the end been recognized with brilliant tips to go over with my visitors. I 'd tell you that most of us readers actually are very much lucky to dwell in a good community with very many brilliant people with good techniques. I feel quite lucky to have discovered your web pages and look forward to plenty of more enjoyable times reading here. Thanks a lot once more for everything. conference organiser in singapore

    ReplyDelete
  17. Hmm it seems like your site ate my first comment (it was super long) so I guess I'll just sum it up what I wrote and say, I'm thoroughly enjoying your blog. I as well am an aspiring blog blogger but I'm still new to everything. Do you have any tips and hints for newbie blog writers? I'd definitely appreciate it. Paint Protection Film

    ReplyDelete
  18. very nice put up, i actually love this web site, carry on it best ecommerce design

    ReplyDelete
  19. Thanks for any other informative blog. Where else could I am getting that type of information written in such a perfect means? I have a project that I'm simply now operating on, and I've been at the look out for such information.
    e commerce in singapore

    ReplyDelete
  20. I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your website to come back down the road. Many thanks
    website design companies in Singapore

    ReplyDelete
  21. Please let me know if you're looking for a article author for your weblog. You have some really great articles and I believe I would be a good asset. If you ever want to take some of the load off, I'd absolutely love to write some material for your blog in exchange for a link back to mine. Please shoot me an email if interested. Regards! online marketing course

    ReplyDelete