An annotation extension based on the PDF.js viewer
Supporting the editing of existing PDF file annotations, posting comments, replying, submitting annotation data, and loading for further editing.
English | 简体中文
PDF.js provides a Viewer for online PDF preview and includes some basic annotation functionalities (FREETEXT, HIGHLIGHT, STAMP, INK).
In actual use cases, there is often a need for various annotation tools, leading to the idea of extending the viewer to add more annotation capabilities.
This project is based on konva
, react
, antd
, and web-highlighter
. It introduces additional annotation types by extending the existing pdf.js
viewer without modifying its original code. Supports editing existing annotations, submitting annotation data, and loading for further editing, as shown below:
For PDF Viewer users, this is a highly useful feature. If your requirement is simple annotation, the current features in the project should suffice. For more specific needs or requirements, you can further develop based on this extension.
- Rectangle
- Circle
- Free Hand (grouped if drawn within a short time)
- Free Highlight (with auto-correction)
- FreeText
- Signature
- Stamp (upload custom images)
- Text Highlight
- Text Strikeout
- Text Underline
- Annotation Selection (use double-click to delete the selected object)
💡 Only editing of existing annotations in the PDF file is supported. Writing annotations back to the PDF file is no longer supported. To write annotations to the file, please use the annotation data and process the writing on the server.
- Square
- Circle
- Ink
- FreeText
- Line
- Polygon
- PolyLine
- Text
- Highlight
- Underline
- StrikeOut
$ npm install or yarn
$ npm run dev or yarn dev
A DEMO example is provided in the repository (located in the examples folder). Navigate to ./examples/pdfjs-4.3.136-dist:
$ miniserve or use another static server
Open the URL: http://localhost:8080/web/viewer.html to see the result.
ae_username= Name of the annotator, displayed as the annotator's name when adding annotations
ae_get_url= URL for annotation data, used to load previously saved annotation data. Example: ./examples/pdfjs-4.3.136-dist/pdfjs-annotation-extension-testdata.json
ae_post_url= URL for submitting annotation data
src/const/default_options.ts
To load PDF file annotations, modify:
LOAD_PDF_ANNOTATION: true, // Whether to load existing PDF annotations
Note:If you need to edit existing PDF annotations, you must set annotationMode in PDF.js to 0, so that PDF.js will not render the annotations
pdfjs-dist/web/viewer.mjs
annotationMode: {
value: 0, // Change to 0
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}
The configuration can be found in the /configuration/environment.js file. By default, it points to examples/pdfjs-4.3.136-dist/web/pdfjs-annotation-extension. You can modify it to match your local pdfjs-dist directory to facilitate development:
output: path.resolve(__dirname, '../examples/pdfjs-4.3.136-dist/web/pdfjs-annotation-extension'),
$ npm run build or yarn build
Alternatively, you can directly download the release version.
Modify the pdfjs-dist/web/viewer.html file by adding a single line to include the generated extension file:
<script src="../build/pdf.mjs" type="module"></script>
<link rel="stylesheet" href="viewer.css">
<script src="viewer.mjs" type="module"></script>
<!-- Insert the generated annotation extension file -->
<script src="./pdfjs-annotation-extension/pdfjs-annotation-extension.js" type="module"></script>
<!-- End -->
</head>
By leveraging the pdfjs EventBus, we capture page events and dynamically insert a Konva drawing layer. Shapes are drawn on the Konva layer. Although there are more annotation types, they are essentially mapped to the ones supported by pdfjs with some additional custom transformations.
For details about pdfjs annotation types, please refer to the documentation here 👇 https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-annotations
Currently, this extension has been tested only with pdfjs-4.3.136-dist. Note that it does not support drawing on rotated pages.