{"record":{"id":"205ddcd5ace84659","repo":"mozilla/pdf.js","slug":"invalid-ndec-value-in-afpercent-format","errorCode":null,"errorMessage":"Invalid nDec value in AFPercent_Format","messagePattern":"Invalid nDec value in AFPercent_Format","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/scripting_api/aform.js","lineNumber":214,"sourceCode":"        this._app.alert(err);\n      }\n      event.rc = false;\n    }\n\n    if (event.willCommit && sepStyle > 1) {\n      event.value = parseFloat(value.replace(\",\", \".\"));\n    }\n  }\n\n  AFPercent_Format(nDec, sepStyle, percentPrepend = false) {\n    if (typeof nDec !== \"number\") {\n      return;\n    }\n    if (typeof sepStyle !== \"number\") {\n      return;\n    }\n    if (nDec < 0) {\n      throw new Error(\"Invalid nDec value in AFPercent_Format\");\n    }\n\n    const event = globalThis.event;\n    if (nDec > 512) {\n      event.value = \"%\";\n      return;\n    }\n\n    nDec = Math.floor(nDec);\n\n    // sepStyle is an integer in [0;4]\n    sepStyle = MathClamp(Math.floor(sepStyle), 0, 4);\n\n    let value = this.AFMakeNumber(event.value);\n    if (value === null) {\n      event.value = \"%\";\n      return;\n    }","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/aform.js#L196-L232","documentation":"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.","triggerScenarios":"A PDF form script calls AFPercent_Format(nDec, sepStyle, percentPrepend) with a number nDec that is < 0.","commonSituations":"A malformed PDF form passes a bad nDec; a calculated nDec underflows to negative; an authoring tool emitted an incorrect format specifier.","solutions":["Report/repair the PDF form script; this is content-driven, not API-driven.","Update PDF.js; sandbox error handling may be made more lenient over time.","Ensure the embedding app degrades gracefully when scripting throws (form value simply does not format).","If you author the PDF, pass a non-negative nDec."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Scripting errors surface from the sandbox; isolate them so the viewer keeps working.\ntry {\n  await pdfDocument.annotationLayer?.render(...);\n} catch (e) {\n  if (/AFPercent_Format/.test(e.message)) { /* content-driven; ignore */ }\n}","preventionTips":["Treat AForm errors as content-driven and non-fatal.","Keep the viewer usable when a form script throws.","Update PDF.js for more lenient scripting where possible.","Repair authoring-side PDFs that pass invalid format arguments."],"tags":["aform","scripting","format","pdf-content"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}