基于POI对Word/PPT/PDF/Excel增加文件水印

您所在的位置:网站首页 wpsppt加水印的方法 基于POI对Word/PPT/PDF/Excel增加文件水印

基于POI对Word/PPT/PDF/Excel增加文件水印

2024-07-02 09:21:16| 来源: 网络整理| 查看: 265

前言

在Web项目中,对于MS Ofice类型的文档有时会存在加密或权限限制的需求,除了对文档增加限制修改密码外,一般需要增加水印以达到溯源的效果。 本文基于POI完成了对常见MS Offce文档(Word/PPT/PDF/Excel)的水印添加操作。

技术要点

添加文档水印的基本步骤是先根据需求生成对应的图像,再通过对应MS Office文档对象提供的方法将图片插入。 不同的文档提供的接口不同,有的自带写水印的方法,有的需要引用第三方组件,有的需要使用awt生成图像写入。

给Word文档添加水印

XWPFHeaderFooterPolicy 官方文档

public static void addWordWaterMark(String inputPath, String outPath, String markStr) { // 读取原始文件 File inputFile = new File(inputPath); XWPFDocument doc = null; try { doc = new XWPFDocument(new FileInputStream(inputFile)); } catch (FileNotFoundException var24) { throw new RuntimeException("源文件不存在"); } catch (IOException var25) { throw new RuntimeException("读取源文件IO异常"); } catch (Exception var26) { throw new RuntimeException("不支持的文档格式"); } // 使用自带工具类完成水印填充 XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy(); headerFooterPolicy.createWatermark(markStr); // 设置文档只读 doc.enforceReadonlyProtection(); // 生成输出文件 File file = new File(outPath); if (!file.exists()) { try { file.createNewFile(); } catch (IOException var23) { throw new RuntimeException("创建输出文件失败"); } } // 打开输出流,将doc写入输出文件 OutputStream os = null; try { os = new FileOutputStream(outPath); doc.write(os); } catch (FileNotFoundException var21) { throw new RuntimeException("创建输出文件失败"); } catch (Exception var22) { throw new RuntimeException("添加文档水印失败"); } finally { if (os != null) { try { os.close(); } catch (IOException var20) { var20.printStackTrace(); } } } } 给PPT文档添加水印

使用了国产PPT组件:Spire-Presentation-JAVA,但是从官网下载的组件会有免费版限制,请自行决定要不要使用。

Spire-Presentation-JAVA 官网 官方教程-添加文字水印

public static void addPptWaterMark(String inputPath, String outPath, String markStr) { //加载PPT源文档 Presentation ppt = new Presentation(); // 设置PPT密码 ppt.encrypt("Ka_ze"); try { ppt.loadFromFile(inputPath); ISlide slide = null; //获取指定幻灯片 for (int o = 0; o for (int j = 0; j e.printStackTrace(); } } 给PDF文档添加水印

使用了开源组件iText,可以用于创建和操作 PDF 文档。

maven仓库 官方文档-itext-v7.0.0 官方Github

com.itextpdf itext-asian 5.2.0 com.itextpdf itextpdf 5.4.3 public static void addPDFWaterMark(String input, String output, String waterMarkName) { BufferedOutputStream bos = null; try { // 读取文件,生成reader com.itextpdf.text.pdf.PdfReader reader = new com.itextpdf.text.pdf.PdfReader(input); // 生成输出文件,开启输出流 bos = new BufferedOutputStream(new FileOutputStream(new File(output))); // 读取输出流,生成stamper(印章) com.itextpdf.text.pdf.PdfStamper stamper = new com.itextpdf.text.pdf.PdfStamper(reader, bos); // 设置stamper加密 stamper.setEncryption(null, "Ka_ze".getBytes(StandardCharsets.UTF_8), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); // 获取总页数 +1, 下面从1开始遍历 int total = reader.getNumberOfPages() + 1; // 使用classpath下面的字体库 com.itextpdf.text.pdf.BaseFont base = null; try { base = com.itextpdf.text.pdf.BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", com.itextpdf.text.pdf.BaseFont.EMBEDDED); } catch (Exception e) { // 日志处理 e.printStackTrace(); } // 间隔 int interval = -15; // 获取水印文字的高度和宽度 int textH = 0, textW = 0; JLabel label = new JLabel(); label.setText(waterMarkName); FontMetrics metrics = label.getFontMetrics(label.getFont()); textH = metrics.getHeight(); textW = metrics.stringWidth(label.getText()); // 设置水印透明度 com.itextpdf.text.pdf.PdfGState gs = new com.itextpdf.text.pdf.PdfGState(); gs.setFillOpacity(0.2f); gs.setStrokeOpacity(0.7f); com.itextpdf.text.Rectangle pageSizeWithRotation = null; PdfContentByte content = null; for (int i = 1; i for (int width = interval + textW; width e.printStackTrace(); } } 给Excel文档添加水印

使用awt包的Graphics2D设置图像属性。

private static void addExcelWaterMark(String inputSrc, String outputSrc, String waterMarkName) { // 读取水印文字 String[] textArray = waterMark.split("\n"); // 设置字体 Font font = new Font("microsoft-yahei", Font.PLAIN, 20); // 单元水印图片宽高 int width = 500; int height = 200; // 缓冲区图片对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 基于特定宽高,创建图形对象 Graphics2D g = image.createGraphics(); // 基于图形对象生成半透明图片 image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); // 释放画笔 g.dispose(); // 基于半透明图片,创建图形对象 g = image.createGraphics(); // 设置图形颜色 g.setColor(new Color(Integer.parseInt("#C5CBCF".substring(1), 16))); // 设置图形字体 g.setFont(font); // 设置图形倾斜度 g.shear(0.1, -0.26); // 设置字体平滑 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 画出字符串 int y = 150; for (int i = 0; i // 将内存中的图片写入至输出流 ImageIO.write(image, "png", os); // 读取原文档 XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputSrc)); // 将图片输出流写入Excel int pictureIdx = workbook.addPicture(os.toByteArray(), Workbook.PICTURE_TYPE_PNG); // 获取工作簿中的图片对象 POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx); for (int i = 0; i workbook.write(new FileOutputStream(outputSrc)); } finally { os.close(); } } catch (Exception e) { e.printStackTrace(); } }


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭