How can I add text to the PDF file correctly, please help me, thanks!

661 views
Skip to first unread message

Wade

unread,
Jul 28, 2021, 12:12:54 PM7/28/21
to pdfium
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.
test.JPG
test.pdf

Lei Zhang

unread,
Jul 28, 2021, 12:57:32 PM7/28/21
to Wade, pdfium
Instead of using FPDFPageObj_NewTextObj(), which ended up creating test.pdf without any embedded fonts, I recommend trying FPDFText_LoadFont() to load the font you actually want to use, followed by FPDFPageObj_CreateTextObj().

After that, try calling FPDFText_SetText(), or the new FPDFText_SetCharcodes() experimental API we just added yesterday.

--
You received this message because you are subscribed to the Google Groups "pdfium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/c4b313d4-70c7-4088-b528-c2e327f673cdn%40googlegroups.com.

Wade

unread,
Jul 29, 2021, 4:31:36 AM7/29/21
to pdfium
Thank you lei, I have found the reason. The reason is that the loaded font does not support these characters.

In addition, is there any good way to reduce the size of the font file loaded in the pdf file?

for example:
1. Use system fonts, do not load into the pdf file
2. Split the font file and only use the text used, like NotoSansSC-Regular.subset.otf in the pdfium demo

Lei Zhang

unread,
Jul 29, 2021, 10:40:42 AM7/29/21
to Wade, pdfium
You need a separate font subsetter, which is outside the scope of PDFium.

Wilson Wade

unread,
Jul 29, 2021, 9:37:32 PM7/29/21
to Lei Zhang, pdfium
Ok, thanks. 

Lei Zhang <the...@chromium.org> 于2021年7月29日周四 下午10:40写道:
Reply all
Reply to author
Forward
0 new messages