Tutorial #97: Generate multi-language PDF using iText in Java

Hi everyone,

iText is a PDF software that can be used to create PDF documents dynamically using Java, .NET, Android etc. Based on the comments I received of late, iText can be used to generate multi-language PDF documents for Java, web and other applications.

iTextG is a special version of iText built for Android and Google App Engine.

Differences between the iText core library and the Android/GAE port are as follows,

1. iTextG uses SpongyCastle instead of Bouncy Castle. Make sure to download and include the right jars namely scprov-jdk15on and scpkix-jdk15on.

2. References to any of the classes not on the Google App Engine whitelist have been removed.

Through this tutorial, we will learn how to generate a multi-language PDF using the iText library in Java.

Pre-requisites: Eclipse IDE, Windows 7 (64 bit), iText software ( itext-2.1.7.jar)

Launch Eclipse IDE and create a new Java console application project called JavaPDFGeneration with package name com.app.generate. Create a new Java class called GeneratePDF and add the following code!


package com.app.pdfgenerate;

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class GeneratePDF {
    public static void main(String arg[]) throws Exception {
       try {
            Document document = new Document();
			String encoding = "Identity-H";
			Font fontNormal = FontFactory.getFont(("F:/programs/pdf/fonts/Roboto-Black.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);
			PdfWriter.getInstance(document, new FileOutputStream("F:/programs/pdf/test.pdf"));
			HeaderFooter header = new HeaderFooter(new Paragraph("Header", fontNormal), false);
			HeaderFooter footer = new HeaderFooter(new Paragraph("footer", fontNormal), false);

			Chunk chunkEnglish = new Chunk("Hello World " + "\n", fontNormal);
			Chunk chunkBrazil = new Chunk("Olá Mundo " + "\n", fontNormal);

			PdfPTable table = new PdfPTable(2);
			table.addCell("Translated Text");

			PdfPCell cellEnglish = new PdfPCell(new Phrase(chunkEnglish));
			table.addCell(new PdfPCell(new Phrase(new Chunk("English",fontNormal))));

			PdfPCell cellBrazil = new PdfPCell(new Phrase(chunkBrazil));
			table.addCell(new PdfPCell(new Phrase(new Chunk("Brazil",fontNormal))));


			System.out.println("PDF generation complete....");
		  } catch (Exception e) {
			System.out.println("Error occurred while generating PDF" + e.getMessage());

Save all changes and run the application. If no errors occur then you should see the PDF file (test.pdf) created in the specified output directory.


Reference: iText library

Karan Balkar About Karan Balkar
self proclaimed extraterrestrial fandroid, computer engineer, amateur gamer and die hard punk rock fan!

Leave a Reply

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