- 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.效果