一、依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
二、工具类
public class PdfUtils {
public static ByteArrayOutputStream addWaterMark(String inputFile, String text, String fontUrl) throws Exception {
return addWaterMark(inputFile, text, fontUrl, 0.2f, 20, 45, 10, 1.4f);
}
/**
* pdf添加水印
*
* @param inputFile 需要添加水印的文件
* @param text 需要添加的水印文字
* @param fontUrl 水印文本字体文件路径
* @param opacity 水印字体透明度
* @param fontSize 水印字体大小
* @param angle 水印倾斜角度(0-360)
* @param heightDensity 数值越大每页竖向水印越少
* @param widthDensity 数值越大每页横向水印越少
* @return
* @throws Exception
*/
public static ByteArrayOutputStream addWaterMark(String inputFile, String text, String fontUrl, float opacity,
int fontSize, int angle, float heightDensity, float widthDensity) throws Exception {
// 0.读取原pdf
PdfReader reader = new PdfReader(inputFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
// 1.准备 字符串的高度、长度、透明度、字体类型 等参数
JLabel label = new JLabel();
FontMetrics metrics;
int textH = 0;
int textW = 0;
label.setText(text);
metrics = label.getFontMetrics(label.getFont());
//字符串的高, 只和字体有关
textH = metrics.getHeight();
//字符串的宽
textW = metrics.stringWidth(label.getText());
// 水印透明度、字体设置
PdfGState gs = new PdfGState();
// 设置填充字体不透明度为0.2f
gs.setFillOpacity(opacity);
// 准备字体类型
BaseFont base = BaseFont.createFont(fontUrl, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 2.循环PDF,每页添加水印
PdfContentByte waterMar = null;
com.itextpdf.text.Rectangle pageRect = null;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
pageRect = reader.getPageSizeWithRotation(i);
// 设置透明度、字体类型、字体大小
waterMar = stamper.getOverContent(i);
waterMar.saveState();
waterMar.setGState(gs);
waterMar.setFontAndSize(base, fontSize);
// 开始添加水印
waterMar.beginText();
// 添加水印
for (float height = textH; height < pageRect.getHeight() * 2; height = height + textH * heightDensity) {
for (float width = textW; width < pageRect.getWidth() * 1.5 + textW; width = width + textW * widthDensity) {
waterMar.showTextAligned(Element.ALIGN_LEFT, text, width - textW, height - textH, angle);
}
}
// 设置水印颜色
waterMar.setColorFill(BaseColor.GRAY);
//结束设置
waterMar.endText();
waterMar.stroke();
}
stamper.close();
return bos;
}
/**
* 添加图片
*
* @param inputFile pdf文件地址
* @param imagePath 图片地址
* @param x 图片所在位置x
* @param y 图片所在位置y
* @param width 图片宽度
* @param height 图片高度
* @return
* @throws IOException
* @throws DocumentException
*/
public static ByteArrayOutputStream addImage(String inputFile, String imagePath, float x, float y, float width,
float height) throws IOException, DocumentException {
// 读取原pdf
PdfReader reader = new PdfReader(inputFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
// 给每一页添加水印
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
PdfContentByte waterMark = stamper.getOverContent(i);
addImage(waterMark, imagePath, x, y, width, height);
}
stamper.close();
return bos;
}
private static void addImage(PdfContentByte waterMar, String imgpath, float x, float y, float width, float height)
throws DocumentException, IOException {
waterMar.beginText();
// 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
com.itextpdf.text.Image img = Image.getInstance(imgpath);
img.setAbsolutePosition(x, y);
img.scaleAbsolute(width, height);
waterMar.addImage(img);
// 设置水印颜色
waterMar.setColorFill(BaseColor.GRAY);
//结束设置
waterMar.endText();
waterMar.stroke();
}
}
三、测试
@SpringBootTest(classes = DaApplication.class)
class ApplicationTests {
@Autowired
private FileConfig fileConfig;
@Test
public void testAddWaterMark() throws Exception {
ByteArrayOutputStream bos = PdfUtils.addWaterMark("e:\\website\\pdf\\1.pdf", "苏州工业园区张三李四",
fileConfig.fontUrl + FontGenerater.FONT_SONTI);
FileOutputStream out = new FileOutputStream("e:\\website\\pdf\\demo.pdf");
out.write(bos.toByteArray());
out.close();
}
@Test
public void testAddImage() throws Exception {
ByteArrayOutputStream bos = PdfUtils.addImage("e:\\website\\pdf\\1.pdf", "e:\\website\\pdf\\问号.png",
80, 750, 50, 50);
FileOutputStream out = new FileOutputStream("e:\\website\\pdf\\imageDemo.pdf");
out.write(bos.toByteArray());
out.close();
}
}
测试结果
添加图片
四、注意
- 工具类中的添加图片是给每一页进行添加,有具体页码要求可自行修改。
网友评论