iText Tutorial / Example

iText Tutorial describes about creating pdf documents using Java and iText.

Creating PDF files in enterprise web applications is quite common in these days.

iText is a free and open-source tool for manipulating and creating PDF files in Java.

It had been written by Paulo Soares, Bruno Lowagie and others. It allows developers looking to boost web applications with dynamic PDF content  manipulation.

You can see the below example, which is demonstrating How to Use IText in java for generating PDF

pom.xml

You need to have following dependencies

<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>kernel</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>io</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>layout</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>forms</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>pdfa</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>pdftest</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.7.18</version>
</dependency>

Create PDF With iText

import java.util.Date;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;



// Java PDF Generation With Itext

public class CreatePDFWithItext{

  public static void main(String[] args) {
    try {
      PdfDocument pdfDoc = new PdfDocument(new PdfWriter("C:\\example.pdf"));
      Document document = new Document(pdfDoc);
      document.add(new Paragraph("Hello World"));
      document.add(new Paragraph("https://www.javatips.net/"));
      document.add(new Paragraph(new Date().toString()));
      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Output










Your email address will not be published. Required fields are marked *