我有一个带有法国口音的简单字符串。我正在尝试使用ITextRenderer将其保存为pdf。问题是所有的口音都从pdf中删除。java保存pdf,带有重音符号的字符串
要保存的输入字符串来自我的速度模板。好了,我doinf StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)),并且这个过程给了我输入的字符串,如增刊é包换:签证& Pourboires”
我的代码:
String documentHtml = "Supplément : àè"
DocumentBuilder builder;
try {
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setFeature("http://xml.org/sax/features/namespaces", false);
fac.setFeature("http://xml.org/sax/features/validation", false);
fac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
fac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
builder = fac.newDocumentBuilder();
byte[] docByte = documentHtml.getBytes("UTF-8");
ByteArrayInputStream is = new ByteArrayInputStream(docByte);
Document doc = builder.parse(is);
is.close();
File file = new File(this.getFolder(), this.getFileName());
if (file.exists()) {
file.delete();
}
// save pdf
OutputStream os = new FileOutputStream(file);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, file.getParentFile().getAbsolutePath());
renderer.layout();
renderer.createPDF(os, true);
os.close();
return this.getFolder().getAbsolutePath() + "/" + this.getFileName();
} catch (ParserConfigurationException e) {
LOGGER.error("Error while parsing the configuration " + e.getMessage(), e);
throw new BOServiceException("Error while parsing the configuration : " + e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
LOGGER.error("Encoding error : " + e.getMessage(), e);
throw new BOServiceException("Encoding error : " + e.getMessage(), e);
} catch (SAXException e) {
LOGGER.error("Error in the document because of SAX : " + e.getMessage(), e);
throw new BOServiceException("Error in the document because of SAX : " + e.getMessage(), e);
} catch (IOException e) {
LOGGER.error("Error due to io problem : " + e.getMessage(), e);
throw new BOServiceException("Error due to io problem :" + e.getMessage(), e);
}
所以u有想法,为什么我的编码不工作,为什么结果中的PDF格式,我不能看到这样的字符和agrave;。& egrave
什么字体你'正在使用? – user1516873
你是指什么字体? – gospodin
刚刚阅读这个http://stackoverflow.com/questions/1775008/embed-font-into-pdf-file-by-using-itext – user1516873