Edit Word Documents using REST API in Node.js

You can easily edit your Word documents programmatically on the cloud. You can add, edit the content of the documents or you may apply text formatting in Word documents programmatically in your Node.js applications. This article will be focusing on how to edit Word documents using a REST API in Node.js.

The following topics shall be covered in this article:

Document Editor REST API and Node.js SDK

I will be using the Node.js SDK of GroupDocs.Editor Cloud API for editing the DOCX files. It allows you to programmatically edit documents of the supported formats such as Word, Excel spreadsheets, PowerPoint, TXT, HTML, XML. The API also enables you to convert the document into HTML for editing and converts it back to its original format keeping the same appearance after the document is edited. It also provides .NET, Java, PHP, Ruby, Android, and Python SDKs as its document editor family members for the Cloud API.

You can install GroupDocs.Editor Cloud to your Node.js application using the following command in the console:

npm install groupdocs-editor-cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Edit Word Documents using REST API in Node.js

You can edit the Word documents by following the simple steps mentioned below:

  1. Upload the Word file to the Cloud
  2. Edit Word document using Node.js
  3. Download the updated file

Upload the Document

Firstly, upload the Word document (DOCX) to the Cloud using the code example given below:

As a result, the uploaded DOCX file will be available in the files section of your dashboard on the cloud.

Edit Word Document using Node.js

Please follow the steps mentioned below to edit the Word documents programmatically.

  • Create instances of the FileAPI and the EditAPI
  • Provide the input file path in the FileInfo
  • Create WordProcessingLoadOptions
  • Create LoadRequest with LoadOptions
  • Load a file with the load() method of EditAPI
  • Create DownloadFileRequest with loaded file
  • Download HTML document using downloadFile() method of FileAPI
  • Edit the downloaded HTML Document
  • Create UploadFileRequest
  • Upload HTML back using uploadFile() method of FileAPI
  • Provide WordProcessingSaveOptions to save in the DOCX
  • Create SaveRequest with SaveOptions
  • Save HTML back to DOCX using the save() method of Edit API

The following code snippet shows how to edit a Word document using a REST API in Node.js.

Edit Word Documents using a REST API in Node.js

Edit Word Documents using a REST API in Node.js

Download the Updated File

The above code sample will save the edited Word document (DOCX) on the cloud. You can download it using the following code sample:

Add Table in Word Documents using Node.js

You can add a table in the Word document programmatically by following the steps mentioned below:

  • Create instances of the FileAPI and the EditAPI
  • Provide the input file path in the FileInfo
  • Create WordProcessingLoadOptions
  • Create LoadRequest with LoadOptions
  • Load a file with the load() method of the EditAPI
  • Create DownloadFileRequest with loaded file
  • Download HTML document using the downloadFile() method of the FileAPI
  • Edit the downloaded HTML Document and add a table
  • Create UploadFileRequest
  • Upload HTML back using the uploadFile() method of the FileAPI
  • Provide WordProcessingSaveOptions to save in the DOCX
  • Create SaveRequest with SaveOptions
  • Save HTML back to the DOCX using the save() method of the EditAPI

The following code snippet shows how to add a table in a Word document using a REST API in Node.js.

Add Table in Word Documents using Node.js

Add Table in Word Documents using Node.js

Insert Image in Word Documents using Node.js

You can insert an image in the Word document programmatically by following the steps mentioned below:

  • Create instances of the FileAPI and the EditAPI
  • Provide the input file path in the FileInfo
  • Create WordProcessingLoadOptions
  • Create LoadRequest with LoadOptions
  • Load a file with the load() method of the EditAPI
  • Create DownloadFileRequest with Loaded file
  • Download HTML document using the downloadFile() method of the FileAPI
  • Edit the downloaded HTML Document and insert an image
  • Create UploadFileRequest
  • Upload HTML back using the uploadFile() method of the FileAPI
  • Provide WordProcessingSaveOptions to save in the DOCX
  • Create SaveRequest with SaveOptions
  • Save HTML back to the DOCX using the s_ave()_ method of the EditAPI

The following code snippet shows how to insert an image in a Word document using a REST API in Node.js.

Insert Image in Word Documents using Node.js

Insert Image in Word Documents using Node.js

Try Online

Please try the following free online DOCX editing tool, which is developed using the above API. https://products.groupdocs.app/editor/docx

Conclusion

In this article, you have learned how to edit Word documents on the cloud. You have also learned how to add a table in the DOCX file using a REST API in Node.js. Moreover, you have learned how to insert an image in a Word document programmatically. This article also explained how to programmatically upload a DOCX file on the cloud and then download the edited file from the Cloud. You can learn more about GroupDocs.Editor Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also