Below is my code:
PDFDocument *pdfDocument = createNewPDFDocument();
PDFPage *pdfPage = createNewPDFPage(pdfDocument, 0, 800, 1200);
wstring wstr = L"ABCabc123一二三こんにちはมันดี";
FPDF_PAGEOBJECT pageObj = FPDFPageObj_NewTextObj(pdfDocument->fpdfDocument, "Arial", 28);
FPDFText_SetText(pageObj, charStringToFPDF_WIDESTRING2(wstr));
FPDFPageObj_Transform(pageObj, 1, 0, 0, 1, 100, 500);
FPDFPage_InsertObject(pdfPage->fpdfPage, pageObj);
FPDFPage_GenerateContent(pdfPage->fpdfPage);
savePDFDocumentToFile(pdfDocument, fd);
closePDFDocument(pdfDocument);
FPDF_WIDESTRING charStringToFPDF_WIDESTRING2(std::wstring &wstr) {
size_t length = sizeof(uint16_t) * (wstr.length() + 1);
auto *result(static_cast<unsigned short *>(malloc(length)));
auto *result_span = reinterpret_cast<uint8_t *>(result);
size_t i = 0;
for (wchar_t w : wstr) {
result_span[i++] = w & 0xff;
result_span[i++] = (w >> 8) & 0xff;
}
result_span[i++] = 0;
result_span[i] = 0;
return result;
}
UTF-16LE is two bytes, but it can only display characters represented by one byte, and other characters are displayed as ÿ, the encoding of ÿ is 255.
I tried a lot of combinations, but nothing works. Any help is much appreciated.