mozilla/pdf.js · warning · Error

Invalid nDec value in AFPercent_Format

Error message

Invalid nDec value in AFPercent_Format

What it means

Thrown inside the Acrobat-form (AForm) scripting layer by AFPercent_Format when nDec (the decimal-place count) is negative. The function early-returns for non-number inputs but hard-rejects negative numbers because a negative decimal count is nonsensical for percentage formatting. This runs inside the sandboxed PDF-JS environment driven by the PDF's own form scripts.

Source

Thrown at src/scripting_api/aform.js:214

        this._app.alert(err);
      }
      event.rc = false;
    }

    if (event.willCommit && sepStyle > 1) {
      event.value = parseFloat(value.replace(",", "."));
    }
  }

  AFPercent_Format(nDec, sepStyle, percentPrepend = false) {
    if (typeof nDec !== "number") {
      return;
    }
    if (typeof sepStyle !== "number") {
      return;
    }
    if (nDec < 0) {
      throw new Error("Invalid nDec value in AFPercent_Format");
    }

    const event = globalThis.event;
    if (nDec > 512) {
      event.value = "%";
      return;
    }

    nDec = Math.floor(nDec);

    // sepStyle is an integer in [0;4]
    sepStyle = MathClamp(Math.floor(sepStyle), 0, 4);

    let value = this.AFMakeNumber(event.value);
    if (value === null) {
      event.value = "%";
      return;
    }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Report/repair the PDF form script; this is content-driven, not API-driven.
  2. Update PDF.js; sandbox error handling may be made more lenient over time.
  3. Ensure the embedding app degrades gracefully when scripting throws (form value simply does not format).
  4. If you author the PDF, pass a non-negative nDec.
Defensive patterns

Strategy: try-catch

Try / catch

// Scripting errors surface from the sandbox; isolate them so the viewer keeps working.
try {
  await pdfDocument.annotationLayer?.render(...);
} catch (e) {
  if (/AFPercent_Format/.test(e.message)) { /* content-driven; ignore */ }
}

Prevention

When it happens

Trigger: A PDF form script calls AFPercent_Format(nDec, sepStyle, percentPrepend) with a number nDec that is < 0.

Common situations: A malformed PDF form passes a bad nDec; a calculated nDec underflows to negative; an authoring tool emitted an incorrect format specifier.

Related errors


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