chenglong

How to Use PDF.js to Highlight Text Programmatically | PSPDFKit

notion image
In this tutorial, you’ll learn how to programmatically add highlight annotations to a PDF document using PDF.js. Highlight annotations are useful for emphasizing important text or passages within a PDF file. In the first part, we’ll walk you through the steps to integrate PDF.js into your project and demonstrate how to add highlight annotations to a loaded PDF. In the second part, we’ll show you how to add highlight annotations programmatically using PSPDFKit for Web.
PDF.js is primarily designed as a PDF viewer, and its manipulation capabilities may be limited compared to those of dedicated PDF editing software.

Downloading the PDF.js Library

To get started with PDF.js, download the library as a ZIP file, or clone the repository using Git.
Extract the ZIP file and copy the pdf.js and pdf.worker.js files from the build/ folder to your project directory.
In your HTML file, add the following script tag to load the PDF.js library:

Loading and Rendering PDF Documents

To get started, you need to load the PDF document and render it in a specified container. For this tutorial, you’ll use a canvas element to display the PDF page.
Make sure you replace annotation.pdf with the actual URL or path to your PDF document. You can use this demo document as an example:

Adding Highlight Annotations Programmatically

Use the getAnnotations() method to retrieve existing annotations from the PDF page. For this tutorial, you’ll focus on highlight annotations specifically:
The renderHighlightAnnotations(page) function retrieves all annotations from the specified page. If the annotation’s subtype matches 'Highlight', it creates a yellow rectangle (highlight) using a div element positioned and sized according to the annotation’s rectangle coordinates. The highlight is then added to the document body.
After executing these steps, the highlight annotations present in the PDF document will be displayed as yellow rectangles with 50 percent opacity on top of the PDF pages.

PSPDFKit for Web

We at PSPDFKit work on the next generation of PDF viewers for the web. We offer a commercial JavaScript PDF viewer library that can easily be integrated into your web application. PSPDFKit for Web offers 30+ features, enabling users to view, annotate, edit, and sign PDFs directly within the browser.

Requirements

  • Node.js installed on your computer.
  • A code editor of your choice.
  • A package manager compatible with npm.

Adding PSPDFKit to Your Project

  1. Install the pspdfkit package from npm. If you prefer, you can also download PSPDFKit for Web manually:
  1. Next, copy the directory containing all the required library files (artifacts) to your project’s assets folder using the following command:
cp -R ./node_modules/pspdfkit/dist/ ./assets/
Ensure your assets directory contains the pspdfkit.js file and a pspdfkit-lib directory with the library assets.

Integrating into Your Project

Once you’ve added PSPDFKit to your project, you can integrate it into your HTML and JavaScript code.
  1. Add the PDF document you want to display to your project’s directory. You can use our demo document as an example.
  1. Create an empty <div> element with a defined height to where PSPDFKit will be mounted:
  1. Include pspdfkit.js in your HTML page:
  1. Initialize PSPDFKit for Web in JavaScript by calling PSPDFKit.load():

Adding Highlight Annotations Programmatically

To add highlight annotations programmatically, you’ll use the PSPDFKit.Annotations.HighlightAnnotation constructor function provided by PSPDFKit. This function enables you to create a new highlight annotation object with specific properties, such as the page index and the rectangles to highlight:
The provided rects array defines the bounding boxes of the areas to be highlighted, and the pageIndex property specifies the page where the highlight annotation will be added.
notion image

Conclusion

In this tutorial, you explored two methods to programmatically add highlight annotations to a PDF document. First, you learned how to use PDF.js to retrieve existing highlight annotations from a PDF, offering a lightweight solution for accessing annotations in a web application. Next, you learned about PSPDFKit for Web, a commercial JavaScript PDF viewer library that provides advanced PDF editing capabilities, and you saw firsthand how to programmatically add highlight annotations using this powerful tool.
To see a list of all web frameworks, you can contact our Sales team. Or, launch our demo to see our viewer in action.

Copyright © 2024 chenglong

logo