java docx 文档不可编辑、复制

一、方式一
1.引入pom

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>4.0.1</version>
</dependency>

2.示例

public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream("C:\\Users\\Mac\\Downloads\\QQQ.docx");
        XWPFDocument doc = new XWPFDocument(is);
        //设置编辑限制的类型及文档保护密码
        doc.enforceFillingFormsProtection("123", HashAlgorithm.sha512);
        OutputStream os=new FileOutputStream("C:\\Users\\Mac\\Downloads\\QQ.docx");
        doc.write(os);
    }

二、方式二
1.引入pom

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>4.0.1</version>
</dependency>
<dependency>
    <groupId> e-iceblue </groupId>
    <artifactId>spire.doc</artifactId>
    <version>5.4.2</version>
</dependency>
<repository>
    <id>com.e-iceblue</id>
    <name>e-iceblue</name>
    <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>

2.示例

public static void main(String[] args) throws Exception {
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Mac\\Downloads\\QQQ.docx");
        //不允许任何更改(只读)
        document.protect(ProtectionType.Allow_Only_Reading, "123456");
        //仅允许批注
        //document.protect(ProtectionType.Allow_Only_Comments, "123456");
        //仅允许填写窗体
        //document.protect(ProtectionType.Allow_Only_Form_Fields, "123456");
        //仅允许修订
        //document.protect(ProtectionType.Allow_Only_Revisions, "123456");
        //保存文档
        document.saveToFile("C:\\Users\\Mac\\Downloads\\Q.docx", FileFormat.Docx_2013);
        InputStream is = new FileInputStream("C:\\Users\\Mac\\Downloads\\Q.docx");
        XWPFDocument doc = new XWPFDocument(is);
        //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
        doc.removeBodyElement(0);
        //输出word内容文件流,新输出路径位置
        OutputStream os=new FileOutputStream("C:\\Users\\Mac\\Downloads\\QQ.docx");
        doc.write(os);
    }

参考文档:
https://www.e-iceblue.cn/docforjavasecurity/set-editing-restrictions-in-word-in-java.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值