Itext Arabic Font coming as question marks

只谈情不闲聊 提交于 2019-12-31 03:08:04

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/6014211/itext-arabic-font-coming-as-question-marks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!