Blog Post

How to build a React Word (DOC and DOCX) viewer

Illustration: How to build a React Word (DOC and DOCX) viewer
Information

This article was first published in April 2023 and was updated in August 2024.

In this blog post, learn how to build a React Word viewer using the Nutrient Web SDK. You’ll open and view DOC or DOCX files directly in your web browser using client-side processing (no server required).

The image below shows what you’ll be building.

resulting image

You can check out the demo to see it in action.

Opening and rendering Office documents in the browser

Nutrient Web SDK brings support for Word, Excel, and PowerPoint formats to your application, without you or your users needing any MS Office software, MS Office licenses, or third-party open source software. The technology works by converting an Office document to PDF directly in the browser, and the document is then rendered in our JavaScript viewer.

Unlocking more capabilities with Office-to-PDF conversion

​​By converting an Office document to PDF using client-side JavaScript, you have the option to support a rich array of additional Office document functionality, such as:

  • Text editing — Edit text directly in the displayed Office document.

  • Page manipulation — Organize documents by adding, removing, or rearranging pages.

  • Annotations — Boost collaboration by adding text highlights, comments, or stamps.

  • Adding signatures — Draw, type, or upload a signature directly to a Word document.

Explore Demo

Requirements to get started

To get started, you’ll need:

  • The latest version of Node.js.

  • A package manager compatible with npm. This post contains usage examples for Yarn and the npm client (installed with Node.js by default).

Setting up a new React project with Vite

  1. To get started, create a new React project using Vite:

# Using Yarn
yarn create vite pspdfkit-react-example --template react

# Using npm
npm create vite@latest pspdfkit-react-example -- --template react
  1. Change to the created project directory:

cd pspdfkit-react-example

Adding Nutrient to your project

  1. Add the Nutrient dependency:

yarn add pspdfkit
npm install --save pspdfkit
  1. Copy the Nutrient Web SDK library assets to the public directory:

cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib

The code above will copy the pspdfkit-lib directory from within node_modules/ into the public/ directory to make it available to the SDK at runtime.

  1. Make sure your public directory contains a pspdfkit-lib directory with the Nutrient library assets.

Displaying a PDF

  1. Add the DOC or DOCX document you want to display to the public directory. You can use our demo document as an example.

  2. Create a new component to handle PDF rendering. In the src/components/ directory, create a file named PdfViewerComponent.jsx with the following content:

import { useEffect, useRef } from 'react';

export default function PdfViewerComponent(props) {
	const containerRef = useRef(null);

	useEffect(() => {
		const container = containerRef.current;
		let PSPDFKit, instance;

		(async function () {
			PSPDFKit = await import('pspdfkit');

			PSPDFKit.unload(container);

			instance = await PSPDFKit.load({
				container,
				document: props.document,
				baseUrl: `${window.location.protocol}//${
					window.location.host
				}/${import.meta.env.BASE_URL}`,
			});
		})();

		return () => PSPDFKit && PSPDFKit.unload(container);
	}, []);

	return (
		<div
			ref={containerRef}
			style={{ width: '100%', height: '100vh' }}
		/>
	);
}
  1. Include the newly created component in App.jsx:

// src/App.jsx

import PdfViewerComponent from './components/PdfViewerComponent';

function App() {
	return (
		<div className="App" style={{ width: '100vw' }}>
			<div className="PDF-viewer">
				<PdfViewerComponent document={'document.docx'} />
			</div>
		</div>
	);
}

export default App;
  1. Your project structure will now look like this:

pspdfkit-react-example/
├── public/
│   ├── pspdfkit-lib/
│   └── document.docx
├── src/
│   ├── components/
│   |   └── PdfViewerComponent.jsx
|   └── App.jsx
├── package.json
└── yarn.lock
  1. Start your development server:

# Using Yarn
yarn dev

# Using npm
npm run dev

Once the server is running, you can view and interact with your PDF directly in the browser.

A note about fonts

When you convert an Office document with custom fonts to a PDF, Nutrient Web SDK might not have access to these fonts due to licensing constraints. In this case, Nutrient typically replaces unavailable fonts with their equivalents — like Arial with Noto.

Adding even more capabilities

Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular React.js guides:

Conclusion

In this blog post, you learned how to build a Word viewer using React with the Nutrient SDK.

If you’re looking for a way to render Office documents in your web application, then Nutrient Web SDK is a great option. It’s a powerful and flexible library that can help you provide your users with a seamless and enjoyable experience.

To get started, you can either:

  • Start your free trial to test out the library and see how it works in your application.

  • Launch our demo to see the viewer in action.

FAQ

Here are a few frequently asked questions about building a React Word viewer.

How do I build a React Word (DOC and DOCX) viewer?

Use the Nutrient Web SDK to build a Word viewer in React, allowing you to open and view DOC or DOCX files directly in the browser.

How do I add Nutrient to my React project?

Install Nutrient using yarn add pspdfkit or npm install pspdfkit. Then copy the library assets to the public directory.

Can I add annotations or signatures to the Word document?

Yes, you can add text highlights, comments, stamps, and signatures to the Word document using Nutrient.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Related products
Share post
Free trial Ready to get started?
Free trial