Spire.Cloud.Word 给开发人员提供了DocumentPropertiesApi接口,可用于添加、获取和删除Word文档自定义属性。本文将介绍如何使用该接口来实现上述操作。
详细步骤如下:
1、创建一个Maven程序,通过Maven仓库安装Spire.Cloud.SDK jar包及依赖,详细步骤可参考这篇文章。
2、通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获得App ID及App Key。
3、上传Word文档至冰蓝云官网的“文档管理”版块。为了便于文档管理,您也可以先创建文件夹“input”和“output”,然后将需要编辑的Word文档上传至input文件夹下,output文件夹用于存放生成的文档。
4、在Java程序中编写代码来添加、获取和删除Word文档自定义属性。
示例1、添加自定义文档属性
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.DocumentPropertiesApi;
import spire.cloud.word.sdk.client.model.*;
import java.util.ArrayList;
import java.util.List;
public class addCustomDocumentProperties {
static String appId = " APP ID ";
static String appKey = " APP Key ";
static String baseUrl = "https://api.e-iceblue.cn";
//配置APP ID和APP Key
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//创建documentPropertiesApi实例
static DocumentPropertiesApi documentPropertiesApi = new DocumentPropertiesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//示例文档名称
String name = "test.docx";
//示例文档的密码
String password = null;
//存放示例文档的文件夹
String folder = "input";
//使用冰蓝云默认的存储空间
String storage = null;
//输出文档存放路径
String destFilePath = "output/addCustomDocumentProperties_out.docx";
//设置自定义文档属性
List properties = new ArrayList();
properties.add(new CustomDocumentProperty("Name1", "Value1"));
properties.add(new CustomDocumentProperty("Name2", "Value2"));
//调用addCustomDocumentProperties添加自定义文档属性
documentPropertiesApi.addCustomDocumentProperties(name, properties, destFilePath, password, folder, storage);
}
}
示例2、获取自定义文档属性
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.DocumentPropertiesApi;
import spire.cloud.word.sdk.client.model.*;
import java.util.List;
public class getCustomDocumentProperties {
static String appId = " APP ID ";
static String appKey = " APP Key ";
static String baseUrl = "https://api.e-iceblue.cn";
//配置APP ID和APP Key
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//创建documentPropertiesApi实例
static DocumentPropertiesApi documentPropertiesApi = new DocumentPropertiesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//示例文档名称
String name = "Sample.docx";
//示例文档的密码
String password = null;
//存放示例文档的文件夹
String folder = "input";
//使用冰蓝云默认的存储空间
String storage = null;
//调用getCustomDocumentProperties读取自定义文档属性
List response = documentPropertiesApi.getCustomDocumentProperties(name, password, folder, storage);
System.out.println(response);
}
}
示例3、删除自定义文档属性
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.DocumentPropertiesApi;
public class deleteCustomDocumentProperties {
static String appId = " APP ID ";
static String appKey = " APP Key ";
static String baseUrl = "https://api.e-iceblue.cn";
//配置APP ID和APP Key
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//创建documentPropertiesApi实例
static DocumentPropertiesApi documentPropertiesApi = new DocumentPropertiesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//示例文档名称
String name = "Sample.docx";
//示例文档自定义属性中需要删除的项目
String propertieName = "Owner";
//示例文档的密码
String password = null;
//存放示例文档的文件夹
String folder = "input";
//使用冰蓝云默认的存储空间
String storage = null;
//输出文档存放路径
String destFilePath = "output/deleteCustomDocumentProperties_output.docx";
//调用deleteCustomDocumentProperty删除指定的自定义文档属性
documentPropertiesApi.deleteCustomDocumentProperty(name, propertieName, destFilePath, password, folder, storage);
}
}