使用android pdf作家lib编写的音符 [英] Encoding of umlauts with the android pdf writer lib
问题描述
我正在使用 Android PDF Writer ,我仍然感到困惑。我的PDF渲染器必须用umlauts('ß','Ä','Ü')写字,并且它们在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);
//只写一些字符串到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不使用UTF-8,
要快速修复,您可以使用 StandardEncodings.WIN_ANSI_ENCODING
另一方面WINDOWS-1252
或ISO-8859-1
b $ b
I'm using the Android PDF Writer and I'm still confused. My PDF Renderer have to write words with umlauts ('ß', 'Ä', 'Ü') and they doesn't appear correctly in the pdf.
I think the issue is with the method getBytes(String encoding)
from the String class.
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");
When I inspect the pdfWriter.asString() the umlauts are present.
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) {
//
}
}
Maybe there is a problem within the getBytes() method?
You have the answer before your very nose: Your PDF doesn't use UTF-8, so the PDF viewer tries to decode your UTF-8 encoded file as MacRoman.
For a quick fix you can use StandardEncodings.WIN_ANSI_ENCODING
on one side and "WINDOWS-1252"
or "ISO-8859-1"
on the other.
这篇关于使用android pdf作家lib编写的音符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!