Fill and sign PDF forms using JavaScript
Nutrient Web SDK enables you to fill and sign forms in a PDF document both programmatically and manually through the UI.
Filling in fields
For standard fields such as text boxes and radio buttons, you can set their values using the nutrientviewer.instance#setFormFieldValues method.
In the example below, the value of all text fields is updated:
try { const formFields = await instance.getFormFields();
// Update the value of all text form fields. const updatedFormFieldValues = formFields .filter( (formField) => formField instanceof NutrientViewer.FormFields.TextFormField ) .reduce((values, formField) => { values[formField.name] = "New Value"; return values; }, {});
await instance.setFormFieldValues(updatedFormFieldValues); console.log("Form fields updated successfully");} catch (error) { console.error("Failed to update form fields:", error.message);}Signing
When a user clicks on an unsigned signature form field, the signature UI pops up so they can create an ink, image, or typed signature. Once they create it, Nutrient overlays the signature and fits it in the same location as the signature field.
You can do this programmatically by creating and positioning the ink annotation yourself. The following example shows how to do this:
try { // The name of the field you want. const formFieldName = "signature";
// First get all `FormFields` in the `Document`. const formFields = await instance.getFormFields();
// Get a signature form with the specific name you want. const field = formFields.find( (formField) => formField.name === formFieldName && formField instanceof NutrientViewer.FormFields.SignatureFormField );
if (!field) { throw new Error(`Signature field "${formFieldName}" not found`); }
// In this example, assume the widget you need is on the first page. const annotations = await instance.getAnnotations(0);
// Find that widget. const widget = annotations.find( (annotation) => annotation instanceof NutrientViewer.Annotations.WidgetAnnotation && annotation.formFieldName === field.name );
if (!widget) { throw new Error(`Widget for field "${formFieldName}" not found on page 0`); }
// Make a new ink annotation. const annotation = new NutrientViewer.Annotations.InkAnnotation({ pageIndex: 0, lines: NutrientViewer.Immutable.List([ NutrientViewer.Immutable.List([ new NutrientViewer.Geometry.DrawingPoint({ x: widget.boundingBox.left + 5, y: widget.boundingBox.top + 5 }), new NutrientViewer.Geometry.DrawingPoint({ x: widget.boundingBox.left + widget.boundingBox.width - 10, y: widget.boundingBox.top + widget.boundingBox.height - 10 }) ]), NutrientViewer.Immutable.List([ new NutrientViewer.Geometry.DrawingPoint({ x: widget.boundingBox.left + widget.boundingBox.width - 10, y: widget.boundingBox.top + 5 }), new NutrientViewer.Geometry.DrawingPoint({ x: widget.boundingBox.left + 5, y: widget.boundingBox.top + widget.boundingBox.height - 10 }) ]) ]), boundingBox: widget.boundingBox, isSignature: true });
await instance.create(annotation); console.log("Signature annotation created successfully");} catch (error) { console.error("Failed to create signature:", error.message);}Checking if a signature form field is signed
Signature form fields don’t have values, so finding the annotation, if any, that acts as the signature for a signature form field can be tricky.
Nutrient Web SDK considers a signature form field signed when an annotation overlaps it. To check if a given form field or an annotation is overlapped by any annotations, use the instance.getOverlappingAnnotations API.
There’s no direct connection between a signature and a form field beyond their positions. This method returns any annotation that overlaps the argument passed to the API.
try { // The name of the field you want. const formFieldName = "signature";
// First get all `FormFields` in the `Document`. const formFields = await instance.getFormFields();
// Get a signature form with the specific name you want. const field = formFields.find( (formField) => formField.name === formFieldName && formField instanceof NutrientViewer.FormFields.SignatureFormField );
if (!field) { throw new Error(`Signature field "${formFieldName}" not found`); }
// Check if the signature form field has been signed. const overlappingAnnotations = await instance.getOverlappingAnnotations(field);
// It will result in a list of annotations that overlaps the given signature form field. // If no annotation overlaps the form field, the list will be empty. const isSigned = overlappingAnnotations.size > 0; console.log(`Signature field is ${isSigned ? "signed" : "not signed"}`);} catch (error) { console.error("Failed to check signature status:", error.message);}You can use this API to check whether one or multiple annotations overlap any type of form field or annotation:
const annotations = await instance.getAnnotations(0);
const inkAnnotation = annotations.find( (annotation) => annotation instanceof NutrientViewer.Annotations.InkAnnotation);
await instance.getOverlappingAnnotations(inkAnnotation);