MCPcopy Create free account
hub / github.com/LibPDF-js/core / createCheckbox

Method createCheckbox

src/api/pdf-form.ts:640–687  ·  view source on GitHub ↗

* Create a new checkbox field. * * @param name - Field name (must be unique) * @param options - Checkbox options * @returns The created checkbox field * @throws {Error} If a field with the same name already exists * * @example * ```typescript * const agreeCheckbox = form.c

(name: string, options: CheckboxOptions = {})

Source from the content-addressed store, hash-verified

638 * ```
639 */
640 createCheckbox(name: string, options: CheckboxOptions = {}): CheckboxField {
641 this.validateUniqueName(name);
642
643 const onValue = options.onValue ?? "Yes";
644 const isChecked = options.defaultChecked ?? false;
645
646 // Button field type without Radio or Pushbutton flags = checkbox
647 // Create field dictionary with /Kids array (separate widget model)
648 const fieldDict = PdfDict.of({
649 FT: PdfName.of("Btn"),
650 T: PdfString.fromString(name),
651 Kids: new PdfArray([]),
652 V: PdfName.of(isChecked ? onValue : "Off"),
653 });
654
655 if (isChecked) {
656 fieldDict.set("DV", PdfName.of(onValue));
657 }
658
659 // Store symbol preference as metadata on field
660 if (options.symbol) {
661 // Store in custom key for appearance generation
662 fieldDict.set("_Symbol", PdfName.of(options.symbol));
663 }
664
665 // Store styling metadata
666 this.storeFieldStyling(fieldDict, options);
667
668 // Register and add to form
669 const fieldRef = this._ctx.registry.register(fieldDict);
670 this._acroForm.addField(fieldRef);
671
672 // Create the CheckboxField instance
673 // oxlint-disable-next-line typescript/no-unsafe-type-assertion
674 const field = createFormField(
675 fieldDict,
676 fieldRef,
677 this._ctx.registry,
678 this._acroForm,
679 name,
680 ) as CheckboxField;
681
682 // Add to cache
683 this.allFields.push(field);
684 this.fieldsByName.set(name, field);
685
686 return field;
687 }
688
689 /**
690 * Create a new radio button group.

Callers 2

pdf-form.test.tsFile · 0.80
mainFunction · 0.80

Calls 9

validateUniqueNameMethod · 0.95
storeFieldStylingMethod · 0.95
createFormFieldFunction · 0.90
addFieldMethod · 0.80
pushMethod · 0.80
ofMethod · 0.45
fromStringMethod · 0.45
setMethod · 0.45
registerMethod · 0.45

Tested by

no test coverage detected