One of the simplest and cheapest utilities that I found was a package called iText. This utility allows you to create PDF documents on the fly or in batch mode. It's simple to use, yet can handle some of the most complex tasks that you will ask it to do. Even better, the price to use it: FREE!
The first step in creating a PDF from Java is to download and import the itext.jar file from www.lowagie.com/iText
into your Java project. Once iText becomes available to you in your project, the fun can begin.
The next step is to tell Java to create the initial PDF document, using the following initialization:
Document document = new Document();
Now that the document variable is initialized, its available for us to start adding information to. We need to initialize a Writer to write the information to the document. This can be accomplished with the following line of code:
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
For our example, we're using "test.pdf" as the file name. However, you can use any valid file name (including file path information) here and you can use a variable to pass the information here. In a production application, I pass a variable that is based on information contained in a work file that is being read.
Now that we have a pdfWriter and our document, we need to open the document in order to write to it.This is another one line statement: document.open(); This tells the code to open the document and allow us to actually write to it.
Now let's write a simple, one line paragraph to the document. document.add(new Paragraph("Hello, this is an example of how to use iText."));
After we write the paragraph to our document, we have to close it before the file can be read by Acrobat Reader (or another program that can read PDF files). To close the document, we use the document.close(); command.
Now that the we've created test.pdf, browse to the directory where you stored this document and open it with any program that can read PDF documents. Inside the document, you should see "Hello, this is an example of how to use iText."
Congratulations, you've just created your first PDF document using iText. See how simple that was. Now that you have created a simple PDF document, start exploring the documentation and see how you can expand on this example to create more complicated PDF documents.
Published by smglo2006
Father of 3 strapping boys with lots of advice of what not to do. View profile
- How to Sign & Certify PDF Documents
- Low-cost Alternative to Adobe Acrobat for PDF Creation/editing
- Primo PDF - a Free Software
- Free Online PDF Creator Converts Almost Any File to PDF Format
- Creating Hot Display Ads for Your Offline Advertising Efforts
- Betrachten - a Great Full Screen PDF Viewer for Mac
- Bridging the Gap Between Java and XML




