2012-10-18 54 views
1

我有一个带有法国口音的简单字符串。我正在尝试使用ITextRenderer将其保存为pdf。问题是所有的口音都从pdf中删除。java保存pdf,带有重音符号的字符串

要保存的输入字符串来自我的速度模板。好了,我doinf StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)),并且这个过程给了我输入的字符串,如增刊é包换:签证& Pourboires”

我的代码:

  String documentHtml = "Supplément : à&egrave" 
     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

+0

什么字体你'正在使用? – user1516873

+0

你是指什么字体? – gospodin

+0

刚刚阅读这个http://stackoverflow.com/questions/1775008/embed-font-into-pdf-file-by-using-itext – user1516873

回答

1

尝试改变从UTF-8编码为ISO-8859-1

+0

如果我更改getBytes(“ISO-8859-1”)即时获取代码错误[STDERR] [致命错误]:66:7:3字节UTF-8序列的无效字节2。 11:57:12,109错误[PdfDocument]由于SAX导致文档错误:3字节UTF-8序列的无效字节2。 org.xml.sax.SAXParseException; lineNumber:66; columnNumber:7;无效的3字节UTF-8序列的字节2。 at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) – gospodin

+0

String documentHtml也必须位于您阅读的字符集中。对于这个String,它是使用的.java文件的编码。你的来源如何编码?也许改变他们的编码。 –

+0

以下是iText中编码示例的示例:http://itextpdf.com/examples/iia.php?id=198 –

相关问题