How to create PDF File in Java – iText Example
This is also one of the top 20 Java libraries I recommend Java programmer to learn. Learning these libraries will greatly improve your ability as a Java developer.
Getting iText
You can either download iText jar from its home page http://www.lowagie.com/iText/download.html
iText core: iText-5.5.13.jar
or you can also download from the Maven repository by adding iText dependency on your pom.xml file.
iText Maven Dependency
com.itextpdf
iText Gradle Dependency
compile group: 'com.itextpdf' , name: 'itextpdf' , version: '5.5.13'
How to create PDF files/documents in Java – iText library Example
Here is a complete code example to generate a PDF file using the
iText library in Java.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
* Java Program to generate PDF document using iText library.
* @author Javin
public class Testing <
public static void main(String args[]) <
OutputStream file = null ;
file = new FileOutputStream( new File( "Contacts.pdf" ));
// Create a new Document object
Document document = new Document();
// You need PdfWriter to generate PDF document
PdfWriter.getInstance(document, file);
// Opening document for writing PDF
document.open();
// Writing content
document.add( new Paragraph( "Hello World, Creating PDF document in Java is easy" ));
document.add( new Paragraph( "You are customer # 2345433" ));
document.add( new Paragraph( new Date( new java.util.Date().getTime()).toString()));
// Add meta data information to PDF file
document.addCreationDate();
document.addAuthor( "Javarevisited" );
document.addTitle( "How to create PDF document in Java" );
document.addCreator( "Thanks to iText, writing into PDF is easy" );
// close the document
document.close();
System.out.println( "Your PDF File is succesfully created" );
> catch (Exception e) <
e.printStackTrace();
// closing FileOutputStream
if (file != null ) <
> catch (IOException io) < /*Failed to close*/
Your PDF File is successfully created
Adding metadata/Setting attributes of PDF using free Java-PDF library
While you generate a PDF, you may want to set its different attribute like: author name, title, file description etc. iText jar can help you to set different attributes of a PDF file. Document object provide different methods to add various attributes to a PDF file.
document.addCreationDate();
document.addAuthor( "" );
document.addTitle( "How to create PDF document in Java" );
document.addCreator( "Thanks to iText, writing into PDF is easy" );
That’s all on how to generate PDF files in Java using iText. Lots of open source framework uses iText to provide PDF support in their application. for example display tag, which is a popular JSP tag library to generate dynamic HTML tables, allow you to export table in PDF format. It does this conversion by using iText library, so all you need is to include iText.jar in your classpath and boom, you can export PDF documents from Java application.
If you like this article, you may find the following articles interesting as well:
- The 2020 Java Developer RoadMap
- 10 Frameworks Java and Web Developers should learn
- 10 Best Courses to learn Spring Framework
- 10 Things Java Developer Should Learn in 2020
- My favorite courses to learn Software Architecture
- 10 Programming Languages to explore in 2020
- 10 Ways to Learn a New Technology in 2020
- 10 PluralSight Courses for Java and Web Developers
- 21 Books Java Developers should read in 2020
- Data Structure and Algorithms Courses to Crack Coding Interview
- 20 Java Books You Can Read in 2020
- 10 Tutorials to Learn Java 8 Better
- 10 tips to become a better Java Programmer
- 10 Tools Java Developer Should Learn in 2020
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues too. If you have any feedback or questions then please share your thoughts by commenting, I highly appreciate your comment and try to reply as soon as possible.
Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: How to create PDF File in Java – iText Example
Opinions expressed by Java Code Geeks contributors are their own.