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

Method createDropdown

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

* Create a new dropdown (combo box) field. * * @param name - Field name (must be unique) * @param options - Dropdown options (options array is required) * @returns The created dropdown field * @throws {Error} If a field with the same name already exists * @throws {Error} If options

(name: string, options: DropdownOptions)

Source from the content-addressed store, hash-verified

791 * ```
792 */
793 createDropdown(name: string, options: DropdownOptions): DropdownField {
794 this.validateUniqueName(name);
795
796 if (!options.options || options.options.length === 0) {
797 throw new Error("Dropdown must have at least one option");
798 }
799
800 // Choice field type with Combo flag
801 let flags = FieldFlags.COMBO;
802
803 if (options.editable) {
804 flags |= FieldFlags.EDIT;
805 }
806
807 // Build default appearance string
808 const da = this.buildDefaultAppearance(options.font, options.fontSize, options.color);
809
810 // Create field dictionary with /Kids array (separate widget model)
811 const fieldDict = PdfDict.of({
812 FT: PdfName.of("Ch"),
813 T: PdfString.fromString(name),
814 Ff: PdfNumber.of(flags),
815 Kids: new PdfArray([]),
816 Opt: PdfArray.of(...options.options.map(o => PdfString.fromString(o))),
817 });
818
819 if (da) {
820 fieldDict.set("DA", PdfString.fromString(da));
821 }
822
823 if (options.defaultValue !== undefined && options.options.includes(options.defaultValue)) {
824 fieldDict.set("V", PdfString.fromString(options.defaultValue));
825 fieldDict.set("DV", PdfString.fromString(options.defaultValue));
826 }
827
828 // Register font in form resources if embedded font provided
829 if (options.font) {
830 this.registerFontInFormResources(options.font);
831 }
832
833 // Store styling metadata
834 this.storeFieldStyling(fieldDict, options);
835
836 // Register and add to form
837 const fieldRef = this._ctx.registry.register(fieldDict);
838 this._acroForm.addField(fieldRef);
839
840 // Create the DropdownField instance
841 // oxlint-disable-next-line typescript/no-unsafe-type-assertion
842 const field = createFormField(
843 fieldDict,
844 fieldRef,
845 this._ctx.registry,
846 this._acroForm,
847 name,
848 ) as DropdownField;
849
850 // Apply styling options

Callers 2

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

Calls 13

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

Tested by

no test coverage detected