# How to Build a React PDF Viewer with Annotation Support

Canonical: https://www.dynamsoft.com/codepool/pdf-creating-viewing-and-annotating-in-react.html
Markdown: https://www.dynamsoft.com/codepool/pdf-creating-viewing-and-annotating-in-react.md
Description: Step-by-step tutorial to build a React PDF viewer and annotator with TypeScript using Dynamsoft Document Viewer. Create, view, and annotate PDFs in a web app.
Author: Lihang Xu
Published: 2024-10-10
Updated: 2026-10-20
Topics: JavaScript, HTML5, Document Viewer, PDF, Annotation, React

[Dynamsoft Document Viewer](https://www.dynamsoft.com/document-viewer/docs/introduction/index.html) is an SDK providing a set of viewers for document images. We can use it to incorporate PDF creating, viewing and annotating into our web apps. In this article, we are going to build a fully functional PDF viewer and annotator in a React + TypeScript project.


**What you'll build:** A React web app that can create, view, and annotate PDF documents using the Dynamsoft Document Viewer SDK, complete with a toolbar, thumbnail panel, and annotation tools.

## Key Takeaways

- Dynamsoft Document Viewer provides a drop-in React-compatible component for PDF viewing, annotation, and creation with minimal setup code.
- The `EditViewer` class renders a full-featured document editor with built-in annotation tools, thumbnail navigation, and image filters.
- WASM engine resources must be copied to the public folder at build time — the `ncp` package automates this in both dev and production workflows.
- This approach works for any React + TypeScript project scaffolded with Vite and can be extended with programmatic document manipulation via the SDK's API.

## Common Developer Questions
#### How do I add a PDF viewer with annotation support to a React app?

Install `dynamsoft-document-viewer`, copy its engine assets into the React app's public folder, initialize the SDK once the app mounts, and create an `EditViewer` bound to a container element. Passing `includeAnnotationSet: true` in the UI config turns on the built-in annotation tools immediately.

### What is the easiest way to create and edit PDFs in a React TypeScript project?

Start with a Vite React TypeScript project, add the DDV package, and let the SDK handle PDF creation, viewing, and annotation inside its viewer component. This avoids building your own canvas editor while still leaving room for programmatic document operations through the API.

### How do I integrate Dynamsoft Document Viewer into a Vite-based React application?

Install the package with npm, use `ncp` in the Vite scripts to copy the DDV distribution files into `public/assets/ddv-resources`, set `DDV.Core.engineResourcePath` to that location, and initialize the viewer from `App.tsx`. That asset-copy step is the main integration detail Vite projects need.

## Prerequisites

- Node.js 16 or later
- npm or yarn
- A Dynamsoft Document Viewer license. [Get a 30-day free trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) to follow along.

## Step 1: Create a New React Project

Create a new React + TypeScript project with Vite:

```bash
npm create vite@latest pdf-app -- --template react-ts
```

## Step 2: Install Dynamsoft Document Viewer

First, install Dynamsoft Document Viewer via npm.


```
npm install dynamsoft-document-viewer
```

Then, we need to copy the resources of Dynamsoft Document Viewer to the public folder.

1. Create a new folder under `public/assets/ddv-resources`.
2. Install `ncp`: `npm install ncp --save-dev`.
3. Modify `package.json` to copy the resources from `node_modules` to the public folder.

   ```diff
   - "dev": "vite",
   - "build": "tsc -b && vite build",
   + "dev": "ncp node_modules/dynamsoft-document-viewer/dist public/assets/ddv-resources && vite",
   + "build": "ncp node_modules/dynamsoft-document-viewer/dist public/assets/ddv-resources && tsc -b && vite build",
   ```

## Step 3: Initialize the SDK with a License Key

Let's rewrite `App.tsx`. When it is mounted, initialize Dynamsoft Document Viewer with a license. You can apply for a license [here](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform).

```tsx
const initializing = useRef(false);
const editViewer = useRef<EditViewer|undefined>();
useEffect(()=>{
  if (initializing.current == false) {
    initializing.current = true;
    initDDV();
  }
},[])

const initDDV = async () => {
  DDV.Core.license = "LICENSE-KEY"; // Public trial license which is valid for 24 hours
  DDV.Core.engineResourcePath = "assets/ddv-resources/engine";// Lead to a folder containing the distributed WASM files
  await DDV.Core.loadWasm();
  await DDV.Core.init(); 
  // Configure image filter feature which is in edit viewer
  DDV.setProcessingHandler("imageFilter", new DDV.ImageFilter());
}
```

## Step 4: Create an Edit Viewer with Annotation Support

1. Use the following in the JSX:

   ```tsx
   return (
     <div id="app">
       <h2>Document Viewer Demo</h2>
       <div id="container"></div>
     </div>
   )
   ```
   
   CSS:
   
   ```css
   #app {
     display: flex;
     align-items: center;
     flex-direction: column;
     width: 100%;
   }

   #container {
     max-width: 80%;
     width: 1280px;
     height: 480px;
   }
   ```

2. Initialize an instance of Edit Viewer and bind it to a container via its ID.

   ```tsx
   const config = DDV.getDefaultUiConfig("editViewer", {includeAnnotationSet: true}) as UiConfig;
   // Create an edit viewer
   editViewer.current = new DDV.EditViewer({
     container: "container",
     uiConfig: config,
   });
   ```
   
3. Import the CSS of Dynamsoft Document Viewer.

   ```tsx
   import "dynamsoft-document-viewer/dist/ddv.css";
   ```

All right, we've incorporated Dynamsoft Document Viewer in our React app.

![React Demo](./img/2024/10/document-viewer/react-demo.jpg)

The viewer provides a toolbar, a thumbnail viewer and a main viewer for the document. We can click the buttons on the toolbar to load a new image, edit images, add PDF annotations and save the document as a PDF file.

Apart from the UI, we can also manipulate the document via code. Read the [docs](https://www.dynamsoft.com/document-viewer/docs/features/index.html) to learn more.

## Common Issues and Edge Cases

- **WASM files not found at runtime:** If the viewer fails to load, verify that the `ncp` copy step ran before the dev server started. Check that `public/assets/ddv-resources/engine` contains the `.wasm` files. A missing or incorrect `engineResourcePath` is the most common setup error.
- **License key expired or invalid:** The public trial key expires after 24 hours. Replace it with a valid trial or production key. An expired key results in a watermark overlay or an initialization error.
- **Viewer container has zero height:** The `EditViewer` requires its container element to have an explicit height. If the viewer renders blank, inspect the container's computed styles and ensure `height` is set (e.g., `480px`).

## Source Code

[Get the complete sample project source code on GitHub](https://github.com/yushulx/web-twain-document-scan-management/tree/main/examples/document-viewer-samples/frameworks/react/pdf-app)
