hi, I am trying to fill form text fields with special characters such as Chinese, and the version is
1.0.1, i have tried multiple ways but didn't work, the text result will be unintelligible text, here is the code:
```js
const pdfDoc = await PDFDocument.load(res.data);
const acroForm = await pdfDoc.context.lookup(
pdfDoc.catalog.get(PDFName.of('AcroForm')),
);
const acroFields = await pdfDoc.context.lookup(
acroForm.get(PDFName.of('Fields')),
);
const acroFields_ = acroFields.array.map((acroField) => pdfDoc.context.lookup(acroField));
acroFields_.forEach((acroField) => {
let name = acroField.get(PDFName.of('T')).value;
acroField.set(PDFName.of('V'), PDFString.of('涓枃涓枃涓枃'));
});
window.open(
URL.createObjectURL( new Blob([await pdfDoc.save()], { type: 'application/pdf' }))
)
transfer to utf-16 and success:
```js
acroField.set(PDFName.of('V'), PDFString.of('xffxfe-Nx87e'));
sorry to bother, another problem, the form filled by:
acroField.set(PDFName.of('V'), PDFString.of('balabala'));
some of those form item don't show the value directly, it only shows when i click(focus it), don't know how to fix that
Hello @xia0ming! I've created an example script here that addresses both of these problems.
The problem of special characters can be solved by using the PDFHexString.fromText method:
acroField.set(PDFName.of('V'), PDFHexString.fromText(text));
And the second problem you mentioned (having to click/focus a field to see its contents in certain readers) can be solved by enabling NeedAppearances on the AcroForm:
acroForm.set(PDFName.of('NeedAppearances'), PDFBool.True);
I hope this helps. Please let me know if you have any additional questions!
acroForm.set(PDFName.of('NeedAppearances'), PDFBool.True);
Doesn't seem to work for macOS Preview.
macOS 10.15
Preview 11
@onedotover Can you please provide some sample PDFs and code that I can use to reproduce your issue? My example is working fine for me on a slightly older version of Preview:
Preview: 10.1
macOS: 10.14.5
I can confirm your PDF and script work in Preview for me as well.
It looks like it has something to do with the form fields in the PDF file I have. Interestingly, if I just copy the field and change the name, it works. Inspecting the form fields in Acrobat shows no differences (outside of their name) but inspecting in my debugger I can see one difference between the fields. The one with an issue has a /AP operator.
See attached. I modified your example code. The only changes are with the field names.
@onedotover I took a look at the code and document you shared. The reason Preview is not rendering anything for that field is that it has an appearance stream specified for it (that's the AP entry you noted). This appearance stream, however, is empty:
18 0 obj
<<
/BBox [ 0 0 224.88 17.28 ]
/FormType 1
/Length 12
/Matrix [ 1 0 0 1 0 0 ]
/Resources << /ProcSet [ /PDF ] >>
/Subtype /Form
/Type /XObject
>>
stream
/Tx BMC
EMC
endstream
endobj
So when Preview encounters this stream, it does what the stream says to do: it renders nothing.
You might wonder why Acrobat doesn't do the same thing. Well, as far as I can tell, it comes down to a discrepancy in how different readers treat the NeedAppearances flag. Preview seems to interpret it as "If the field has an appearance stream, then use it. Otherwise, make your own." Whereas Acrobat seems to think it means "Make your own appearance streams no matter what."
You can solve this problem in one of two ways:
NeedAppearances and create your own appearance streamsNeedAppearances and delete the AP entry of all your fieldsEither solution should ensure consistency across readers.
Thank you for looking into this (and the detailed response). Adding this fixed the problem for me:
acroField.delete(PDFName.of('AP'));
Thanks for all your work on this project!
pdf-lib now has form creation and filling APIs that should be used instead of the above example(s). They automagically handle creating the appearance streams for you. See the form filling JSFiddle for a working example. Additional information is available in the README and API docs. See also Creating and Filling Forms for an example of filling form fields with a custom font and special characters.
Most helpful comment
Thank you for looking into this (and the detailed response). Adding this fixed the problem for me:
Thanks for all your work on this project!