mozilla/pdf.js · error · Error

Not a choice widget

Error message

Not a choice widget

What it means

Thrown by the getter of `Field.numItems` (src/scripting_api/field.js:170). `numItems` is only defined for choice widgets (combo boxes and list boxes). PDF.js marks a field as a choice via `_isChoice = Array.isArray(data.items)` at construction; accessing `numItems` on a text/button/signature field throws.

Source

Thrown at src/scripting_api/field.js:170

  set bgColor(color) {
    this.fillColor = color;
  }

  get charLimit() {
    return this._charLimit;
  }

  set charLimit(limit) {
    if (typeof limit !== "number") {
      throw new Error("Invalid argument value");
    }
    this._charLimit = Math.max(0, Math.floor(limit));
  }

  get numItems() {
    if (!this._isChoice) {
      throw new Error("Not a choice widget");
    }
    return this._items.length;
  }

  set numItems(_) {
    throw new Error("field.numItems is read-only");
  }

  get strokeColor() {
    return this._strokeColor;
  }

  set strokeColor(color) {
    if (Color._isValidColor(color)) {
      this._strokeColor = color;
    }
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Gate access on the field type: only read `numItems` when `field.type === 'ListBox' || field.type === 'ComboBox'`.
  2. Check `'numItems' in field` is not sufficient (the accessor always exists); instead branch on `field.type`.
  3. Wrap the access in try/catch if you must iterate heterogeneous fields generically.

Example fix

// before
for (let k = 0; k < f.numItems; k++) { ... }
// after
if (f.type === 'ListBox' || f.type === 'ComboBox') {
  for (let k = 0; k < f.numItems; k++) { ... }
}
Defensive patterns

Strategy: validation

Validate before calling

function isChoiceField(f) {
  return f && (f.type === 'ListBox' || f.type === 'ComboBox');
}
function numItemsSafe(f) {
  return isChoiceField(f) ? f.numItems : 0;
}

Type guard

function isChoiceField(f) {
  return !!f && (f.type === 'ListBox' || f.type === 'ComboBox');
}

Prevention

When it happens

Trigger: Calling `var n = someTextField.numItems;` on a non-choice field, or generic enumeration code that reads `numItems` on every field in `doc.getArray()` / `doc.numFields` iteration regardless of `field.type`.

Common situations: Bulk form-introspection code that assumes all fields support item lists; scripts that were written for a list-box-only form and later run over a mixed form.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/05bb5bd27def4e9a. Report an issue: GitHub.