要求有点另类,
但文档涉及部门权限和机密,也就可以理解了
非常简单
import com.spire.doc.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//加载测试文档
String input = "E://a.docx";
String output= "E://b.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_Revisions,"123");
//保存加密后的文档
doc.saveToFile(output);
doc.dispose();
}
}
后来变卦了,机密要求有点高 要pdf的
package com.sinosoft.common.wordEncrypt;
import com.spire.doc.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//加载测试文档
String input = "E://a.doc";
String targetPath = "E://bb.pdf";
Document document = new com.spire.doc.Document();
System.out.println("================== 读取文件开始" + targetPath);
//加载word文档
document.loadFromFile(input);
//保存结果文件
document.saveToFile(targetPath, com.spire.doc.FileFormat.PDF);
}
}
add:
小常识: pdf还是可以编辑的, 想要pdf不可编辑, 需要加数字签名
https://www.e-iceblue.cn/pdf_java_security/add-digital-signature-to-pdf-in-java.html
spire处理pdf后会在第一页加信息 ,如何去掉:
https://blog.csdn.net/qq_21416547/article/details/108250016
简而言之:
a.getPages().add();
...
a.getPages().remove(a.getPages().get(a.getPages().getCount()-1));