且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用itext将文本文件转换为pdf时设置编码

更新时间:2023-02-20 10:38:48

当我查看你的代码时,我看到很多奇怪的东西。

When I look at your code, I see a number of things that are odd.


  1. 你说你需要UTF-8,但你使用 BaseFont.CP1252 $ c创建一个 BaseFont 对象$ c>而不是 BaseFont.IDENTITY_H (这是使用Unicode时所需的编码)。

  2. 您使用标准的Type 1字体Courier,这是一种不知道如何渲染é,è,à...以及从未嵌入的字体的字体。如上所述,在这种情况下会忽略 BaseFont.EMBEDDED 参数!

  3. 您不要将此字体与具有的对象一起使用实际内容。实际内容放入使用默认字体Helvetica创建的段落,这是一种不知道如何渲染é,è,à的字体...

  1. You say you require UTF-8, but you create a BaseFont object using BaseFont.CP1252 instead of BaseFont.IDENTITY_H (which is the "encoding" you need when you work with Unicode).
  2. You use the standard Type 1 font Courier, which is a font that doesn't know how to render é,è,à... and a font that is never embedded. As documented, the BaseFont.EMBEDDED parameter is ignored in this case!
  3. You don't use this font with an object that has actual content. The actual content is put into a Paragraph that is created using the default font "Helvetica", a font that doesn't know how to render é,è,à...

要解决此问题,您需要使用适当的字体创建段落 。这是 NOT 标准的1型字体,但类似 courier.ttf 。您还需要使用适当的编码: BaseFont.IDENTITY_H

To solve this, you need to create the Paragraph with the appropriate font. That is NOT a standard type 1 font, but something like courier.ttf. You also need to use the appropriate encoding: BaseFont.IDENTITY_H.