A modern Next.js application for viewing and annotating PDF files with rectangle-based annotations.
- PDF Viewing: High-quality PDF rendering with zoom and navigation controls
- Rectangle Annotations: Draw, edit, and delete rectangular annotations
- Responsive Design: Works seamlessly on desktop and mobile devices
- State Management: Persistent annotations using localStorage
- Keyboard Shortcuts: Efficient workflow with keyboard navigation
- Export/Import: Save and load annotations as JSON files
- Framework: Next.js 14+ with App Router
- PDF Rendering: react-pdf library with PDF.js
- Styling: Tailwind CSS
- State Management: Custom React hooks
- TypeScript: Full type safety throughout
- Node.js 18+
- npm or yarn
- Clone or download the project files
- Install dependencies:
npm install
# or
yarn install
- Run the development server:
npm run dev
# or
yarn dev
- Open http://localhost:3000 in your browser
- Click the "Open PDF" button in the toolbar
- Select a PDF file from your computer
- The PDF will load and display in the viewer
- Select the Rectangle tool from the toolbar (or press
R
) - Click and drag on the PDF to create a rectangle annotation
- Release the mouse to complete the annotation
- Select the Select tool from the toolbar (or press
S
) - Click on an annotation to select it (blue border and handles appear)
- Move: Drag the annotation body to move it
- Resize: Drag any of the 8 resize handles (corners and edges) to change size
- Delete:
- Hover over annotation and click the red close icon (top-right corner)
- Press
Delete
key when annotation is selected - Use the Delete tool to remove
- Zoom: Use the zoom buttons or scroll wheel
- Pages: Navigate with arrow buttons or type page numbers
- Keyboard: Use arrow keys for page navigation
R
- Rectangle annotation toolS
- Select toolD
- Delete toolESC
- Cancel current action←/→
- Previous/Next pageDelete
- Remove selected annotation
- Auto-save: Annotations are automatically saved to localStorage
- Export: Download annotations as JSON file
- Import: Load previously exported annotations
src/
├── app/
│ ├── page.tsx # Main application page
│ ├── layout.tsx # Root layout component
│ └── globals.css # Global styles and Tailwind
├── components/
│ ├── PDFAnnotationViewer.tsx # Single comprehensive PDF annotation component
│ ├── ExampleUsage.tsx # Usage examples
│ ├── PDFViewer.tsx # (Legacy) Main PDF viewer component
│ ├── AnnotationLayer.tsx # (Legacy) Annotation overlay component
│ ├── AnnotationToolbar.tsx # (Legacy) Toolbar with controls
│ └── RectangleAnnotation.tsx # (Legacy) Individual annotation component
├── hooks/
│ ├── useAnnotations.ts # (Legacy) Annotation state management
│ └── usePDFViewer.ts # (Legacy) PDF viewer state management
├── types/
│ └── annotations.ts # TypeScript type definitions
└── utils/
├── annotationUtils.ts # Annotation helper functions
└── pdfUtils.ts # PDF-related utilities
Single comprehensive component that includes all PDF viewing and annotation functionality in one self-contained React component.
Props:
file?: File | string
- PDF file or URL to loadclassName?: string
- Additional CSS classesonAnnotationsChange?: (annotations: Annotation[]) => void
- Callback when annotations changeinitialAnnotations?: Annotation[]
- Pre-existing annotations to display
Usage:
import PDFAnnotationViewer from '@/components/PDFAnnotationViewer';
function MyApp() {
return (
<PDFAnnotationViewer
file="/path/to/document.pdf"
onAnnotationsChange={(annotations) => console.log(annotations)}
/>
);
}
Main component that orchestrates PDF display and annotation functionality.
Handles mouse/touch events for drawing and selecting annotations.
Provides tools for annotation creation, navigation, and file operations.
Renders individual annotations with selection and editing capabilities.
The app uses a dual coordinate system:
- Canvas Coordinates: Screen pixels relative to the PDF display
- PDF Coordinates: Points in the PDF's native coordinate system
Coordinates are automatically converted to maintain accuracy across zoom levels.
- Chrome/Edge 90+
- Firefox 88+
- Safari 14+
- Efficient rendering with React optimization
- Debounced annotation updates
- Virtual scrolling for large PDFs
- Responsive image loading
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and available under the MIT License.
- Ensure the file is a valid PDF
- Check browser console for errors
- Try a different PDF file
- Check localStorage availability
- Ensure browser allows local storage
- Try clearing browser cache
- Reduce zoom level for large PDFs
- Close other browser tabs
- Check available system memory
- Text annotations
- Collaborative editing
- Annotation comments
- Drawing tools (pen, highlighter)
- PDF form filling
- Cloud storage integration
- Real-time collaboration
- Annotation templates