问题
I have to write German text on a pdf created by Libharu. I assign German Text to a string variable (i.e. std::string TestString = "VariableGesamtlänge";
) and then put that text to a pdf. My simple code is following:
//-----UTF8 Encoding
HPDF_UseUTFEncodings(pdf);
HPDF_SetCurrentEncoder(pdf, "UTF-8");
const char *fontname = HPDF_LoadTTFontFromFile(pdf, "FreeSans.ttf", HPDF_TRUE);
HPDF_Font font = HPDF_GetFont(pdf, fontname, "UTF-8");
HPDF_Page_SetFontAndSize(page, font, 24);
std::string TestString = "VariableGesamtlänge";
DrawText(page, font, TestString.c_str(), y);
Problem: I get two square boxes instead of ä
. I am using VS2010.
回答1:
'ä' is not an ASCII character. It may be stored as a single character (in which case, which one?), or it may be stored as multiple characters (in which case, which ones?).
You have told the HPDF functions that you are going to pass text around as UTF-8 (which is an entirely sensible choice). This means 'ä' is represented by 0xC3 0xA4.
The source file is almost certainly encoded in 8-bit text, using (probably) code-page 1252. So 'ä' will be the single character 0xE4. You either need to tell the compiler to store strings as UTF-8, or it may be possible to re-encode the source files in UTF-8.
Your final option is to store the text in a (UTF-8) file, and read it from there.
来源:https://stackoverflow.com/questions/41125242/german-text-not-proper-on-pdf-created-by-libharu-c