Repository: Hopding/pdf-lib
Pull request #730 (2021-01-02 18:43:09)

Support hidden form fields


Patch:
diff --git a/src/api/form/PDFButton.ts b/src/api/form/PDFButton.ts
index acf623c..1bb494f 100644
--- a/src/api/form/PDFButton.ts
+++ b/src/api/form/PDFButton.ts
@@ -142,6 +142,7 @@ export default class PDFButton extends PDFField {
       borderWidth: options?.borderWidth ?? 0,
       rotate: options?.rotate ?? degrees(0),
       caption: text,
+      hidden: options?.hidden,
     });
     const widgetRef = this.doc.context.register(widget.dict);
 
diff --git a/src/api/form/PDFCheckBox.ts b/src/api/form/PDFCheckBox.ts
index 7c23255..4da69f3 100644
--- a/src/api/form/PDFCheckBox.ts
+++ b/src/api/form/PDFCheckBox.ts
@@ -169,6 +169,7 @@ export default class PDFCheckBox extends PDFField {
       borderColor: options.borderColor,
       borderWidth: options.borderWidth ?? 0,
       rotate: options.rotate ?? degrees(0),
+      hidden: options.hidden,
     });
     const widgetRef = this.doc.context.register(widget.dict);
 
diff --git a/src/api/form/PDFDropdown.ts b/src/api/form/PDFDropdown.ts
index 717e1b6..5f1964e 100644
--- a/src/api/form/PDFDropdown.ts
+++ b/src/api/form/PDFDropdown.ts
@@ -522,6 +522,7 @@ export default class PDFDropdown extends PDFField {
       borderColor: options.borderColor,
       borderWidth: options.borderWidth ?? 0,
       rotate: options.rotate ?? degrees(0),
+      hidden: options.hidden,
     });
     const widgetRef = this.doc.context.register(widget.dict);
 
diff --git a/src/api/form/PDFField.ts b/src/api/form/PDFField.ts
index aa867ee..2d7b1a5 100644
--- a/src/api/form/PDFField.ts
+++ b/src/api/form/PDFField.ts
@@ -43,6 +43,7 @@ export interface FieldAppearanceOptions {
   borderWidth?: number;
   rotate?: Rotation;
   font?: PDFFont;
+  hidden?: boolean;
 }
 
 export const assertFieldAppearanceOptions = (
@@ -291,6 +292,7 @@ export default class PDFField {
     borderWidth: number;
     rotate: Rotation;
     caption?: string;
+    hidden?: boolean;
   }): PDFWidgetAnnotation {
     const textColor = options.textColor;
     const backgroundColor = options.backgroundColor;
@@ -302,6 +304,7 @@ export default class PDFField {
     const y = options.y;
     const width = options.width + borderWidth;
     const height = options.height + borderWidth;
+    const hidden = Boolean(options.hidden);
 
     assertMultiple(degreesAngle, 'degreesAngle', 90);
 
@@ -328,7 +331,7 @@ export default class PDFField {
     if (borderWidth !== undefined) bs.setWidth(borderWidth);
 
     widget.setFlagTo(AnnotationFlags.Print, true);
-    widget.setFlagTo(AnnotationFlags.Hidden, false);
+    widget.setFlagTo(AnnotationFlags.Hidden, hidden);
     widget.setFlagTo(AnnotationFlags.Invisible, false);
 
     // Set acrofield properties
diff --git a/src/api/form/PDFOptionList.ts b/src/api/form/PDFOptionList.ts
index 5eac5e0..4adf861 100644
--- a/src/api/form/PDFOptionList.ts
+++ b/src/api/form/PDFOptionList.ts
@@ -425,6 +425,7 @@ export default class PDFOptionList extends PDFField {
       borderColor: options.borderColor,
       borderWidth: options.borderWidth ?? 0,
       rotate: options.rotate ?? degrees(0),
+      hidden: options.hidden,
     });
     const widgetRef = this.doc.context.register(widget.dict);
 
diff --git a/src/api/form/PDFRadioGroup.ts b/src/api/form/PDFRadioGroup.ts
index acb17dc..c4f897f 100644
--- a/src/api/form/PDFRadioGroup.ts
+++ b/src/api/form/PDFRadioGroup.ts
@@ -369,6 +369,7 @@ export default class PDFRadioGroup extends PDFField {
       borderColor: options?.borderColor ?? rgb(0, 0, 0),
       borderWidth: options?.borderWidth ?? 1,
       rotate: options?.rotate ?? degrees(0),
+      hidden: options?.hidden,
     });
     const widgetRef = this.doc.context.register(widget.dict);
 
diff --git a/src/api/form/PDFTextField.ts b/src/api/form/PDFTextField.ts
index 6e44241..1a9901b 100644
--- a/src/api/form/PDFTextField.ts
+++ b/src/api/form/PDFTextField.ts
@@ -669,6 +669,7 @@ export default class PDFTextField extends PDFField {
       borderColor: options.borderColor,
       borderWidth: options.borderWidth ?? 0,
       rotate: options.rotate ?? degrees(0),
+      hidden: options.hidden,
     });
     const widgetRef = this.doc.context.register(widget.dict);
 


Test patch:
diff --git a/tests/api/form/PDFTextField.spec.ts b/tests/api/form/PDFTextField.spec.ts
index 60fcb31..1b91c69 100644
--- a/tests/api/form/PDFTextField.spec.ts
+++ b/tests/api/form/PDFTextField.spec.ts
@@ -111,4 +111,18 @@ describe(`PDFTextField`, () => {
     expect(widgets().length).toBe(1);
     expect(widgets()[0].hasFlag(AnnotationFlags.Print)).toBe(true);
   });
+
+  it(`sets the 'hidden' flag when passed options.hidden`, async () => {
+    const pdfDoc = await PDFDocument.create();
+    const page = pdfDoc.addPage();
+    const form = pdfDoc.getForm();
+
+    const textField = form.createTextField('a.hidden.text.field');
+    const widgets = () => textField.acroField.getWidgets();
+
+    textField.addToPage(page, { hidden: true });
+
+    expect(widgets().length).toBe(1);
+    expect(widgets()[0].hasFlag(AnnotationFlags.Hidden)).toBe(true);
+  });
 });