Package Exports
- pdf-annotator-react
- pdf-annotator-react/dist/index.esm.js
- pdf-annotator-react/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pdf-annotator-react) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
PDF Annotator React
A modern React component library for PDF annotation. This library allows you to easily add annotation capabilities to PDFs in your React applications.
Features
- View PDFs with page navigation
- Create and manage annotations:
- Highlights
- Underlines
- Strikeouts
- Rectangles
- Freehand drawing
- Text notes
- Comments
- Pins with tags
- Customize annotation colors and thickness
- Category-based annotation filtering
- View-only and viewer-only modes
- Configurable toolbar position (top/bottom)
- Automatic fit-to-width scaling
- Get callbacks for annotation events (create, update, delete, select)
- Modern React hooks-based API
Installation
npm install pdf-annotator-react
# or
yarn add pdf-annotator-reactUsage
Basic Example
import React, { useState } from 'react';
import { PdfAnnotator, AnnotationMode } from 'pdf-annotator-react';
const MyPdfAnnotator = () => {
const [annotations, setAnnotations] = useState([]);
const handleAnnotationCreate = (newAnnotation) => {
setAnnotations(prev => [...prev, newAnnotation]);
};
const handleAnnotationUpdate = (updatedAnnotation) => {
setAnnotations(prev =>
prev.map(a => a.id === updatedAnnotation.id ? updatedAnnotation : a)
);
};
const handleAnnotationDelete = (annotationId) => {
setAnnotations(prev => prev.filter(a => a.id !== annotationId));
};
return (
<div style={{ height: '100vh', width: '100%' }}>
<PdfAnnotator
url="https://example.com/sample.pdf"
annotations={annotations}
onAnnotationCreate={handleAnnotationCreate}
onAnnotationUpdate={handleAnnotationUpdate}
onAnnotationDelete={handleAnnotationDelete}
annotationMode={AnnotationMode.HIGHLIGHT}
/>
</div>
);
};
export default MyPdfAnnotator;Advanced Example with Categories and Custom Settings
import React, { useState } from 'react';
import { PdfAnnotator, AnnotationMode } from 'pdf-annotator-react';
const MyPdfAnnotator = () => {
const [annotations, setAnnotations] = useState([]);
const [mode, setMode] = useState(AnnotationMode.NONE);
// Custom categories with tags
const customCategories = [
{
competencia: {
id: 1,
displayName: "Grammar",
color: "#FF5733"
},
tags: [
{ id: 1, name: "Verb Tense" },
{ id: 2, name: "Agreement" }
]
},
{
competencia: {
id: 2,
displayName: "Style",
color: "#33FF57"
},
tags: [
{ id: 3, name: "Clarity" },
{ id: 4, name: "Conciseness" }
]
}
];
// Annotation event handlers
const handleAnnotationsChange = (newAnnotations) => {
setAnnotations(newAnnotations);
};
const handleAnnotationSelect = (selectedAnnotation) => {
console.log('Selected annotation:', selectedAnnotation);
};
return (
<div style={{ height: '100vh', width: '100%' }}>
<PdfAnnotator
url="https://example.com/sample.pdf"
annotations={annotations}
onAnnotationsChange={handleAnnotationsChange}
onAnnotationSelect={handleAnnotationSelect}
annotationMode={mode}
onAnnotationModeChange={setMode}
// Custom settings
customCategories={customCategories}
defaultThickness={2}
fitToWidth={true}
toolbarPosition="top"
viewOnly={false}
viewerOnly={false}
// Custom colors
highlightColor="rgba(255, 230, 0, 0.4)"
underlineColor="rgba(0, 100, 255, 0.7)"
rectangleColor="rgba(255, 0, 0, 0.3)"
drawingColor="#22cc22"
pinColor="rgba(249, 115, 22, 0.7)"
/>
</div>
);
};
export default MyPdfAnnotator;Props
The PdfAnnotator component accepts the following props:
| Prop | Type | Description |
|---|---|---|
url |
string | URL of the PDF to display |
annotations |
Array | Array of annotation objects |
scale |
number | Scale factor for rendering (default: 1.0) |
pageNumber |
number | Initial page number to display (default: 1) |
onDocumentLoadSuccess |
function | Callback when PDF loads successfully |
onPageChange |
function | Callback when page changes |
annotationMode |
AnnotationMode | Current annotation mode |
onAnnotationModeChange |
function | Callback when annotation mode changes |
onAnnotationCreate |
function | Callback when an annotation is created |
onAnnotationUpdate |
function | Callback when an annotation is updated |
onAnnotationDelete |
function | Callback when an annotation is deleted |
onAnnotationSelect |
function | Callback when an annotation is selected |
onAnnotationsChange |
function | Callback when annotations array changes |
customCategories |
Array | Array of category objects with tags |
currentCategory |
object | Currently selected category |
onCategoryChange |
function | Callback when category changes |
defaultThickness |
number | Default thickness for annotations (default: 2) |
fitToWidth |
boolean | Whether to fit PDF to container width (default: true) |
viewOnly |
boolean | Whether annotations can be edited (default: false) |
viewerOnly |
boolean | Whether to show annotations and toolbar (default: false) |
toolbarPosition |
string | Position of the toolbar ('top' or 'bottom') |
highlightColor |
string | Custom color for highlight annotations |
underlineColor |
string | Custom color for underline annotations |
strikeoutColor |
string | Custom color for strikeout annotations |
rectangleColor |
string | Custom color for rectangle annotations |
drawingColor |
string | Custom color for drawing annotations |
textColor |
string | Custom color for text annotations |
commentColor |
string | Custom color for comment annotations |
pinColor |
string | Custom color for pin annotations |
pdfWorkerSrc |
string | Custom PDF.js worker URL |
Changelog
0.2.0
- Added support for annotation categories with tags
- Added pin annotations with tag support
- Added view-only and viewer-only modes
- Added configurable toolbar position (top/bottom)
- Added automatic fit-to-width scaling
- Added annotation thickness control
- Added comprehensive type definitions
- Improved annotation positioning and scaling
- Fixed various bugs with annotation rendering and interaction
License
MIT