mozilla/pdf.js · error · Error

field.page is read-only

Error message

field.page is read-only

What it means

Thrown by the setter of `Field.page` (src/scripting_api/field.js:202). The page on which a field lives is an intrinsic property set from the PDF structure during construction; it cannot be moved by assignment. The getter returns `this._page`.

Source

Thrown at src/scripting_api/field.js:202

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

  get borderColor() {
    return this.strokeColor;
  }

  set borderColor(color) {
    this.strokeColor = color;
  }

  get page() {
    return this._page;
  }

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

  get rotation() {
    return this._rotation;
  }

  set rotation(angle) {
    angle = Math.floor(angle);
    if (angle % 90 !== 0) {
      throw new Error("Invalid rotation: must be a multiple of 90");
    }
    angle %= 360;
    if (angle < 0) {
      angle += 360;
    }
    this._rotation = angle;
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Do not assign `page`; to navigate, use `doc.pageNum = f.page` (read the property instead).
  2. Maintain a blocklist of read-only field properties (`page`, `numItems`, etc.) and skip them during property-copy loops.
  3. Wrap in try/catch if the assignment originates in code you cannot change.

Example fix

// before
f.page = targetPage;
// after
doc.pageNum = f.page; // navigate instead of relocating the field
Defensive patterns

Strategy: try-catch

Validate before calling

const READ_ONLY_FIELD_PROPS = new Set(['page','numItems']);
function safeAssignField(field, key, value) {
  if (READ_ONLY_FIELD_PROPS.has(key)) return false;
  field[key] = value;
  return true;
}

Try / catch

try {
  f.page = target;
} catch (e) {
  // page is read-only; navigate the doc instead
  doc.pageNum = f.page;
}

Prevention

When it happens

Trigger: Executing `f.page = 2;` attempting to relocate a field to another page, or generic copy/restore logic that writes back every read property including `page`.

Common situations: Cloning field configuration across pages; scripts mis-translating a 'go to page' intent (`f.page` read is fine, write is not); round-tripping a field-snapshot object.

Related errors


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