August 16, 2007

Creating PDF files through Java

PDF has become a very popular interchange format for text and graphical documents and are used in electronic publishing. Abobe's PDF format has penetrated into the market extensively and this format has a big market share. Initially, it was designed as a "final" document format, but the dynamic requirements of document exchange over the Internet led to dynamic PDFs and also other formats. You will find many other formats like excel sheets, word documents dynamically created on net to deliver reports, results, product comparisons etc. So, lets have a look at creating PDF files dynamically.
As a java programmer you should be able to create PDF files in web applications and standalone applications once you finish this article. In the end I will also explain how to convert other documents to PDF online and also how to make options for creating PDFs of your blog or webpage.

PDF libraries:

You can find many libraries both commercial and open-source that will help you create PDF files.I will consider only open-source libraries here and particularly one library: iText. Apart from this library you have gnujpdf, JFreeReport, PJX etc...

As I said iText is a open-source pure Java class library for creating and manipulating PDF documents. It provides a rich set of features like support for both PDF and FDF documents, different layouts, images, document encryption, watermarks, document templates etc. Its published in two different licenses: MPL and LGPL.

On to Coding...

Like I said, I will be looking into iText library for now.. Let's get started. Upon downloading and extracting the compressed file, you will have a single jar file which holds all the necessary classes. There are two major classes in creating a PDF file, they are Document and PdfWriter. Document represents your programmatic PDF file and the writer helps you to associate the document object with any output stream. So you can directly write to a file system or transfer the PDF through a servlet's output stream. Now lets look into a simple method that will create a PDF file on file system:

public void createTestPdf() throws FileNotFoundException {
Document pdfDocument = new Document();
String filePath = "c:\\" + fileName;
FileOutputStream fos = new FileOutputStream( filePath );

PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(pdfDocument, fos);
writer.open();
pdfDocument.addTitle("A sample PDF Title");
pdfDocument.addAuthor("Abdel");
pdfDocument.open();
pdfDocument.add(new Paragraph("This is a sample PDF document created using iText PDF library!"));
pdfDocument.add(Image.getInstance("C:\\Documents and Settings\\My Documents\\My Pictures\\puzzle.jpg"));
pdfDocument.add(new Paragraph("This is second Paragraph"));
pdfDocument.close();
writer.close();
}catch (DocumentException e) {
System.out.println("Document Exception!");
e.printStackTrace();
}
}

The above method createTestPdf will create a simple PDF file with some text and image. The API is quite simple.

Online PDF tools:

There are many online PDF tools available to create and convert files. Adobe provides PDF conversion for a fee, but lets look into some other alternatives. PDF Online provides two services: Doc2PDF Online and Web2PDF Online. Web2PDF Online is still in BETA and I will talk about it too.. but, first Doc2PDF. It gives you the facility to convert different documents like word document, excel sheets, power point slides, HTML etc to be converted online in three steps. Upload the documents, give the output filename and email address to which the converted document is to be sent.

Now onto web2PDF Online service. This helps you to convert your blog or website into PDF files. Your visitors can quickly save their useful information with a click of a button. To avail this service you have to create a account with PDF Online. They also track your site's PDF conversions. I tried it on my testing blogs and found some small glitches, like the quality and page size. But overall the service is good.

2 comments :

Anitha said...

Let me know if u find any tool for editing a pdf.

Zarfishan said...

Try this PDF Creator in java , and @anitha you can also edit the existing pdf file by using this creator.