Skip to content

[Feature]: Add rectangle (square) annotation support in PDF.js #20146

@BaluprasathSBP

Description

@BaluprasathSBP

Is the feature relevant to the Firefox PDF Viewer?

Yes

Feature description

Feature: Rectangle Annotation Support in PDF.js

Objective:
Enable creation, rendering, editing, and saving of rectangle (square) annotations in PDF documents using the Mozilla PDF.js source code.

  • Drawing rectangle annotations by user interaction.
  • Rendering them on the canvas.
  • Storing them in annotationStorage.
  • Saving to and loading from XFDF-compatible metadata or embedded PDF annotation stream (if supported).

Technical Specification:

  1. Add new RectangleAnnotation class (subtype: Square) extending Annotation or MarkupAnnotation in src/core/annotation.js.
  2. Data fields: x, y, width, height, color (RGBA), borderWidth, pageIndex, id, and optionally author, date, opacity, intent.
  3. UI workflow: Add "Draw Rectangle" button, implement drawing tool using mouse events, show overlay during drawing.
  4. Rendering: Render rectangles on top of PDFs via SVG/Canvas, support scaling and persistence.
  5. Storage: Store in annotationStorage, render rectangles from storage when reloading.
  6. Export/Import: Provide APIs for export/import, optionally support XFDF.

Why is it useful?
Rectangle annotations are standard in PDF workflows for highlighting, redacting, or marking document areas. Enabling this in PDF.js brings it closer to feature parity with other PDF viewers, improves user productivity, and unlocks new use cases for annotation-heavy environments.

Current Workarounds:
Currently, PDF.js does not support rectangle (square) annotation creation or rendering by end users; only reading pre-existing annotations is possible. Users must rely on external PDF tools to add rectangles, then reload the file to see them rendered.

Other PDF viewers

Adobe Acrobat, Foxit PDF Reader, and most commercial PDF viewers support drawing and editing rectangle (square) annotations. They provide toolbar tools and allow manipulation, export, and import through XFDF or PDF streams.

References:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions