Cannot remove multiline form field
What were you trying to do?
I am trying to merge pdf documents with form fields. It does not work with forms very well as they become very unstable in the merge document, or disappear. My workaround is to get all of the form data, strip all the form fields, merge all the documents and try to apply the form data back on the merged document. Multiline text fields fail on form.removeField(field)
How did you attempt to do it?
Running node v14.18.3
multiline.pdf
module.exports.removeFormFieldsPDF = async (filePath = './multiline.pdf') => {
const pdfDoc = await PDFDocument.load(fs.readFileSync(filePath));
const pages = pdfDoc.getPages();
const form = pdfDoc.getForm();
const formFields = form.getFields();
try {
for (const field of formFields) {
form.removeField(field);
}
} catch (error) {
console.log(error);
}
//SEEMS TO NOT BE ABLE TO REMOVE Multiline textfields
fs.writeFileSync("../build/test.pdf", await pdfDoc.save());
};
What actually happened?
Error: Unexpected N type: undefined
at PDFWidgetAnnotation.PDFAnnotation.getNormalAppearance (C:\dev\apps\pts-forms\node_modules\pdf-lib\cjs\core\annotation\PDFAnnotation.js:60:15)
at PDFForm.findWidgetAppearanceRef (C:\dev\apps\pts-forms\node_modules\pdf-lib\cjs\api\form\PDFForm.js:608:32)
at PDFForm.removeField (C:\dev\apps\pts-forms\node_modules\pdf-lib\cjs\api\form\PDFForm.js:489:34)
at module.exports.removeFormFieldsPDF (C:\dev\apps\pts-forms\utils\pdf.js:195:14)
(node:13664) UnhandledPromiseRejectionWarning: Error: Expected instance of PDFDict or PDFStream, but got instance of undefined
What did you expect to happen?
Remove multi line text field.
How can we reproduce the issue?
Run multiline.pdf With my function, and it will produce an error
Version
1.17.1
What environment are you running pdf-lib in?
Node
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
No response

i didn't solve the problem with remove multiline form field so i removed all forms with
const form = pdfDoc.getForm();
form flatten();
more details here https://pdf-lib.js.org/docs/api/classes/pdfform#flatten maybe it will be useful for someone
I found a solution which might work also for you
while (field.acroField.getWidgets().length) {
field.acroField.removeWidget(0);
}
form.removeField(field);
I found a solution which might work also for you
while (field.acroField.getWidgets().length) { field.acroField.removeWidget(0); } form.removeField(field);
Thanks This allowed me to remove all types of fields
I found a solution which might work also for you
while (field.acroField.getWidgets().length) { field.acroField.removeWidget(0); } form.removeField(field);
Excellent 👍 Thank you!!
I found a solution which might work also for you
while (field.acroField.getWidgets().length) { field.acroField.removeWidget(0); } form.removeField(field);
OMG yes! I owe you.