Convert Figma logo to code with AI

agentcooper logoreact-pdf-highlighter

Set of React components for PDF annotation

1,030
406
1,030
99

Top Related Projects

2,543

Golang PDF library for creating and processing PDF files (pure go)

47,890

PDF Reader in JavaScript

Quick Overview

The react-pdf-highlighter project is a React component that allows users to highlight and annotate PDF documents within a web application. It provides a set of tools and APIs to integrate PDF viewing and annotation functionality into React-based projects.

Pros

  • Robust PDF Viewing and Annotation: The library offers a comprehensive set of features for viewing and annotating PDF documents, including highlighting, adding notes, and more.
  • Customizable UI: The library provides a flexible and customizable user interface, allowing developers to integrate the PDF viewer and annotation tools seamlessly into their application's design.
  • Cross-browser Compatibility: The library is designed to work across a wide range of modern web browsers, ensuring a consistent user experience.
  • Active Development and Community: The project has an active development team and a growing community of contributors, ensuring ongoing support and improvements.

Cons

  • Dependency on External Libraries: The library relies on several external dependencies, such as pdf-lib and react-pdf, which may increase the overall project's complexity and size.
  • Limited Offline Support: While the library can be used to view and annotate PDF documents, it may not provide robust offline support, which could be a limitation for some use cases.
  • Potential Performance Issues: Depending on the size and complexity of the PDF documents being viewed, the library may experience performance issues, especially on lower-powered devices.
  • Limited Documentation: The project's documentation, while generally helpful, could be more comprehensive, especially for advanced use cases or customization options.

Code Examples

Rendering a PDF Document

import React from 'react';
import { Viewer } from 'react-pdf-highlighter';

const MyPdfViewer = () => {
  return (
    <Viewer
      pdf="/path/to/your/pdf.pdf"
      enableAnnotationEditing={true}
      enableHighlightAnnotations={true}
      enableTextSelection={true}
    />
  );
};

export default MyPdfViewer;

This code snippet demonstrates how to render a PDF document using the Viewer component provided by the react-pdf-highlighter library. The component allows users to enable annotation editing, highlight annotations, and enable text selection.

Adding a Highlight Annotation

import React, { useRef } from 'react';
import { Viewer, Highlight, HighlightArea } from 'react-pdf-highlighter';

const MyPdfViewer = () => {
  const viewerRef = useRef(null);

  const handleHighlight = (highlight) => {
    viewerRef.current.addHighlight(highlight);
  };

  return (
    <Viewer
      ref={viewerRef}
      pdf="/path/to/your/pdf.pdf"
      enableAnnotationEditing={true}
      enableHighlightAnnotations={true}
    >
      <Highlight onHighlight={handleHighlight} />
      <HighlightArea />
    </Viewer>
  );
};

export default MyPdfViewer;

This code snippet demonstrates how to add a highlight annotation to a PDF document using the Highlight and HighlightArea components provided by the react-pdf-highlighter library. The handleHighlight function is called when a user highlights a section of the PDF, and the addHighlight method is used to add the highlight to the viewer.

Saving and Loading Annotations

import React, { useRef } from 'react';
import { Viewer, Highlight, HighlightArea } from 'react-pdf-highlighter';

const MyPdfViewer = () => {
  const viewerRef = useRef(null);

  const handleSaveAnnotations = () => {
    const annotations = viewerRef.current.getAnnotations();
    // Save the annotations to a database or local storage
    console.log(annotations);
  };

  const handleLoadAnnotations = (annotations) => {
    viewerRef.current.setAnnotations(annotations);
  };

  return (
    <Viewer
      ref={viewerRef}
      pdf="/path/to/your/pdf.pdf"
      enableAnnotationEditing={true}
      enableHighlightAnnotations={true}

Competitor Comparisons

2,543

Golang PDF library for creating and processing PDF files (pure go)

Pros of unipdf

  • Comprehensive PDF manipulation library with features beyond highlighting
  • Written in Go, offering potential performance benefits
  • Supports both creation and modification of PDF documents

Cons of unipdf

  • Not specifically designed for React integration
  • Requires more setup and configuration for web-based highlighting
  • May have a steeper learning curve for developers primarily working with JavaScript

Code Comparison

react-pdf-highlighter:

<PdfLoader url={url} beforeLoad={<Spinner />}>
  {pdfDocument => (
    <PdfHighlighter
      pdfDocument={pdfDocument}
      enableAreaSelection={true}
      onSelectionFinished={(position, content, hideTipAndSelection) => {
        // Handle highlight creation
      }}
    />
  )}
</PdfLoader>

unipdf:

pdf, err := model.NewPdfReader(pdfFile)
page, err := pdf.GetPage(1)
annotation := model.NewPdfAnnotationHighlight()
annotation.SetColor(model.NewPdfColorDeviceRGB(1, 1, 0))
annotation.SetRectangle(model.PdfRectangle{Llx: 100, Lly: 100, Urx: 200, Ury: 200})
page.AddAnnotation(annotation)

Summary

react-pdf-highlighter is a React-specific library focused on PDF highlighting, while unipdf is a more comprehensive Go-based PDF manipulation library. react-pdf-highlighter offers easier integration for React projects, while unipdf provides broader PDF functionality but requires more setup for web-based highlighting.

47,890

PDF Reader in JavaScript

Pros of pdf.js

  • More comprehensive PDF rendering and manipulation capabilities
  • Wider browser compatibility and support
  • Larger community and more frequent updates

Cons of pdf.js

  • Steeper learning curve for integration
  • Heavier library size, which may impact performance
  • Less focused on highlighting and annotation features

Code Comparison

pdf.js:

pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport({ scale: scale });
    // Render page
  });
});

react-pdf-highlighter:

<PdfLoader url="path/to/document.pdf" beforeLoad={<Spinner />}>
  {pdfDocument => (
    <PdfHighlighter
      pdfDocument={pdfDocument}
      enableAreaSelection={true}
      onSelectionFinished={(position, content, hideTipAndSelection) => {
        // Handle highlight creation
      }}
    />
  )}
</PdfLoader>

The code snippets demonstrate that pdf.js requires more setup for basic rendering, while react-pdf-highlighter provides a more streamlined approach for PDF viewing and highlighting in React applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

☕️ Buy me a coffee

Node CI

react-pdf-highlighter

Set of React components for PDF annotation.

Features:

  • Built on top of PDF.js
  • Text and image highlights
  • Popover text for highlights
  • Scroll to highlights

Example

See demo https://agentcooper.github.io/react-pdf-highlighter/.

To run the example app locally:

npm install
npm start

Install

npm install react-pdf-highlighter

How to use

See ./example/src/App.tsx for the React component API example.

NPM DownloadsLast 30 Days