'Unable to highlight/select textField after filling it with a value using pdf-lib

I want to dynamically update fields inside some pdf files and use them. For the sake of user experience, I want the fields to be able to be highlighted so it's easier to copy/paste so I don't have to input the values later manually... but I have been unsuccessful in finding a solution.

Here's my code that fills out the textFields using pdf-lib:

const pdf = await PDFLib.PDFDocument.load(filedata);
const form = pdf.getForm();

var textField = form.getTextField(TEXTFIELD_NAME);

textField.setMaxLength(20);

textField.setText("Some value");

textField.enableReadOnly();
const pdfToBytes = await pdf.save();

And then I create a file using "pdfToBytes", but when opening the PDF using any PDF viewer, I'm unable to highlight/select the value inside the textField.

I have tried textField.disableReadonly, but I actually want the field to be read only, and it doesn't really work in all PDF viewers when doing so anyways.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source