In this article, we learn how to create PDF files using iTextPDF’s Java library. This tutorial includes Video guide that explains how to add iTextPDF library to your eclipse project. And later you learn how to use this in order to create a PDF file. If you prefer text instructions then keep reading. If you prefer to watch video then take a look at the video below.
Let’s start the tutorial with Installation of iText.
- Go to iText PDF library files.
- Extract the content of zip file.
- Create a folder and copy the contents of zip folder.
- Open Eclipse IDE.
- Create a new Java project.
- Add the itext jar files in class path.
- Finish the project creation wizard.
- That’s it.
Once you have created the Eclipse Java project and added itext jar files. Next step is to focus on the code. Here we have to create a document. After creating a document we have to push the content inside the document. This helps us to properly format before exporting that document to PDF format. So let’s see how this can be done with the code. Check out the code below.
public class Demo
public static void main(String args)
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“demo.pdf”));
document.add(new Paragraph(“This is Demo PDF”));
} catch (DocumentException e)
} catch (FileNotFoundException e)
Code: In first statement, we have created pdfwriter instance and created pdf document. In second statement we have opened the created document. In next statement we have added the content inside the pdf document. And finally we closed the document. We have placed this code inside try and catch block to avoid file exceptions.
You can copy the code given above and paste it inside your Eclipse project class. Just watch out with the classname in this code, and change it to your project’s classname. Make that change and any other change in the output that you want. Now the next step is to run the code.
When you run the code, eclipse tries to export the PDF using iText. And the file is stored in the same folder to that of the project files. This was just an example. And there are many other things that can you do with the document. You can format it for the table. Alternatively you can use it to export the database tables to the PDF document. There are styles that you can apply to the document. In this case however I suggest you to check out the documentation of the iText PDF library. In future I may cover some of the examples for the iText library that may include styling and the form handling along with the database export options.
If you like this short tutorial, then do let me know. Also check out the video above to see the code in action.Don’t forget to share the article with your friends on social media. If you have any questions or suggestions regarding the video or the code, then do let me know. 🙂