Itext阿拉伯字体作为问号

我是iText图书馆的新手。 我有一个要求,我需要提供PDF格式的输出。 pdf中包含阿拉伯字符。 我创建了一个测试servlet,如下所示。

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

我们使用阿拉伯字符的输出显示为????? 。 如何纠正这个问题? 我在哪里弄错了?

您的问题是您使用Windows CP1252字符集创建BaseFont的地方,该字符集仅适用于拉丁字符。 请尝试使用Unicode编码:

  BaseFont bf = BaseFont.createFont("C:\\WINDOWS\\Fonts\\ARIALUNI.ttf", BaseFont.IDENTITY_H, true);