How to preview a word document in HTML using javascript

Created at 24-Sep-2021 , By samar

How to preview a word document in HTML using javascript

In this session, we are going to try to solve the "How to preview a word document in HTML using javascript" puzzle by using the computer language.

You can preview a word document in HTML page using javascript. You can display word documents (.doc, .docx) as HTML document in the browser on choose file.
  • Preview MS word docx document on choose file using mammoth

    <!-- Javascript CDN (jQuery and mammoth library) -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.18/mammoth.browser.min.js" integrity="sha512-Z8jpnlnXO9rena5GNKiX0EHQRNLLh0LobtlTESOc55UMcQPOdxBpSMrU9MMZI1b5Xoph9bPMFbNyi9s33Du0EA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    
    <!-- HTML -->
    <!-- Input file to choose file and target div to preview docx content -->
    <input type="file" id="file" name="document" accept=".doc, .docx"/>
    <div id="docPreview"></div>
    
    <!-- Javascript code -->
    <script>
        $("body").on("change", "#file", function(e){
            parseWordDocxFile(e.target.files, '#docPreview');
        });
    
        function parseWordDocxFile(inputElement, showDiv) {
            var files = inputElement || [];
            if (!files.length) return;
            var file = files[0];
            console.time();
            var reader = new FileReader();
            reader.onloadend = function(event) {
                
                var arrayBuffer = reader.result;
                mammoth.convertToHtml({arrayBuffer: arrayBuffer}).then(function (resultObject) {
                    console.log(resultObject.value);
                    $(showDiv).html(resultObject.value);
                    console.log(resultObject.value);
                })
                console.timeEnd();
                mammoth.extractRawText({arrayBuffer: arrayBuffer}).then(function (resultObject) {
                    result2.innerHTML = resultObject.value;
                    console.log(resultObject.value);
                })
    
                mammoth.convertToMarkdown({arrayBuffer: arrayBuffer}).then(function (resultObject) {
                    result3.innerHTML = resultObject.value;
                    console.log(resultObject.value);
                })
            };
            reader.readAsArrayBuffer(file);
        }
    </script>
    

    Here we have a mammoth ajax library to display the document on the chosen file. Here we have two html element input file to choose the file and target div to show the output (doc content) on choosing the input file.

  • How do I open a Word document with JavaScript?

    function PreviewWordDoc() {
    //URL of the Word Document.
    var url = "DOCs/Sample. docx";
    //Send a XmlHttpRequest to the URL.
    var request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.responseType = 'blob';
    request.onload = function () {
    
  • How do I render a word document .doc .docx in the browser using JavaScript?

    The Word (Docx) file will be displayed (rendered) in Browser using docx-preview. js JavaScript plugin. Note: This library only works for Word 2007 and higher formats (docx) and not the Word 97 – 2003 formats (doc). The following files JavaScript plugin will be used.

  • How do I embed a Word document in an HTML page?

    Right-click in the Embed Code box, and click Copy. In your blog editor, begin writing your post. When you want to embed the document, switch to HTML editing and press Ctrl+V (or ⌘+V on a Mac) to paste the embed code.

  • How do I make a file preview in HTML?

    First, open the html file you are editing from the File : Open dialog, or from the Open File icon on the toolbar. Click on the toggle Browser Preview on the toolbar or from the View menu. This will give you a quick browser preview. Click on the button again and it will return to the code view.

  • How do I preview a PDF in JavaScript?

    • Canvas → Where the pdf will be rendered.
    • Previous button → To go to the previous page.
    • Next button → To go to the next page.
    • Input box → To enter a page number.
    • Go to page button → Button to go to a particular page.
    • 2-span elements → Display the current page number and total pages of the PDF.
  • How do you preview in JavaScript?

    Step 1: Create a Basic Layout for the Image Preview Using HTML. Add a div element with a class named image-preview-container. Step 2: Design the Image Preview Section Using CSS. Step 3: Display the Preview of the Image Using JavaScript.

  • How to read word document in Javascript?

    By examining various real-world cases, we've shown how to fix the Javascript Read Word Document bug.

    // using docxtemplater

    var zip = new JSZip(content);
    var doc=new Docxtemplater().loadZip(zip)
    var text= doc.getFullText();
    console.log(text);
    
  • How to embed Microsoft world and excel in HTML ?

    Just append your src attribute with an appropriate URL to a specific doc viewer, it will download your file from URL and then generate an HTML.

  • What is HTML preview?

    The HTML preview action step provides a way to display HTML in a local in-app web browser. The primary use of this step is to preview content intended for the web, and is typically used along with content created in Markdown to preview the HTML output. HTML Previews are not limited to this purpose.

Back to code snippet queries related javascript

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.