Spire.Cloud.Word offers the ImageApi interface to add, get or delete an image in Word. This article will introduce how to add an image to Word, set the size, location and rotation angle of the image, and finally delete it.
Add an image to Word
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ImagesApi;
public class AddImage {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on your AppID and AppKey
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an ImageApi instance
static ImagesApi imagesApi = new ImagesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a Word document
String name = "Sample.docx";
//Load an Image file
String imagePath = "logo.png";
String paragraphPath = "Section/0/Body/0/Paragraph/0";
//Specify the folder storing the document, and it's "null" if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
String storage = null;
//Specify the password used to open the document, and it's "null" if nothing
String password = null;
Integer indexInParagraph = 0;
//Save the resulting document
String destFilePath = "output/addImage_out.docx";
//Call the addImage method to add an image to Word
imagesApi.addImage(name, imagePath, paragraphPath, destFilePath, folder, storage, password, indexInParagraph);
}
}
Output
From the screenshot of the resulting document above, the size of the image is large and now we can use the ImagesAPI interface provided by Spire.Cloud.Word to reset its size, location and rotation angel.
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ImagesApi;
import spire.cloud.word.sdk.client.model.ImageFormat;
public class UpdateImage {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on your AppID and AppKey
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an ImageApi instance
static ImagesApi imagesApi = new ImagesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a Word document
String name = "addImage_out.docx";
String paragraphPath = "Section/0/Body/0/Paragraph/0";
Integer index = 0;
//Set the size of the image
ImageFormat format = new ImageFormat();
format.setWidth(150);
format.setHeight(150);
format.setVerticalPosition(30);
format.setHorizontalPosition(30);
//Set the rotation angle of the image
format.setRotation(20);
//Specify the folder storing the document, and it's "null" if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
String storage = null;
//Specify the password used to open the document, and it's "null" if nothing
String password = null;
//Save the resulting document
String destFilePath = "output/updateImageFormat.docx";
//Call the updateImageFormat method to reset an image in Word
AddImage.imagesApi.updateImageFormat(name, paragraphPath, index, destFilePath, format, password, folder, storage);
}
}
Output
Delete an image in Word
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ImagesApi;
public class DeleteImage {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on your AppID and AppKey
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an ImageApi instance
static ImagesApi imagesApi = new ImagesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a Word document
String name = "updateImageFormat.docx";
String paragraphPath = "Section/0/Body/0/Paragraph/0";
Integer index = 0;
//Specify the folder storing the document, and it's "null" if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue, and it's "null" by default
String storage = null;
//Specify the password used to open the document, and it's "null" if nothing
String password = null;
//Save the resulting document
String destFilePath = "output/DeleteImage.docx";
//Call the deleteImage method to delete an image in Word
imagesApi.deleteImage(name, paragraphPath, index, destFilePath, folder, storage, password);
}
}
Output