Display Files in the Document Viewer - ReactJS

This tutorial shows how to load and display files in the Document Viewer in a React JS application using the LEADTOOLS SDK.

Summary This tutorial covers how to load files into a DocumentViewer object in a React JS.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (312 KB)
Platform React JS Web Application
IDE Visual Studio : Service \ Visual Studio Code : Client
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Display Files in the Document Viewer - ReactJS tutorial.

Make sure that Yarn is installed so that creating a React application can be done quickly via the command line. If yarn is not installed, it can be found on:


Get Started

Start by creating a template of the project by running the following command in a terminal npx create-react-app tutorialname --template typescript. This command will create the project folder and subfolders necessary to build the project.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have a copy of that tutorial project, follow the steps inside that tutorial to create it.

The references needed depend upon the purpose of the project. For this project, the following JS and TS files are needed:

Make sure to copy the JS files to the public\common folder and import them in the public\index.html file.

For more information on which files to include for your JavaScript application, see Files to be Included with your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Import LEADTOOLS Dependencies

Open the index.html file in the public folder and add the below necessary script tags inside the head to import LEADTOOLS dependencies.

   <meta charset="utf-8" /> 
   <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> 
   <meta name="viewport" content="width=device-width, initial-scale=1" /> 
   <meta name="theme-color" content="#000000" /> 
      content="Web site created using create-react-app" 
   <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> 
   <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> 
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>    
    <script src="/Common/Leadtools.js"></script>  
    <script src="/Common/Leadtools.Controls.js"></script>    
    <script src="/Common/Leadtools.Annotations.Engine.js"></script>    
    <script src="/Common/Leadtools.Annotations.Designers.js"></script>    
    <script src="/Common/Leadtools.Annotations.Rendering.Javascript.js"></script>    
    <script src="/Common/Leadtools.Annotations.Automation.js"></script>    
    <script src="/Common/Leadtools.ImageProcessing.Main.js"></script>    
    <script src="/Common/Leadtools.ImageProcessing.Color.js"></script>    
    <script src="/Common/Leadtools.ImageProcessing.Core.js"></script>    
    <script src="/Common/Leadtools.ImageProcessing.Effects.js"></script>    
    <script src="/Common/Leadtools.Document.js"></script>    
    <script src="/Common/Leadtools.Document.Viewer.js"></script>   
   <title>React App</title> 

Add the Document Viewer Code

With the project created, the references added, and the license set coding can begin.

Create a new folder named Components in the src folder and add two Typescript files named DocumentViewer.tsx and Toolbar.tsx. Insert the following code into the DocumentViewer.tsx file:

function DocumentViewer() { 
    return ( 
        <div className='container'> 
            <div id="thumbnail" /> 
            <div id="viewer" /> 
export default DocumentViewer; 

Then enter the following code in the Toolbar.tsx Typescript file:

import { ChangeEvent, useState, MouseEvent } from "react"; 
import panZoomLogo from "../Resources/PanZoomMode.png"; 
import selectTextLogo from "../Resources/SelectTextMode.png"; 
// create the props for the input and button events 
interface Props { 
    inputOnChange: (e: ChangeEvent<HTMLInputElement>) => void 
    selectTextButtonClick: () => void, 
    panZoomButtonClick: () => void, 
const Toolbar = (props: Props) => { 
    // maintain the selected interactive mode button in the state 
    const [selectedButton, setselectedButton] = useState('panZoom'); 
    // handle the click events of the buttons 
    function handleClick(sender: string, method: () => void) { 
    return ( 
        <div className="Toolbar"> 
            <input type="file" id="file" onChange={(e) => props.inputOnChange(e)} /> 
            <button className={selectedButton === 'selectText' ? 'selected' : ''} id="selectText" onClick={() => handleClick('selectText', props.selectTextButtonClick)}> 
                			<div class="tutorial-image"><img src={selectTextLogo} /></div> 
            <button className={selectedButton === 'panZoom' ? 'selected' : ''} id="panZoom" onClick={() => handleClick('panZoom', props.panZoomButtonClick)}> 
                			<div class="tutorial-image"><img src={panZoomLogo} /></div> 
export default Toolbar; 


The PanZoomMode.png and SelectTextMode.png can be found in this tutorial's downloadable .zip file

With the DocumentViewer and Toolbar code added, we can now add code to make the document viewer functional. Create a new Typescript file named App.tsx in the src folder and add the following code:

// Reference paths to enable intellisense 
///<reference path="../public/LEADTOOLS/Leadtools.d.ts"/> 
///<reference path="../public/LEADTOOLS/Leadtools.Annotations.Automation.d.ts"/> 
///<reference path="../public/LEADTOOLS/Leadtools.Annotations.Engine.d.ts"/> 
///<reference path="../public/LEADTOOLS/Leadtools.Annotations.Rendering.JavaScript.d.ts"/> 
///<reference path="../public/LEADTOOLS/Leadtools.Document.d.ts"/> 
///<reference path="../public/LEADTOOLS/Leadtools.Document.Viewer.d.ts"/> 
import './App.css'; 
import DocumentViewer from './Components/DocumentViewer'; 
import { useEffect, useState } from 'react'; 
import Toolbar from './Components/Toolbar'; 
function App() { 
  // set state of DocumentViewer in app 
  const [documentViewer, setDocumentViewer] = useState<lt.Document.Viewer.DocumentViewer>(); 
  // on page mount, initialize the app 
  useEffect(() => { 
  }, []); 
  // set the license and create the document viewer 
  function init() { 
   var licenseUrl = "./Leadtools/LEADTOOLS.lic.txt";  
   var developerKey = "ADD THE CONTENTS OF YOUR LEADTOOLS.lic.key.txt FILE";  
    lt.RasterSupport.setLicenseUri(licenseUrl, developerKey, async () => { 
      if (await initFactory()) 
        await createDocumentViewer(); 
  // set the service path and then verify the service is ready 
  async function initFactory(): Promise<Boolean> { 
    lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
    lt.Document.DocumentFactory.servicePath = ""; 
    lt.Document.DocumentFactory.serviceApiPath = "api"; 
    let serviceStatus: lt.Document.ServiceStatus = await lt.Document.DocumentFactory.verifyService(); 
    if (serviceStatus.message !== "Ready" || serviceStatus.isLicenseExpired) { 
      alert('Cannot connect to service'); 
      return false; 
    return true; 
  // create the document viewer, load a default pdf, set pan zoom as the default interactive mode 
  async function createDocumentViewer() { 
    const createOptions = new lt.Document.Viewer.DocumentViewerCreateOptions(); 
    createOptions.viewContainer = document.getElementById("viewer") as HTMLElement; 
    createOptions.thumbnailsContainer = document.getElementById("thumbnail") as HTMLElement; 
    createOptions.viewCreateOptions.useElements = true; 
    let docViewer = lt.Document.Viewer.DocumentViewerFactory.createDocumentViewer(createOptions); 
    docViewer.view.preferredItemType = lt.Document.Viewer.DocumentViewerItemType.image; 
    const url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
    let doc = await lt.Document.DocumentFactory.loadFromUri(url, new lt.Document.LoadDocumentOptions()); 
    docViewer.commands.run(lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom, null); 
  // handle the file input changed event 
  function inputChanged(selectorFiles: FileList | null) { 
    if (selectorFiles != null && selectorFiles.length > 0) { 
  // event to set the interactive modes from the buttoms 
  function setInteractiveMode(command: string) { 
    if (documentViewer) 
      documentViewer.commands.run(command, null); 
  // event to load the uploaded file 
  async function loadFile(file: File) { 
    if (documentViewer != null) { 
      var uploadPromise: any; 
      uploadPromise = lt.Document.DocumentFactory.loadFromFile(file, new lt.Document.LoadDocumentOptions()); 
      uploadPromise.done((doc: lt.Document.LEADDocument): void => { 
  return ( 
      <Toolbar inputOnChange={(e) => inputChanged(e.target.files)} panZoomButtonClick={() => setInteractiveMode(lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom)} selectTextButtonClick={() => setInteractiveMode(lt.Document.Viewer.DocumentViewerCommands.interactiveSelectText)} /> 
      <DocumentViewer /> 
export default App; 

Improve the Visuals of the Project

Navigate to App.css, in the src folder which creates our HTML elements. Add the following code to improve the visuals of the application.

   Remove default body styling.   
   Set the body to flex as a column;   
#root { 
  height: 100%; 
  width: 100%; 
  background-color: darkgray; 
body { 
  display: flex; 
  flex-direction: row; 
.Toolbar { 
  display: flex; 
  flex-direction: row; 
  justify-content: center; 
  column-gap: 10px; 
  margin: 20px; 
.container { 
  height: 98%; 
  width: 100%; 
  display: flex; 
  flex-direction: row; 
  background-color: lightgray; 
.selected { 
  background-color: #c9e0f7 
#viewer { 
  width: 90%; 
#thumbnail { 
  width: 10%; 
.lt-image-border { 
  /* Box Shadow (view, item, image border) */ 
  box-shadow: #333 2px 2px 5px 1px; 
.lt-thumb-item { 
  /* View */ 
  margin: 5px; 
  padding: 5px; 
.lt-item { 
  /* Item */ 
  border: 2px solid #6ecaab; 
  background-color: #b2decf; 
  padding: 10px; 
.lt-image-border { 
  /* Image Border */ 
  border: 3px solid #444; 
  background-color: white; 
.lt-thumb-item { 
  /* Thumbnail Item */ 
  border: 2px solid #6ecaab; 
  background-color: #b2decf; 
.lt-thumb-item.lt-thumb-item-selected { 
  /* Selected Thumbnail Item */ 
  border: 2px solid #59b2ba; 
  background-color: #8ce1e1; 
.lt-thumb-item-hovered { 
  /* Hovered Thumbnail Item */ 
  border: 2px solid #52b996; 
  background-color: #87c7b1; 
If you don't want the React Developer window popping up, then add this code to the end of App.css:

iframe { 
  display: none; 

Run the Document Service

In order to run this application successfully, the LEADTOOLS .NET Framework Document Service is required. The LEADTOOLS .NET Framework Document Service project is located at <INSTALL_DIR>\LEADTOOLS22\Examples\Document\JS\DocumentServiceDotNet\fx.


.NET FrameWork Document Service supports uploadDocumentBlob but .NET Core Document Service does not.

Open the DocumentService.csproj and run the project using IIS Express. After running the csproj Document Service project in Visual Studio, the webpage will show that the service is listening. The Client Side will be able to communicate with the Document Service, allowing the Image Data processing, and returning the OCR's text from the image.

Document service

Run the Project

To run the Document Viewer React application open a new terminal and cd into the root of the project. From there, run yarn start. If you do not have the node modules included with the project, be sure to also run the command, npm install before running the project.

The application will run and open the browser. The DocumentViewer object appears and a sample PDF document indicated in the url variable passed into the loadFromUri function appears. This tutorial allows you to pan/zoom the document, view all the pages, and supports drawing annotations. To draw annotations, select one of the two annotation objects in the drop-down menu, then click and drag to draw the annotation object.


This tutorial showed how to initialize, load, and display a document into a DocumentViewer object. It also showed how to add Automated Annotations support.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.