当前位置:网站首页>pdf加水印
pdf加水印
2022-04-23 07:34:00 【beinlife】
pdf加水印
相关jar包
- itext-2.1.7.jar
- iTextAsian.jar
代码如下
package com.solex.waterPrint;
import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/** * 给PDF添加水印 * @author solexit06 * */
public class TestWaterPdf {
public static void main(String[] args) throws DocumentException,IOException {
//要输出的文件
String outFile="C:/Users/solexit06/Desktop/WaterPrint.pdf";
//要加水印的原文件
String inputFile="C:/Users/solexit06/Desktop/testWaterPrint.pdf";
//水印内容:公司、日期、内容
String company="**科技有限公司";
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String waterMarkDate=sdf.format(date);
String content="受控文件";
// 将文件先加水印然后输出
setWatermark(outFile,inputFile,company,waterMarkDate,content);
}
public static void setWatermark(String outFile, String inputFile,String company,
String waterMarkDate,String content) throws DocumentException,IOException {
PdfReader reader = new PdfReader(inputFile);//获取需要加水印的输入文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(outFile)));
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages();//获取输入文件页数
for (int i = 1; i <= total; i++) {
PdfContentByte contentOver = stamper.getOverContent(i);//在内容上方加水印
/*添加图片水印*/
String imgUrl="C:/Users/solexit06/Desktop/Border.png";
Image img = Image.getInstance(imgUrl);//获取要作为水印的图片
img.setAbsolutePosition(200, 450);//图片距离文档的左下边距
img.scaleToFit(150, 240);//图片水印的大小
img.setRotationDegrees(10);//旋转
contentOver.addImage(img);//添加图片水印
/*添加文字水印*/
contentOver.beginText();
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED);//字体实例,影响中文字体显示
contentOver.setColorFill(Color.RED);//水印颜色
contentOver.setFontAndSize(bf, 16);//字体样式和大小
contentOver.showTextAligned(Element.ALIGN_LEFT, company, 215, 520,10);//左边距、下边距
contentOver.setFontAndSize(bf, 10);//字体样式和大小
contentOver.showTextAligned(Element.ALIGN_LEFT, waterMarkDate, 260, 505,10);//左边距、下边距
contentOver.setFontAndSize(bf, 24);//字体样式和大小
contentOver.showTextAligned(Element.ALIGN_LEFT, content, 240, 465,10);//左边距、下边距
contentOver.endText();
}
stamper.close();
System.out.println("添加水印成功");
}
}
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://blog.csdn.net/beinlife/article/details/53514440
边栏推荐
猜你喜欢
Qt利用QtXlsx操作excel文件
2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
MySQL数据库中delete、truncate、drop原理详解
dried food! Point based: differentiable Poisson solver
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
搜一下导航完整程序源码
总线结构概述
Rotation function of leetcode medium problem
vslam PPT
随机推荐
Rotation function of leetcode medium problem
C语言学习记录——삼십팔 字符串函数使用和剖析(2)
mysql查询字符串类型的字段使用数字类型查询时问题
npm安装yarn
LeetCode简单题之重新排列日志文件
vslam PPT
Campus transfer second-hand market source code download
数论求a^b(a,b为1e12级别)的因子之和
js常用数组方法
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
网赚APP资源下载类网站源码
Compiling principle questions - with answers
synchronized 实现原理
idea:使用easyYapi插件导出yapi接口
【学习】从零开始的音视频开发(9)——NuPlayer
Qt读写XML文件
Compiler des questions de principe - avec des réponses
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download