Write PDF file in Java using iText

In this tutorial we will see how to write PDF file in Java using iText library.

Review the code listed below. Running this program will generate a PDF file at location c:/itext.pdf. You can change the location in the program to suit your platform:

package com.quicklyjava;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

public class JavaWritePDF {

	 * @param args
	public static void main(String[] args) {
		try {
			File file = new File("c://itext.pdf");
			FileOutputStream pdfFileout = new FileOutputStream(file);
			Document doc = new Document();
			PdfWriter.getInstance(doc, pdfFileout);

			doc.addTitle("This is title");

			Paragraph para1 = new Paragraph();
			para1.add("This is paragraph 1");

			Paragraph para2 = new Paragraph();
			para2.add("This is paragraph 2");



		} catch (Exception e) {


Write PDF file in Java using iText code explanation:

1) The line below gets an instance of PDFWriter. The doc object instance can be subsequently used to add content to the PDF

PdfWriter.getInstance(doc, pdfFileout);

2) The lines below adds Author and Title to the PDF file.

			doc.addTitle("This is title");

3) The line below open the PDF document for writing.


4) The lines below create a Paragraph and add String content to the Paragraph.

Around The World Flights - Promo Code WORLD15
            Paragraph para1 = new Paragraph();
			para1.add("This is paragraph 1");

5) The line below add the Paragraph to the PDF document:


6) Once done with adding content, be sure to close the FileOutputStream and the Document:


Write PDF file in Java using iText Output:

Write PDF file in java using itext

Tutorials in this series:

You can download the eclipse project of the code explained in this tutorial:

JavaWritePDF (1.6MB)

7 responses to “Write PDF file in Java using iText”

  1. PDF Generation in Java: iText Tutorial - QuicklyJava

    [...] 3. Write PDF file in Java using iText [...]

  2. Read PDF file in Java using iText - QuicklyJava

    [...] 3. Write PDF file in Java using iText [...]

  3. Christian

    Thought it wouldn’t to give it a shot. I was right.

  4. natasha

    i prefer to use Aspose.PDF for Java its very secure API and since its using Java language its reliable also and you dont have to do the coding yourself it does it for you. here is the link if you wanna use this API:

  5. naranjo

    Thanks for the information, Can you help me please? I need to export the entire website to PDF file. What are the necessary changes?

  6. Create Password Protected PDF Files in Java Using Itext : QuicklyJava

    […] this tutorial, we will extend our previous example where we created a PDF file and attempt to Create Password protected PDF files in Java using […]

  7. Adding Image to PDF in Java Using Itext : QuicklyJava

    […] of the code here is similar to what we have already seen in the previous tutorials where we created PDF […]

Leave a Reply