This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /blog/how-to-build-a-react-word-viewer.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. How to build a React DOCX viewer: Preview and render Word files

Table of contents

    How to build a React DOCX viewer: Preview and render Word files
    TL;DR

    This tutorial demonstrates how to build a React Word viewer using Nutrient Web SDK to display DOC/DOCX files directly in browsers without server-side processing. The implementation involves creating a React project with Vite, installing the SDK, copying library assets to the public directory, and creating a viewer component. The SDK converts Office documents to PDF client-side, enabling features like text editing, annotations, and signatures without requiring Microsoft Office software or licenses. Font substitutions are handled automatically when custom fonts aren’t available, and the viewer can be customized further with additional capabilities like document assembly and redaction.

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

    This tutorial covers viewing — rendering a Word file so users can read it. If you need users to edit the document and export their changes, see how to integrate a DOCX editor in React and Next.js instead.

    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 Microsoft Office software, Microsoft Office licenses, or third-party open source software. The technology works by converting an Office document to PDF directly in the browser using our Office-to-PDF conversion engine, and the document is then rendered in our JavaScript viewer.

    Additional capabilities with Office-to-PDF conversion

    After converting an Office document to PDF in the browser, you can add features to the PDF representation. Available features depend on your license and selected components:

    • Text editing — Edit text in the converted PDF.
    • Page manipulation — Organize documents by adding, removing, or rearranging pages.
    • Annotations — Add text highlights, comments, or stamps.
    • Adding signatures — Draw, type, or upload a signature on the converted PDF.
    Explore Demo

    Requirements to get started

    To get started, you’ll need:

    Setting up a new React project with Vite

    1. To get started, create a new React project using Vite:
    Terminal window
    # Using Yarn
    yarn create vite nutrient-react-example --template react
    # Using npm
    npm create vite@latest nutrient-react-example -- --template react
    1. Change to the created project directory:
    cd nutrient-react-example

    Adding Nutrient to your project

    1. Add the Nutrient dependency:
    Terminal window
    npm i @nutrient-sdk/viewer
    1. Nutrient Web SDK loads its WebAssembly and supporting files from a local path, so you need to copy them to the public folder. Start by installing the required copy plugin:
    Terminal window
    npm install -D rollup-plugin-copy
    1. Then, update your Vite configuration (vite.config.ts) to copy the SDK’s asset files during build:
    import { defineConfig } from 'vite';
    import react from '@vitejs/plugin-react';
    import copy from 'rollup-plugin-copy';
    export default defineConfig({
    plugins: [
    copy({
    targets: [
    {
    src:
    'node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer-lib',
    dest: 'public/',
    },
    ],
    hook: 'buildStart',
    }),
    react(),
    ],
    });

    Displaying a Word document

    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. Render the Word document as a PDF representation using Nutrient Web SDK.

    Basic usage in App.tsx:

    import { useEffect, useRef } from 'react';
    function App() {
    const containerRef = useRef<HTMLDivElement>(null);
    useEffect(() => {
    const container = containerRef.current;
    if (!container) return;
    // Give each effect run its own target, including in Strict Mode.
    const target = document.createElement('div');
    target.style.height = '100%';
    container.appendChild(target);
    let cancelled = false;
    let unload = () => {};
    const setup = (async () => {
    const NutrientViewer = (await import('@nutrient-sdk/viewer')).default;
    if (cancelled) return;
    unload = () => NutrientViewer.unload(target);
    await NutrientViewer.load({
    container: target,
    document: 'document.docx',
    baseUrl: new URL(import.meta.env.BASE_URL, window.location.origin).href,
    });
    })().catch(error => {
    if (!cancelled) console.error('Failed to load document', error);
    });
    return () => {
    cancelled = true;
    target.remove();
    // Wait for a pending load before disposing of its instance.
    void setup.then(() => unload());
    };
    }, []);
    return <div ref={containerRef} style={{ height: '100vh', width: '100%' }} />;
    }
    export default App;
    1. Start your development server:
    Terminal window
    # Using Yarn
    yarn dev
    # Using npm
    npm run dev

    Once the server is running, you can view the converted Word document 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 Nutrient SDK.

    Test the viewer with your Word files to check font substitution and layout before integrating it into your application.

    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

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

    Use 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 @nutrient-sdk/viewer or npm install @nutrient-sdk/viewer. Then copy the library assets to the public directory.

    Can I add annotations or signatures to the Word document?

    Yes, on the PDF representation created from the Word file. You can export those changes as PDF; this viewer workflow doesn’t write them back to DOCX. Use Document Authoring when you need DOCX editing and export.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

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

    Explore related topics

    Try for free Ready to get started?