How to delete any object in a Word DOCX document in Node.JS

For today’s example, our goal is to delete a specific object from a DOCX file, such as an image, table, or paragraph. Using an API, this task will be greatly simplified, allowing us to finish almost instantly, as you will soon see.

With this dependency reference for package.json, we will be able to add our API client to the project.

"dependencies": {
"cloudmersive-convert-api-client": "^2.1.6"

Our function call for editDocumentDocxRemoveObject will come next, which makes use of a newly instantiated API class object. If you look at the following example, you will see exactly what I mean.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.EditDocumentApi();var reqConfig = new CloudmersiveConvertApiClient.DocxRemoveObjectRequest(); // DocxRemoveObjectRequest | Document input requestvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editDocumentDocxRemoveObject(reqConfig, callback);

And thats done! Our output will include a URL, which can be used to call the Finish Editing API to give you the edited document. To get lists of your available objects in the DOCX, you can use Get Tables, Get Images, and Get Get Body.

Why deal with all this when you could just be done in a couple minutes?

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store