【发布时间】:2013-10-12 02:15:59
【问题描述】:
我正在使用 Android PDF Write(APW) 创建 PDF,但它不适用于某些特殊字符(葡萄牙语)。
mypdf.addText(170, 50, 40,"Coração");
标准的编码是:
mypdf.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
outputToFile("helloworld.pdf",pdfcontent,"ISO-8859-1");
我试过了
outputToFile("helloworld.pdf",pdfcontent,"UTF-8");
outputToFile("helloworld.pdf",pdfcontent,"UTF-16");
outputToFile("helloworld.pdf",pdfcontent,"Cp1252");
并没有成功。 有什么想法我应该怎么做?
编辑
方法 outputToFile 定义为:
private void outputToFile(String fileName, String pdfContent, String encoding) {
File newFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
try {
newFile.createNewFile();
try {
FileOutputStream pdfFile = new FileOutputStream(newFile);
pdfFile.write(pdfContent.getBytes(encoding));
pdfFile.close();
} catch(FileNotFoundException e) {
//
}
} catch(IOException e) {
//
}
}
方法addText定义为:
public void addText(int leftPosition, int topPositionFromBottom, int fontSize, String text, String transformation) {
addContent(
"BT\n" +
transformation + " " + Integer.toString(leftPosition) + " " + Integer.toString(topPositionFromBottom) + " Tm\n" +
"/F" + Integer.toString(mPageFonts.size()) + " " + Integer.toString(fontSize) + " Tf\n" +
"(" + text + ") Tj\n" +
"ET\n"
);
}
此外,我将字体颜色更改为白色,添加以下原始内容:
mypdf.addRawContent("1 1 1 rg\n");
然后我回到黑色字体颜色:
mypdf.addRawContent("0 0 0 rg\n");
【问题讨论】:
-
outputToFile做什么?有人要求快速查看 APW 的来源,这似乎表明它在字体和编码方面不是很灵活。 -
啊,我假设来自 PDFWriterDemo。首先:因为字符串包含偏移量引用,您不得使用多字节或可变字节数编码。实际上,APW 在这里返回的是字符串而不是字节数组,这是相当荒谬的。 Pdf 文档不是文本。话虽如此,您应该显示实际添加特殊字符的代码,包括用于创建和选择字体对象的代码。
-
好的,但是如何设置在
mypdf.addText(170, 50, 40,"Coração")行之前使用的字体?我问这个的原因是,根据您选择的字体,您的字符串所需的编码可能会有所不同,并且您的字符可能根本无法选择!例如。 ã (atilde) 在内置编码中可能有八进制值 213、343 或 343(MacRomanEncoding、WinAnsiEncoding、PDFDocEncoding,参见 PDF 的附件 D specification),并且您使用的字体必须包含字符开始。