Open and read Word or PDF file in Node SDK

Use REST API to open and read documents programmatically in JavaScript

Easily open Word, PDF documents with just a few lines of JavaScript code. Our Node library will provide you with everything you need to access document content programmatically.

View code snippet

Open and read documents in JavaScript

If you want to open a text file and work with the data in plain-text format, you can do it using the most basic features of the JavaScript language and operating system. As for reading office documents, presented in a wide variety of file formats (DOCX, DOC, PDF, RTF, ODT, HTML, etc.), this task is more difficult to solve, since such documents are described by complex tree-like data structures within the DOM model.

Our JavaScript library is a cloud-based solution that will allow you to open documents in any office and web format. This distributed approach gives you flexibility in software development using JavaScript language, as all resource-intensive document processing will take place on Aspose servers. All you need is to get private keys to access Cloud API in JavaScript code.

Programmatically read Word, PDF documents in JavaScript

You can iterate over document elements, or you can search for specific elements in the DOM tree. We provide a wide range of JavaScript functions both for working with the structure of the document and with the data contained in the document nodes.

The following JavaScript code example will help you quickly figure out how to load a document and perform some actions on it.

Upload a document you want to access
Run code
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");
const fileName = "Input.docx";

const requestFileContent = fs.createReadStream(fs.readFileSync(
   fileName, 'utf8'));
const uploadDocumentRequest = new model.UploadFileRequest({
    fileContent: requestFileContent,
    path: fileName
});

wordsApi.uploadFile(uploadDocumentRequest)
.then((uploadDocumentRequestResult) => {
    console.log("Result of UploadDocumentRequest: ", uploadDocumentRequestResult);
    const requestParagraph = new model.ParagraphInsert({
        text: "Reading and writing to the file in the cloud occurs automatically."
    })
    const insertParagraphRequest = new model.InsertParagraphRequest({
        name: fileName,
        paragraph: requestParagraph
    });

    wordsApi.insertParagraph(insertParagraphRequest)
    .then((insertParagraphRequestResult) => {
        console.log("Result of InsertParagraphRequest: ", insertParagraphRequestResult);
        const downloadDocumentRequest = new model.DownloadFileRequest({
            path: fileName
        });

        wordsApi.downloadFile(downloadDocumentRequest)
        .then((downloadDocumentRequestResult) => {
            console.log("Result of DownloadDocumentRequest: ", downloadDocumentRequestResult);
        });
    });
});
Run code
  
Copy JavaScript code to the clipboard

How to open a document Word, PDF, JPG JavaScript REST API

  1. Install Aspose.Words Cloud for Node
  2. Add a library reference (import the library) to your JavaScript project
  3. Read a document

Install JavaScript library to read documents

Install 'Aspose.Words Cloud SDK for Node.js' using NPM package manager. Run npm install asposewordscloud --save from the command line to install the SDK via the NPM package manager. As an alternative, you can manually clone Aspose.Words Cloud SDK for Node.js source code from GitHub and use it in your project. Please follow these Instructions to quickly get the necessary security credentials and access our REST API.

System Requirements

  • @types/request (version 2.48.3+)
  • lodash (version 4.17.15+)
  • lodash.template (version 4.5.0+)
  • request (version 2.88.0+)
  • request-debug (version 0.2.0+)

Refer to the Repository Documentation to see more details.

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2025. All Rights Reserved.