1,引入maven依赖,使用2.7.3免费版本
如果引入包失败,需要注意是否 http://repo.e-/nexus/content/groups/public/ 这个镜像下载的包
<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-/nexus/content/groups/public/</url></repository></repositories>
<dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>2.7.3</version></dependency>
2,java代码
import com.spire.doc.*;public class Encrypt {public static void main(String[] args){//加载测试文档String input = "test.docx";String output= "result.docx";Document doc = new Document(input);doc.encrypt("123");//设置文档打开密码//doc.protect(ProtectionType.Allow_Only_Reading,"123");//设置文档只读密码//doc.protect(ProtectionType.Allow_Only_Comments,"123");//设置文档只允许添加批注//doc.protect(ProtectionType.Allow_Only_Form_Fields,"123");//只允许表单域//doc.protect(ProtectionType.Allow_Only_Revisions,"123");//只允许修订//保存加密后的文档doc.saveToFile(output);doc.dispose();}}