Generating PDF report is the very general requirement in most of the Java projects. iText is the most popular PDF API used by the Java developers for generating the PDF report. It is very simple and easy to get started writing simple PDF files. If you are familiar with iText API, it provides more advanced features to format the PDF output as you need. This tutorial explains with simple Java program that helps you to generate a PDF file with sample text and table. If you have any questions, please write it in the comments section.
1. Add iText Libraries
You can download the iText jar files from here. There is three types of JAR files.
- itextpdf-x.y.z.jar: the core library
- itext-xtra-x.y.z.jar: extra functionality
- itext-pdfa-x.y.z.jar: PDF/A-related functionality
2. Create PDF Conversion Utility Java File
package javabeat.net.pdf; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class PDFConversionDemo { public static void main(String[] args) { try { OutputStream file = new FileOutputStream(new File("SamplePDF.pdf")); Document document = new Document(); PdfWriter.getInstance(document, file); document.open(); document.add(new Paragraph("First iText PDF")); document.add(new Paragraph(new Date().toString())); document.addAuthor("Krishna Srinivasan"); document.addCreationDate(); document.addCreator("JavaBeat"); document.addTitle("Sample PDF"); //Create Paragraph Paragraph paragraph = new Paragraph("Title 1",new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD)); //New line paragraph.add(new Paragraph(" ")); paragraph.add("Test Paragraph"); paragraph.add(new Paragraph(" ")); document.add(paragraph); //Create a table in PDF PdfPTable pdfTable = new PdfPTable(3); PdfPCell cell1 = new PdfPCell(new Phrase("Table Header 1")); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); pdfTable.addCell(cell1); cell1 = new PdfPCell(new Phrase("Table Header 2")); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); pdfTable.addCell(cell1); cell1 = new PdfPCell(new Phrase("Table Header 3")); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); pdfTable.addCell(cell1); pdfTable.setHeaderRows(1); pdfTable.addCell("Row 1 Col 1"); pdfTable.addCell("Row 1 Col 2"); pdfTable.addCell("Row 1 Col 3"); pdfTable.addCell("Row 2 Col 1"); pdfTable.addCell("Row 2 Col 2"); pdfTable.addCell("Row 2 Col 3"); document.add(pdfTable); document.close(); file.close(); } catch (Exception e) { e.printStackTrace(); } } }
3. Project Structure
It is simple Java project. Look at the project folder structure.
4. Output
If you run the above program, you would have a new PDF generated in the location mentioned above section. The PDF would look like this.