java使用Spire.Doc生成的word文件去除水印(头部的警告信息)

1.问题

在使用Spire.Doc的word文档产生了警告水印

(Evaluation Warning: The document was created with Spire.Doc for JAVA.)

2.解决方式

 word格式主要有2003 doc格式和2007docx格式,针对不同的格式采用不同的方法。



import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

public class test {
    public static void main(String args[]) throws IOException {
        RemoveTag("C:\\Users\\QYF\\Desktop\\测试去水印.docx");
        RemoveTag("C:\\Users\\QYF\\Desktop\\测试去水印.doc");
    }
    public static void RemoveTag(String file_path) throws IOException {
        InputStream inputStream=null;
        String out_path="C:\\Users\\MYH\\Desktop";
        //加载Word文档
        Document document = new Document(file_path);
        document.replace("张三", "李四", false, true);
        if(file_path.endsWith(".doc")){
            document.saveToFile(out_path+"\\test.doc", FileFormat.Doc);
            inputStream=new FileInputStream(out_path+"\\test.doc");
            HWPFDocument hwpfDocument = new HWPFDocument(inputStream);
            //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
            //hwpfDocument.delete() 该方法去掉文档指定长度的内容
            hwpfDocument.delete(0,70);
            //输出word内容文件流,新输出路径位置
            OutputStream os=new FileOutputStream(out_path+"\\del_tag_test.doc");
            try {
                hwpfDocument.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                hwpfDocument.close();
                os.close();
                inputStream.close();
            }
        }else if(file_path.endsWith(".docx")){
            document.saveToFile(out_path+"\\test.docx", FileFormat.Docx_2013);
            inputStream=new FileInputStream(out_path+"\\test.docx");
            XWPFDocument old_document = new XWPFDocument(inputStream);
            //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
            old_document.removeBodyElement(0);
            //输出word内容文件流,新输出路径位置
            OutputStream os=new FileOutputStream(out_path+"\\del_tag_test.docx");
            try {
                old_document.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                document.close();
                os.close();
                inputStream.close();
            }
        }
    }
}

 3.运行效果

3.1 docx格式

原文档
替换后文档
去水印后文档

 

3.2 doc格式
 

原文档
替换后文档
去水印后文档

 

 

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值