Spire.Cloud.Word provides the DocumentPropertiesApi interface for developers to add, get and delete custom document properties in Word. This article will demonstrate how to do it programmatically using Java codes.
Add Custom Document Properties
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.DocumentPropertiesApi;
import spire.cloud.word.sdk.client.model.*;
import java.util.*;
public class AddProperties {
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 a DocumentPropertiesApi instance
static DocumentPropertiesApi documentPropertiesApi = new DocumentPropertiesApi
(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a Word document
String name = "Sample.docx";
//Specify the password of the document, and it's null if nothing
String password = null;
//Specify the folder storing the document, and it's null if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue Cloud, and it's null by default
String storage = null;
//Set custom document properties
List properties = new ArrayList();
properties.add(new CustomDocumentProperty("name1", "value1"));
properties.add(new CustomDocumentProperty("name2", "value2"));
//Save the resulting document to a specified path
String destFilePath = "addCustomDocumentProperties_out.docx";
//Call the addCustomDocumentProperties method to set custom properties of the document
documentPropertiesApi.addCustomDocumentProperties(name, properties, destFilePath, password, folder, storage);
}
}
Output
Get Custom Document Properties
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.DocumentPropertiesApi;
import java.util.*;
public class GetProperties {
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 a DocumentPropertiesApi instance
static DocumentPropertiesApi documentPropertiesApi = new DocumentPropertiesApi
(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a Word document
String name = "addCustomDocumentProperties_out.docx";
//Specify the password of the document, and it's null if nothing
String password = null;
//Specify the folder storing the document, and it's null if nothing
String folder ="input";
//Use the 2G storage provided by E-iceblue Cloud, and it's null by default
String storage = null;
//Call the getCustomDocumentProperties method to get custom document properties of the document
List response = documentPropertiesApi.getCustomDocumentProperties(name, password, folder, storage);
System.out.println(response);
}
}
Output
Delete Custom Document Properties
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.DocumentPropertiesApi;
public class DeleteProperties {
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 a DocumentPropertiesApi instance
static DocumentPropertiesApi documentPropertiesApi = new DocumentPropertiesApi
(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a Word document
String name = "addCustomDocumentProperties_out.docx";
//Specify the name of custom document property to be deleted
String propertieName = "name1";
//Specify the password of the document, and it's null if nothing
String password = null;
//Specify the folder storing the document, and it's null if nothing
String folder ="input";
//Use the 2G storage provided by E-iceblue Cloud, and it's null by default
String storage = null;
//Save the resulting document to a specified path
String destFilePath = "deleteCustomDocumentProperties_output.docx";
//Call the deleteCustomDocumentProperty method to delete the specified custom property in the document
documentPropertiesApi.deleteCustomDocumentProperty(name, propertieName, destFilePath, password, folder, storage);
}
}
Output