Skip to content

Cannot remove multiline form field #1168

Description

@jelizarovas

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

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions