当前位置:网站首页>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
边栏推荐
猜你喜欢
Distributed service governance Nacos
The simple problem of leetcode is to calculate the numerical sum of strings
Using qlst excel file
[learning] audio and video development from scratch (9) -- nuplayer
stm32以及freertos 堆栈解析
Talk about the basic but not simple stock data
mysql查询字符串类型的字段使用数字类型查询时问题
Flink SQL实现流批一体
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
随机推荐
The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
使用JWT生成与解析Token
Brief description of CPU
将实例化对象的方法 给新的对象用
Situational leaders - Chapter 7, solving performance problems
一键清理项目下pycharm和Jupyter缓存文件
Online app resource download website source code
ASAN 极简原理
編譯原理題-帶答案
Multi vision slam
JS converts tree structure data into one-dimensional array data
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
分组背包呀
如何保护开源项目免遭供应链攻击-安全设计(1)
二维01背包
数论求a^b(a,b为1e12级别)的因子之和
Distributed service governance Nacos
QFileDialog 选择多个文件或文件夹
There are some problems when using numeric type to query string type fields in MySQL
Vowel substring in statistical string of leetcode simple problem