栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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格式
 
原文档
替换后文档
去水印后文档

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/825279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号