java使用Spire.Doc生成的word文件去除水印

java使用Spire.Doc生成的word文件去除水印,第1张

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

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/719626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存