How to Insert an Image into a DOCX in C#
Due to the design of DOCX files, adding an image to your document may not be as simple as you think. Fortunately, we’ve got some APIs to help with that. Before using the Insert Image API, which I will cover in this tutorial, you should have used the Begin Editing API to open up the process and generate a unique editing URL.
First off, you will need to install the .NET Framework SDK (.NET Core also available):
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8
Once the installation is complete, you have the green light to call the function with the following code:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example
{
public class EditDocumentDocxInsertImageExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");var apiInstance = new EditDocumentApi();
var reqConfig = new DocxInsertImageRequest(); // DocxInsertImageRequest | Document input requesttry
{
// Insert image into a Word DOCX document
DocxInsertImageResponse result = apiInstance.EditDocumentDocxInsertImage(reqConfig);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentDocxInsertImage: " + e.Message );
}
}
}
}
Now you will receive a response indicating the success of the operation and the document URL. If successful, you’re all set to move on to your next editing function, or call the Finish Editing function to obtain your finalized result.