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();
}
}
我们使用阿拉伯字符的输出显示为 ?????? 。 如何纠正这个问题?我在哪里犯了错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您使用 Windows CP1252 字符集创建 BaseFont,该字符集仅适用于拉丁字符。尝试使用 Unicode 编码:
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: