PDF Generation in Java using iText JAR

This post is about how to create pdf with iText jar. The code snippet is

try {
OutputStream file = new FileOutputStream(new File(“E:\CreateTest.pdf”));
Document doc = new Document();
PdfWriter.getInstance(doc, file);
doc.open();
doc.add(new Paragraph(“Hi! Welcome to my blog”));
doc.add(new Paragraph(new Date().toString()));
doc.close();
file.close();
System.out.println(“Done”);
}
catch (DocumentException | IOException e) {
e.printStackTrace();
}

Jar Link: https://mvnrepository.com/artifact/itext/itext/4.2.1

Code Flow:

Create a new pdf file named as CreateTest.
Create a object for document(iText jar api)
Call getInstance for PDFWriter and pass document object and outputstrem object as parameters.
Call document object to open the document
Add some message, date by using the Paragraph instance.
Close document and file object.

Hope this helps. Please subscribe and comment if any doubts.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s