Insert an Image into an Online Word Document in Java

In an effort to optimize your DOCX document editing procedure, we have created several APIs that can shorten your processing time and simplify your task. One such API is our Insert Image tool, which follows the Begin Editing function in the editing path.

To start things off, you can install the Maven SDK by adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then, add a reference to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

Now before we call our function, you will need the request configuration to input:

{
"InputFileBytes": "string",
"InputFileUrl": "string",
"ParagraphToInsert": {
"ParagraphIndex": 0,
"Path": "string",
"ContentRuns": [
{
"RunIndex": 0,
"Path": "string",
"TextItems": [
{
"TextIndex": 0,
"Path": "string",
"TextContent": "string"
}
],
"Bold": true,
"Italic": true,
"Underline": "string",
"FontFamily": "string",
"FontSize": "string"
}
],
"StyleID": "string"
},
"InsertPlacement": "string",
"InsertPath": "string"
}

After this, we are officially ready to call our Insert Image function:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditDocumentApi apiInstance = new EditDocumentApi();
InsertDocxInsertParagraphRequest reqConfig = new InsertDocxInsertParagraphRequest(); // InsertDocxInsertParagraphRequest | Document input request
try {
InsertDocxInsertParagraphResponse result = apiInstance.editDocumentDocxInsertParagraph(reqConfig);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditDocumentApi#editDocumentDocxInsertParagraph");
e.printStackTrace();
}

To conclude the process, a result will be returned with your edited document URL; this can be used for continued modifications, or to wrap things up and download your document with the Finish Editing function. If you want to retrieve your personal API key, head to the Cloudmersive website to register for a free account and receive access to 800 calls/month across our library of APIs.

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