Skip to main content

Creating Pdf in multiple Languages in Java


Creating Pdf in multiple Languages in Java



This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator.
To use this sample code download itext.jar
To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project
Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font
(Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);)

You can download the source code from here




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 PDFGenerating {
public static void main(String arg[]) throws Exception {
try {
System.out.println("===Started=====>");
Document document = new Document();
String encoding = "Identity-H";
Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);
PdfWriter.getInstance(document, new FileOutputStream("C:/DemoSamples/pdf/hello.pdf"));
HeaderFooter header = new HeaderFooter(new Paragraph("Header", fontNormal), false);
HeaderFooter footer = new HeaderFooter(new Paragraph("footer", fontNormal), false);
document.setHeader(header);
document.setFooter(footer);
document.open();

Chunk chunkEnglish = new Chunk("Hello World " + "\n", fontNormal);
Chunk chunkRussian = new Chunk("привет мир " + "\n", fontNormal);
Chunk chunkBrazil = new Chunk("Olá Mundo " + "\n", fontNormal);
Chunk chunkBengali = new Chunk("হ্যালো ফোটোস " + "\n", fontNormal);
Chunk chunkArabic = new Chunk("مرحبا العالم" + "\n", fontNormal);
Chunk chunkChineese = new Chunk("你好 世界" + "\n", fontNormal);

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

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

PdfPCell cellRussian = new PdfPCell(new Phrase(chunkRussian));
table.addCell(new PdfPCell(new Phrase(new Chunk("Russian",fontNormal))));
table.addCell(cellRussian);

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

PdfPCell cellBengali = new PdfPCell(new Phrase(chunkBengali));
table.addCell(new PdfPCell(new Phrase(new Chunk("Bengali",fontNormal))));
table.addCell(cellBengali);

PdfPCell cellArabic = new PdfPCell(new Phrase(chunkArabic));
table.addCell(new PdfPCell(new Phrase(new Chunk("Arabic",fontNormal))));
table.addCell(cellArabic);

PdfPCell cellChineese = new PdfPCell(new Phrase(chunkChineese));
table.addCell(new PdfPCell(new Phrase(new Chunk("Chineese",fontNormal))));
table.addCell(cellChineese);
document.add(table);
document.close();

System.out.println("===Finished=====>");
} catch (Exception e) {
System.out.println("=====>" + e.getMessage());
e.printStackTrace();
}

}
}

======= PDF Created in Different Locale =======>

Comments

  1. Hi, i used the above code to generate russian text but i get output ??????????????????? .
    i double checkd the font and encoding.
    ANy help will be highly appreciated.

    ReplyDelete
  2. I have tested this code on my machine, it worked for me, can you please send your piece of code at yadavangad100@gmail.com

    ReplyDelete

Post a Comment

Popular posts from this blog

Uploading data on Remote Server in Java

This blog demonstrate how to upload data from file to remote server/database. Follow the steps as shown. 1. Create new Dynamic Web Project. 2. Now create all the packages as show in the image below. 3. Create controller with name ImportContact.java 4. Create pojo with name Contact.java 5. Create database layer code with name ImportContactBroker.java 6. Create connectivity with name DBConnection.java 7. Create db connectivity setting file as connectionSetting.java 8. Create jsp page with importcontact.jsp 9. Create supporting files like Constant.java and CsvReader.java 10. You can download the war file and import in your workspace. 11. MultipartRequest in Servlet and inside jsp helps to upload file on the remote server. 12. Download sample csv file from here . 13. Create Table with name Contact in Database as given in configuration file. 14.Just compile and run the code. ImportContact.java package com.uploadfile.controller; import java.io.BufferedReader; impo...

Executing executable Jar file from Java Program

Sample code to execute jar file from Java Program. Pre-requisite 1. Need to have executable jar file. 2. Copy the code in your workspace and Run the code. package com.test.reusable.enterprise.service; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.lang.exception.ExceptionUtils; public class ExecuteJarFile { public static void main(String[] args) { try { //Method1 to Execute Jar file From Java Program ProcessBuilder pb = new ProcessBuilder("java", "-jar", "HelloWorld.jar"); // command to execute jar file with fileName.jar pb.redirectErrorStream(true); pb.directory(new File("C:/www/")); // Directory path where your jar file is placed. Process p = pb.start(); ...