Loading Documents in React Document Viewer
This article explains how to load documents into the React Document Viewer by selecting values from a drop-down list. The drop-down list is populated with the values that then loaded into the Document Viewer.

In a recent blog post, we showed you how to load a document from a URL into the Document Viewer. But what if the document data is already available on the client side based on React SPA principles? This article shows how to use the client-side JavaScript method loadDocument to load existing data into the Document Viewer.
Learn More
This article shows how to use the TX Text Control Document Viewer in a React application. The Document Viewer is a powerful, web-based document viewing and editing component that is based on TX Text Control.
Using the TX Text Control Document Viewer in a React Application
Selecting the Document
For this example, we are using a simple drop-down list that is populated with the available documents. The selected document is then loaded into the Document Viewer. The following screenshot shows the application:
The drop-down lists contain the values of the documents in HTML format. They can also contain the values as base64-encoded strings in other supported formats, or they can refer to a variable that contains the corresponding data.
<select onChange={e => load(e.currentTarget.value)}>
<option>Select a Document</option>
<option value="Document <strong>#1</strong>">Document 1</option>
<option value="Document <strong>#2</strong>">Document 2</option>
<option value="Document <strong>#3</strong>">Document 3</option>
</select>
The following method load uses the loadDocument method of the Document Viewer to load the selected option value:
const load = (contentValue: string) => {
if (TXDocumentViewer) {
// encode the content to base64
contentValue = btoa(contentValue);
// load the document into the viewer
TXDocumentViewer.loadDocument(contentValue, "loaded.html");
}
};
TXDocumentViewer must be declared as it is a JavaScript object:
declare var TXDocumentViewer: any;
The full code of the index.tsx file would look like the following:
import React from 'react';
import ReactDOM from 'react-dom/client';
import DocumentViewer from '@txtextcontrol/tx-react-document-viewer';
declare var TXDocumentViewer: any;
const App: React.FC = () => {
const load = (contentValue: string) => {
if (TXDocumentViewer) {
// encode the content to base64
contentValue = btoa(contentValue);
// load the document into the viewer
TXDocumentViewer.loadDocument(contentValue, "loaded.html");
}
};
return <>
<select onChange={e => load(e.currentTarget.value)}>
<option>Select a Document</option>
<option value="Document <strong>#1</strong>">Document 1</option>
<option value="Document <strong>#2</strong>">Document 2</option>
<option value="Document <strong>#3</strong>">Document 3</option>
</select>
<br />
<br />
<DocumentViewer
width="800px"
height="500px"
basePath ="https://backend.textcontrol.com?access-token=O5KVBVClFm7Pus1qESin"
isSelectionActivated={true}>
</DocumentViewer>
</>;
};
const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(<App />);
Conclusion
This article showed how to load documents into the React Document Viewer by selecting values from a drop-down list. The drop-down list is populated with the values that are then loaded into the Document Viewer.
Related Posts
AngularASP.NET CoreDocument Viewer
Asynchronous Loading and Saving in Angular Document Editor with an ASP.NET…
This article shows how to load and save documents in an Angular Document Editor using an ASP.NET Core Web API asynchronously. A Web API is used to load and save documents from and to the server…
Building an ASP.NET Core Backend Application to Host the Document Editor and…
This article explains how to create an ASP.NET Core backend application to host the Document Editor and Document Viewer. This backend application is required to provide the required functionality…
Loading Documents into the Document Viewer in React
This article explains how to load documents into the Document Viewer in React. It shows how to load documents from a base64 encoded string and a URL.
TX Text Control React Packages Released
We are very happy to announce the immediate availability of React packages for TX Text Control. The new packages are available on npm and provide a wide range of document processing features…
Using the TX Text Control Document Viewer in a React Application
This article shows how to use the TX Text Control Document Viewer in a React application. The Document Viewer is a powerful, web-based document viewing and editing component that is based on TX…