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.





40 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
  22. Hi there, You've done a fantastic job. I will certainly digg it and personally recommend to my friends. I'm confident they'll be benefited from this site. stationary pump

    ReplyDelete
  23. I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme? Superb work!
    social media campaign singapore

    ReplyDelete
  24. I was recommended this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are incredible! Thanks! ihg hotel singapore

    ReplyDelete
  25. As a Newbie, I am continuously exploring online for articles that can benefit me. Thank you

    ReplyDelete
  26. Oh my goodness! an incredible article dude. Thank you However I'm experiencing concern with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss drawback? Anybody who is aware of kindly respond. brand activation Singapore

    ReplyDelete
  27. Hello there, You have done an incredible job. I will definitely digg it and personally recommend to my friends. I'm sure they will be benefited from this website. reach your goals faster

    ReplyDelete
  28. thank you for information

    คาสิโนออนไลน์ที่น่าเชื่อถือและมีความเป็นมืออาชีพที่สุดในตอนนี้
    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> Goldenslot
    สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
    มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย

    ReplyDelete
  29. Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is wonderful blog. An excellent read. I'll definitely be back.corporate catering Singapore 

    ReplyDelete
  30. Excellent blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol
    divorce in singapore 

    ReplyDelete
  31. Hi there! I'm at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the excellent work! affordable seo services

    ReplyDelete
  32. I'm truly enjoying the design and layout of your blog. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Exceptional work!
    cheap flyer printing in Singapore

    ReplyDelete
  33. I loved as much as you'll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike.
    travel insurance

    ReplyDelete
  34. Good day! I know this is kinda off topic but I'd figured I'd ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa? My website addresses a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you happen to be interested feel free to send me an e-mail. I look forward to hearing from you! Great blog by the way!
    Event Photography in Singapore

    ReplyDelete
  35. Greetings! I've been following your website for a long time now and finally got the courage to go ahead and give you a shout out from New Caney Tx! Just wanted to tell you keep up the fantastic job!
    Provide local content

    ReplyDelete