How to insert an image into a Word DOCX document in Node.JS

Having support for the ever-ubiquitous DOCX format in your app is generally a necessity these days. I’m here today to show you how to get this handled in a real hurry. With an easy-to-use API, we will be done before you can even finish your coffee.

Client installation is our first task:

npm install cloudmersive-convert-api-client --save

Calling the editDocumentDocxInsertImage is our next priority. This is going to first require us to instance our API. All the details are illustrated in this lovely example we have here:

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.DocxInsertImageRequest(); // DocxInsertImageRequest | Document input requestvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editDocumentDocxInsertImage(reqConfig, callback);

Now we just need to set up our DocxInsertImageRequest object parameters. Here is what to expect in that department:

"InputDocumentFileBytes": "string",
"InputDocumentFileUrl": "string",
"InputImageFileBytes": "string",
"InputImageFileUrl": "string",
"ImageToAdd": {
"Path": "string",
"ImageName": "string",
"ImageId": 0,
"ImageDescription": "string",
"ImageWidth": 0,
"ImageHeight": 0,
"XOffset": 0,
"YOffset": 0,
"ImageDataEmbedId": "string",
"ImageDataContentType": "string",
"ImageInternalFileName": "string",
"ImageContentsURL": "string",
"InlineWithText": true
"InsertPlacement": "string",
"InsertPath": "string",
"WidthInEMUs": 0,
"HeightInEMUs": 0

As you can see, it allows for plenty of customization. Now just run the code, and voila! Your document now has your image.



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

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