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 PSPDFKit for 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

PSPDFKit for Web 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 PSPDFKit to Your Project

  1. Add the PSPDFKit dependency:

yarn add pspdfkit
npm install --save pspdfkit
  1. Copy the PSPDFKit for Web 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 PSPDFKit 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, PSPDFKit for Web might not have access to these fonts due to licensing constraints. In this case, PSPDFKit 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 PSPDFKit SDK.

If you’re looking for a way to render Office documents in your web application, then PSPDFKit for Web Standalone 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 PSPDFKit for 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 PSPDFKit to my React project?

Install PSPDFKit 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 PSPDFKit.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at PSPDFKit 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 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
PRODUCTS  |  Web

Introducing AI Document Assistant: Enhancing the Document Experience with Cutting-Edge AI Features

DEVELOPMENT  |  WebAssembly • JavaScript • Web • Document Processing

Leveraging WebAssembly in JavaScript for High-Performance Document Processing

DESIGN  |  Baseline UI • Web

Optimizing Icon Design: Our Journey to the Baseline UI Icon Set