需要添加一个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包方法可直接使用,记得带后缀。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2021-12-02 Java之Calender类数据
2021-12-02 IntelliJ Idea的几种注释快捷键