我在使用iText PDF Library导出PDF时遇到了字符编码问题,尽管我将导出器的 CHARACTER_ENCODING 属性设置为 UTF-8 ,如下所示:
Class clazz = Class.forName(User.class);
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
addMetaData(document);
addDate(document);
addTitlePage(document, className);
addEmptyLineToDocument(document, 2);
addContent(document, className, clazz);
addEmptyLineToDocument(document, 2);
addSignature(document);
return document;
以下是 addContent 方法示例:
private static FontFamily fontFamily = FontFamily.TIMES_ROMAN;
Font fontNormal10 = new Font(fontFamily, 10, Font.NORMAL);
Paragraph paragraph = new Paragraph();
Chunk chunk = new Chunk("Here is the list of " + className, fontNormal10);
paragraph.add(chunk);
// continues
然后我用ServletOutputStream编写文档:
// **document** => which I created and returned above
document.close();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFileName);
response.setContentType("application/pdf; charset=UTF-8");
response.setContentLength(baos.size());
ServletOutputStream outputStream = response.getOutputStream();
baos.writeTo(outputStream);
outputStream.flush();
outputStream.close();
有什么想法吗?提前谢谢..