FPDFAnnot_SetStringValue Problems encountered

51 views
Skip to first unread message

Wilson Wade

unread,
Aug 20, 2021, 6:54:53 AM8/20/21
to pdfium
Hi, I try to add notes to the pdf file, But I ran into some problems, This string runs normally on Foxit Reader, but it displays garbled characters on other PDF readers. For example, Chrome.

Below is my code:

    PDFDocument *pdfDocument = createNewPDFDocument();
    PDFPage *pdfPage = createNewPDFPage(pdfDocument, 0, 800, 1200);

    FPDF_ANNOTATION fpdfAnnotation = FPDFPage_CreateAnnot(pdfPage->fpdfPage, FPDF_ANNOT_TEXT);
    FS_RECTF rect;
    rect.left = 35;
    rect.bottom = 150;
    rect.right = 53;
    rect.top = 165;

    FPDFAnnot_SetRect(fpdfAnnotation, &rect);
    wstring wstring1 = L"XY書き込む文字列";
    FPDFAnnot_SetStringValue(fpdfAnnotation, "Contents", wstringToFPDF_WIDESTRING(wstring1));

    FPDFPage_GenerateContent(pdfPage->fpdfPage);
    savePDFDocumentToFile(pdfDocument, fd);
    closePDFDocument(pdfDocument);
FPDF_WIDESTRING wstringToFPDF_WIDESTRING(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;
}

Any help is much appreciated.
correct.JPGwrong.JPG
test.pdf
Reply all
Reply to author
Forward
0 new messages