Generate PDF files in Java on the fly

September 29th, 2009No Comments

iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won’t use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you’ll build iText into your own applications so that you can automate the PDF creation and manipulation process. For instance in one or more of the following situations:

* Due to time or size, the PDF documents can’t be produced manually.
* The content of the document must be calculated or based on user input.
* The content needs to be customized or personalized.
* The PDF content needs to be served in a web environment.
* Documents are to be created in “batch process” mode.

You can use iText to:

* Serve PDF to a browser
* Generate dynamic documents from XML files or databases
* Use PDF’s many interactive features
* Add bookmarks, page numbers, watermarks, etc.
* Split, concatenate, and manipulate PDF pages
* Automate filling out of PDF forms
* Add digital signatures to a PDF file
* And much more

The library is easy to use and can produce great results with only little programming. The basic usage is pretty simple. Let’s take a look at a small “Hello World” application:

Document document = new Document();
try {
    PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));

    document.open();
    document.add(new Paragraph("Hello World"));
} catch (Exception e) {

}
document.close();

Creating a new document object will initialize a new PDF document with A4 dimensions. The PdfWriter will be used to write the resulting document to an OutputStream, which is in the above example a file. After opening the document you can just add new paragraphs to the document.

Related Posts:

Tagged : ,

Comments are closed.