{"record":{"id":"59091bbed8089949","repo":"mozilla/pdf.js","slug":"doc-numtemplates-is-read-only","errorCode":null,"errorMessage":"doc.numTemplates is read-only","messagePattern":"doc\\.numTemplates is read-only","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":542,"sourceCode":"\n  set numFields(_) {\n    throw new Error(\"doc.numFields is read-only\");\n  }\n\n  get numPages() {\n    return this._numPages;\n  }\n\n  set numPages(_) {\n    throw new Error(\"doc.numPages is read-only\");\n  }\n\n  get numTemplates() {\n    return 0;\n  }\n\n  set numTemplates(_) {\n    throw new Error(\"doc.numTemplates is read-only\");\n  }\n\n  get outerAppWindowRect() {\n    return [0, 0, 0, 0];\n  }\n\n  set outerAppWindowRect(_) {\n    throw new Error(\"doc.outerAppWindowRect is read-only\");\n  }\n\n  get outerDocWindowRect() {\n    return [0, 0, 0, 0];\n  }\n\n  set outerDocWindowRect(_) {\n    throw new Error(\"doc.outerDocWindowRect is read-only\");\n  }\n","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L524-L560","documentation":"`Doc.numTemplates` returns the number of page templates in the document; PDF.js always returns 0 because the page-template feature is not implemented. It is read-only per the Acrobat spec — templates are managed via `createTemplate`/`removeTemplate`, not by setting a count. The throwing setter keeps PDF.js behavior aligned with Acrobat's contract.","triggerScenarios":"A script writes `doc.numTemplates = n;` or increments it. Because templates are unsupported, the value would be meaningless to set even if the setter accepted it; instead it throws.","commonSituations":"Acrobat SDK sample code that demonstrates template creation; legacy dynamic-form scripts; scripts unaware that PDF.js does not implement page templates.","solutions":["Remove the assignment; `numTemplates` is observable only (`var n = doc.numTemplates;`, always 0 in PDF.js).","If template behavior is required, implement it at the host/integration layer rather than via this property.","Guard execution of legacy scripts with try/catch to tolerate the unsupported write."],"exampleFix":"// before\ndoc.numTemplates = 3;\n\n// after\nvar n = doc.numTemplates; // 0 in PDF.js; templates unsupported","handlingStrategy":"try-catch","validationCode":"const READ_ONLY_DOC_PROPS = new Set(['numTemplates','templates' /* ... */]);\nfunction assertsNoReadOnlyWrite(scriptSrc) {\n  for (const p of READ_ONLY_DOC_PROPS) {\n    if (new RegExp(`\\\\bdoc\\\\.${p}\\\\s*=[^=]`).test(scriptSrc)) {\n      throw new Error(`Script writes read-only property doc.${p}`);\n    }\n  }\n}\nassertsNoReadOnlyWrite(myScript);","typeGuard":"function isReadOnlyDocProp(doc, prop) {\n  const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(doc), prop)\n    || Object.getOwnPropertyDescriptor(doc, prop);\n  return !!desc && !!desc.get && !desc.set;\n}\nisReadOnlyDocProp(doc, 'numTemplates'); // true","tryCatchPattern":"try {\n  doc.numTemplates = 3;\n} catch (e) {\n  if (/numTemplates is read-only/.test(e.message)) console.warn(e.message);\n  else throw e;\n}","preventionTips":["PDF.js does not implement page templates; never write template-count properties.","Lint scripts for read-only template properties before execution.","Wrap legacy dynamic-form scripts in try/catch."],"tags":["scripting","acrobat-js-api","doc-object","read-only","templates","unsupported"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}