【问题标题】:Encoding of umlauts with the android pdf writer lib使用 android pdf writer 库对变音符号进行编码
【发布时间】:2015-02-17 14:53:50
【问题描述】:

我正在使用Android PDF Writer,但我仍然感到困惑。我的 PDF 渲染器必须使用变音符号('ß'、'Ä'、'Ü')编写单词,并且它们在 pdf 中显示不正确。

我认为问题出在 String 类中的方法 getBytes(String encoding) 上。

PDFWriter pdfWriter = new PDFWriter(PaperSize.A4_WIDTH, PaperSize.A4_HEIGHT);
pdfWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.SANS_SERIF, StandardFonts.MAC_ROMAN_ENCODING);
// only write some strings into the pdfwriter
parseData(pdfWriter);
outputToFile(filename, pdfWriter.asString(), "UTF-8");

当我检查 pdfWriter.asString() 时,变音符号出现了。

private void outputToFile(String fileName, String pdfContent, String encoding) {
    File newFile = new File(fileName);
    Log.v(Constants.LOG_TAG, newFile.getAbsolutePath());
    try {
        newFile.createNewFile();
        try {
            FileOutputStream pdfFile = new FileOutputStream(newFile);
            pdfFile.write(pdfContent.getBytes("UTF8"));
            pdfFile.close();
        } catch(FileNotFoundException e) {
            //
        }
    } catch(IOException e) {
        //
    }
}

也许 getBytes() 方法有问题?

【问题讨论】:

  • 您应该将 PDF 库视为概念验证,但尚未准备好用于生产用途。它有许多缺陷,您的编码问题就是其中之一,但更糟糕的是,生成的 PDF 在某些方面无效。 PDF 查看器通常不会抱怨,但一旦您的 PDF 被其他软件后期处理,这些错误通常会弹出。
  • 可能最好的方法是开发一个支持原生 pdf 的更高级别的 android api。否则 Karol S 对我帮助很大。

标签: java android pdf encoding character-encoding


【解决方案1】:

您的答案就在眼前:您的 PDF 不使用 UTF-8,因此 PDF 查看器会尝试将您的 UTF-8 编码文件解码为 MacRoman。

为了快速修复,您可以在一侧使用 StandardEncodings.WIN_ANSI_ENCODING,在另一侧使用 "WINDOWS-1252" 或 "ISO-8859-1"。

【讨论】:

  • 谢谢。在这个问题上我浪费了半天的时间。
  • 这可能会破坏手头文件中的 PDF 交叉引用。另一方面,无论如何,它们都被这个库破坏了... ;)
猜你喜欢
  • 2016-04-25
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多