Spire.Cloud.Word 加密 Word 文档

前言:

Spire.Cloud 在线编辑器是一款基于网页的 Office 文件编辑工具,支持在网页中打开、编辑、打印 Word、Excel、PPT 文件,支持将文档保存到私有云盘。支持 IE、Chrome、FireFox、搜狗、遨游、360 等常见浏览器。Spire.Cloud Web API 能帮助开发人员能在任何时间、任何地点直接调用 SDK 接口对 Word、Excel、PPT、PDF 文档进行操作。Spire.Cloud 支持 .NET、Java、PHP、Python、JavaScript 等多种编程语言,并提供了 1 万次的免费调用次数及 2G 文档内存。

本文将通过实例阐述如何通过Spire.Cloud.Word API给开发人员提供的EncryptApi接口,来加密保护Word文档以及使用在线编辑器查看生成文档。

详细步骤:

1、通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获得App ID及App Key。

2、上传Word文档至冰蓝云官网的“文档管理”版块。为了便于文档管理,您也可以先创建文件夹“input”和“output”,然后将需要编辑的Word文档上传至input文件夹下,output文件夹用于存放生成的文档。

 3、创建Maven应用程序,通过Maven仓库安装Spire.Cloud.SDK jar包及依赖。详细步骤参考文章

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>cloud</name>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>

<dependencies>

    <dependency>
            <groupId> cloud </groupId>
            <artifactId>spire.cloud.sdk</artifactId>
            <version>3.5.0</version>
        </dependency>

    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.5.18</version>
    </dependency>

    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
    </dependency>

    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>2.7.5</version>
    </dependency>

    <dependency>
        <groupId> com.squareup.okio </groupId>
        <artifactId>okio</artifactId>
        <version>1.6.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
    </dependency>

    <dependency>
        <groupId>io.gsonfire</groupId>
        <artifactId>gson-fire</artifactId>
        <version>1.8.0</version>
    </dependency>

    <dependency>
        <groupId>org.threeten</groupId>
        <artifactId>threetenbp</artifactId>
        <version>1.3.5</version>
    </dependency>

</dependencies>

4、新建Java class,调用Spire.Cloud.Word API为input文件夹下的示例文档设置密码。

Java代码:

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.e-iceblue.cn";

    //配置App ID和App Key
    static Configuration wordConfiguration = new Configuration(appId, appKey);
    //创建EncryptApi实例
    static EncryptApi encryptApi = new EncryptApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        //原文档名称
        String name = "test.docx";

        //存放原文档的文件夹,没有则为null
        String folder = "input";

        //原文档密码,没有则为null
        String oldPassword = null;

        //输入新密码加密保护文档
        String newPassword = "123456";

        //使用冰蓝云配置的2G空间存贮文档,可设置为null
        String storage = null;

        //指定生成文档的存放路径
        String destFilePath = "output/encryptWord.docx";
        
        //调用encryptDocument方法对文档进行加密
        encryptApi.encryptDocument(name, destFilePath, folder, storage, oldPassword, newPassword);
    }
}

使用Spire.Cloud在线编辑打开加密后的Word文档效果图:

原文地址:https://www.cnblogs.com/jazz-z/p/13402461.html