java 实现给PDF、PPT添加水印

  1. java实现PDF添加水印

pom.xml引入依赖

<dependency>

<groupId>com.itextpdf</groupId>

<artifactId>itextpdf</artifactId>

<version>5.5.13.3</version> <!-- 请使用最新的稳定版本 -->

</dependency>

java 复制代码
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
/**
 * PDF文件水印添加
 * @author jia
 *
 */
public class PDFWaterMarkUtil {

	/**
	 * 
	 * @param srcPath 源文件路径
	 * @param destPath 目的文件路径
	 * @param word 添加水印(不支持汉字)
	 * @throws Exception
	 */
	public static void addPDFWaterMark(String srcPath, String destPath, String word)
            throws Exception {

        PdfReader reader = new PdfReader(srcPath);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPath));


        //创建字体,第一个参数是字体路径
        BaseFont base = BaseFont.createFont();
        //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

        PdfGState gs = new PdfGState();
        gs.setFillOpacity(0.2f);//图片水印透明度
        //gs.setStrokeOpacity(0.4f);//设置笔触字体不透明度
        PdfContentByte content = null;

        int total = reader.getNumberOfPages();//pdf文件页数
        for (int i=0; i<total; i++) {
            float x = reader.getPageSize(i+1).getWidth();//页宽度
            float y = reader.getPageSize(i+1).getHeight();//页高度
            content = stamper.getOverContent(i+1);
            content.setGState(gs);
            content.beginText();//开始写入
            content.setFontAndSize(base, 20);//字体大小
            //每页3行,一行3个
            for (int j=0; j<3; j++) {
                for (int k=0; k<3; k++) {
                    //showTextAligned 方法的参数(文字对齐方式,位置内容,输出水印X轴位置,Y轴位置,旋转角度)
                    content.showTextAligned(Element.ALIGN_CENTER, word, x/3*j+100, y/3*k+100, 45);
                }
            }
            content.endText();//结束写入
        }
        //关闭流
        stamper.close();
        reader.close();
    }
	
	public static void main(String[] args) {

		// 获取指定路径的pdf
		try {
			addPDFWaterMark("H:\\test.pdf" , "H:\\example_water.pdf" , "jia");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	
}

2.java实现PPT添加水印

pom.xml引入poi

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>4.1.2</version>

</dependency>

java 复制代码
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.sl.usermodel.TextShape.TextDirection;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextBox;

public class PPTWaterMarkUtil {
	
	public static void setPPTWaterMark(String path,String targetpath, String markStr) throws IOException {
		
          XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(path));
          double x = slideShow.getPageSize().getWidth();
          double y = slideShow.getPageSize().getHeight();
          for (XSLFSlide slide : slideShow.getSlides()) {
          	for (int j=0; j< 3; j++) {
//	                for (int k=0; k< 2; k++) {
//	                    
	                    XSLFTextBox textBox = slide.createTextBox();
	                    textBox.setTextDirection(TextDirection.VERTICAL_270);//设置文本框文字方向
	                    textBox.setVerticalAlignment(VerticalAlignment.MIDDLE);
	                	textBox.setText(markStr);
	                	textBox.setAnchor(new Rectangle2D.Double(x/3*j+90,y/2, 25, 160)); // 设置水印文本框的位置和大小
	                    textBox.setRotation(45); // 设置水印文本框的旋转角度
//	                    textBox.setFillColor(new Color(0, 0, 0, 128)); // 设置水印文本框的填充颜色
	                    textBox.setLineColor(new Color(0, 0, 0, 128));
//	                    textBox.setLineWidth(1);
	                    
//	                }
	            }
          }

          FileOutputStream out = new FileOutputStream(targetpath);
          slideShow.write(out);
          out.close();
    }
	
	public static void main(String[] args) {
        try {
            setPPTWaterMark("H:/waterppt.pptx", "H:/watermark0.pptx", "Hello World!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

3.效果

相关推荐
weixin_8368695201 小时前
Java中的机器学习模型集成与训练
java·开发语言·机器学习
VX_DZbishe1 小时前
springboot旅游管理系统-计算机毕业设计源码16021
java·spring boot·python·servlet·django·flask·php
橙子味冰可乐1 小时前
isprintable()方法——判断字符是否为可打印字符
java·前端·javascript·数据库·python
yunpeng.zhou1 小时前
logging 模块简单使用记录
java·前端·数据库
嗨!陌生人2 小时前
SpringSecurity中文文档(Servlet Session Management)
java·hadoop·spring boot·后端·spring cloud·servlet
广西千灵通网络科技有限公司2 小时前
基于Java的微信记账小程序【附源码】
java·微信·小程序
凉拌糖醋鱼4 小时前
Python-PDF文件密码破解小工具
开发语言·python·pdf·密码破解
shangjg36 小时前
如何实现高可用的分布式系统
java·分布式
G皮T7 小时前
【Spring Boot】Java 的数据库连接模板:JDBCTemplate
java·数据库·spring boot·jdbc·jdbctemplate
Arran阿蓝7 小时前
8.javaSE基础进阶_泛型generics(无解通配符?+上下界统配符super&extends)
java·jvm·windows·intellij-idea