随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

需要添加一个jar包(百度就行很多):

 

 

package com.seeyon.apps.watermark.utils;

import com.itextpdf.text.pdf.PdfContentByte;
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.documents.WatermarkLayout;
import com.spire.doc.fields.ShapeObject;
import net.coobird.thumbnailator.filters.Watermark;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.awt.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WordAddWaterMark {
    private static final Log log = LogFactory.getLog(PdfAddWaterMark.class);

    public static void setWatermark(String filePath, String savePath, String waterInfo,String suffix) throws IOException {
        //加载示例文档
        Document doc = new Document();
        log.info("加载文档");
        doc.loadFromFile(filePath);

        //添加艺术字并设置大小
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.setWidth(150);
        shape.setHeight(60);
        //设置艺术字文本内容、位置及样式
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(315);
        shape.getWordArt().setFontFamily("宋体");
        shape.getWordArt().setText(waterInfo);//设置信息
        shape.setFillColor(Color.LIGHT_GRAY);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(Color.LIGHT_GRAY);
        shape.setStrokeWeight(0.5);

        Section section;
        HeaderFooter header;
        for (int n = 0; n < doc.getSections().getCount(); n++) {
            section = doc.getSections().get(n);
            //获取section的页眉
            header = section.getHeadersFooters().getHeader();
            Paragraph paragraph;
            if (header.getParagraphs().getCount() > 0) {
                //如果页眉有段落,取它第一个段落
                paragraph = header.getParagraphs().get(0);
            } else {
                //否则新增加一个段落到页眉
                paragraph = header.addParagraph();
            }
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 3; j++) {
                    //复制艺术字并设置多行多列位置
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(50 + 150 * i);
                    shape.setHorizontalPosition(20 + 160 * j);
                    paragraph.getChildObjects().add(shape);
                }
            }
        }
        log.info("保存文档");
        //保存文档
        doc.saveToFile(savePath, FileFormat.Doc);
        RemoveTag(savePath, savePath,suffix);
    }
    private static void RemoveTag(String file_path,String savePath,String suffix) throws IOException {
        log.info("移除头部警告!");
        InputStream inputStream=null;
        //加载Word文档
        Document document = new Document(file_path);
        if(suffix.equals("doc")){
            log.info("doc处理分支");
            document.saveToFile(file_path, FileFormat.Doc);
            inputStream= Files.newInputStream(Paths.get(file_path));
            HWPFDocument hwpfDocument = new HWPFDocument(inputStream);
            //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
            //hwpfDocument.delete() 该方法去掉文档指定长度的内容
            hwpfDocument.delete(0,70);
            //输出word内容文件流,新输出路径位置
            OutputStream os= Files.newOutputStream(Paths.get(savePath));
            try {
                hwpfDocument.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                hwpfDocument.close();
                os.close();
                inputStream.close();
            }
        }else if(suffix.equals("docx")){
            log.info("docx处理分支");
            document.saveToFile(file_path, FileFormat.Docx_2013);
            inputStream= Files.newInputStream(Paths.get(file_path));
            XWPFDocument old_document = new XWPFDocument(inputStream);
            //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
            old_document.removeBodyElement(0);
            //输出word内容文件流,新输出路径位置
            OutputStream os= Files.newOutputStream(Paths.get(savePath));
            try {
                old_document.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                document.close();
                os.close();
                inputStream.close();
            }
        }
    }
//    public static void main(String[] args) throws IOException { //加载测试文档
//        String filePath = "C:\\Users\\26072\\OneDrive\\桌面\\2.docx";
//        String savePath = "F:\\870_1_Files\\LogFiles\\tw.docx";
//        String waterInfo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//        setWatermark(filePath, savePath, waterInfo);
//    }
}

添加了jar包方法可直接使用,记得带后缀。

posted on 2022-12-02 10:57  时间完全不够用啊  阅读(323)  评论(0编辑  收藏  举报