3

I am new to iText library. I have a requirement where i need to provide the output as PDF. The pdf has Arabic characters in it. I created a test servlet as given below.

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType ("application/pdf;charset=UTF-8");
Document document = new Document();
    try{
        PdfWriter.getInstance(document, 
            response.getOutputStream()); // Code 2
        document.open();

        Font f1;
  BaseFont bf  = BaseFont.createFont("C:\\WINDOWS\\Fonts\\ARIALUNI.ttf", BaseFont.CP1252, true);
  f1 = new Font(bf, 10);

        PdfPTable table = new PdfPTable(2);
        table.addCell("hellooooo1");
        table.addCell("world2");
        table.addCell("1113");
        table.addCell("422");

 // String a = "يبسبيبيبيسسسيبيببيسبيسيببي";
  String a = "سش";
  PdfPCell cell = new PdfPCell (new Paragraph (a,f1));
  table.addCell (cell);
  cell = new PdfPCell (new Paragraph ("Road",f1));
  table.addCell (cell);

        document.add(table);        
        document.close(); 
    }catch(DocumentException e){
        e.printStackTrace();
    }
}

The out put where we use the arabic characters are being displayed as ????? . How to rectify this problem? where i am making the mistake?

vikka
  • 53
  • 1
  • 8
  • Try replacing `CP1252` with `IDENTITY_H` – jmj May 16 '11 at 07:29
  • no success...Its still showing as Question marks only. – vikka May 16 '11 at 10:23
  • Just to be paranoid, try adding `a` directly to the document rather than as part of a table. If that doesn't work, we know it's not the PdfTable class causing a problem. – Mark Storer May 17 '11 at 20:58
  • no mark, i tried adding paragraph directly to the document with no luck. I replaced the font with a free new arabic font having the arabic glyphs. now i am getting some strange symbols instead of the arabic chars. – vikka May 18 '11 at 05:04

1 Answers1

3

Your problem is where you're creating the BaseFont with the Windows CP1252 Character Set, which is only suitable for latin characters. Try the encoding for Unicode instead:

 BaseFont bf  = BaseFont.createFont("C:\\WINDOWS\\Fonts\\ARIALUNI.ttf", BaseFont.IDENTITY_H, true);
stevevls
  • 10,675
  • 1
  • 45
  • 50
  • I have made the change. But still arabic characters showing as Question marks. – vikka May 16 '11 at 08:53
  • silly question, but you've verified that your font has arabic glyphs, right? another thing to try is passing your string as unicode literals (e.g. \u1610 etc.) to the Paragraph constructor to see if that's the spot where they're getting munged. – stevevls May 16 '11 at 10:53
  • Sorry for asking, How can i check my font is having arabic glyphs? i tried to use some fonts from my windows fonts directory, which gave me licensing error. Now i have downloaded some fonts which support's arabic and using them. – vikka May 17 '11 at 08:30
  • ok. my font is having arabic gylphs. i am able to use the font in word to type in arabic. – vikka May 17 '11 at 09:01
  • 1
    Run "charmap". It'll show all the available characters for all the fonts installed on your Windows machine. You can even sort by unicode subrange to make looking for Arabic easier. – Mark Storer May 17 '11 at 20:57
  • Thanks. The font that i am using is having arabic support. – vikka May 18 '11 at 05:06