Spire.Cloud.Word provides the EncryptApi interface for developers to encrypt Word documents with password in Java application. This article will introduce how to encrypt a Word document using this interface.
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.EncryptApi;
public class EncryptWord {
static String appId = "APP ID";
static String appKey = "APP Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base Url
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an instance of EncryptApi
static EncryptApi encryptApi = new EncryptApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Specify the name of the input document
String name = "test.docx";
//Specify the folder where the input document is stored, set it to null if there's none
String folder = "input";
//Specify the original password of the input document, set it to null if there's none
String oldPassword = null;
//Set a new password to encrypt the input document
String newPassword = "123456";
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Specify the name and storage path of the generated document
String destFilePath = "output/encryptWord.docx";
//Encrypt the Word document with the encryptDocument method
encryptApi.encryptDocument(name, destFilePath, folder, storage, oldPassword, newPassword);
}
}