【发布时间】: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