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.






compass app

39 comments:

  1. 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
  2. nice article in your blog.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete
  3. 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
  4. 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
  5. It's a useful open source free library to create and manipulate PDF files

    ReplyDelete
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. very nice put up, i actually love this web site, carry on it best ecommerce design

    ReplyDelete
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. As a Newbie, I am continuously exploring online for articles that can benefit me. Thank you

    ReplyDelete
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. I definitely wanted to make a remark to be able to thank you for these amazing facts you are giving out at this site. My time-consuming internet research has at the end of the day been compensated with sensible ideas to write about with my companions. I 'd suppose that most of us website visitors actually are very fortunate to be in a useful website with so many marvellous individuals with valuable strategies. I feel extremely fortunate to have seen your website page and look forward to plenty of more entertaining moments reading here. Thanks again for everything.
    responsive web design

    ReplyDelete
  31. You actually make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I will try to get the hang of it! responsive website design

    ReplyDelete
  32. Thanks for each of your hard work on this site. My daughter take interest in engaging in internet research and it is easy to understand why. A lot of people hear all about the dynamic form you present helpful guides by means of your web site and as well as attract contribution from website visitors about this area while our own daughter is undoubtedly discovering a great deal. Enjoy the remaining portion of the new year. You're the one carrying out a stunning job.build a website from scratch

    ReplyDelete
  33. Hey there! I could have sworn I've been to this blog before but after checking through some of the post I realized it's new to me. Anyhow, I'm definitely delighted I found it and I'll be bookmarking and checking back often!
    keywords in your URLs

    ReplyDelete
  34. This comment has been removed by the author.

    ReplyDelete
  35. Hello there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips?
    expert in your niche

    ReplyDelete
  36. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your blog when you could be giving us something enlightening to read? having a responsive design

    ReplyDelete
  37. Thanks for another magnificent post. Where else could anyone get that type of information in such a perfect way of writing? I've a presentation next week, and I'm on the look for such information.
    marketing companies singapore

    ReplyDelete