当前位置:网站首页>PDF with watermark
PDF with watermark
2022-04-23 08:26:00 【beinlife】
pdf Add watermark
relevant jar package
- itext-2.1.7.jar
- iTextAsian.jar
The code is as follows
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;
/** * to PDF Add watermark * @author solexit06 * */
public class TestWaterPdf {
public static void main(String[] args) throws DocumentException,IOException {
// The file to output
String outFile="C:/Users/solexit06/Desktop/WaterPrint.pdf";
// The original file to be watermarked
String inputFile="C:/Users/solexit06/Desktop/testWaterPrint.pdf";
// Watermark content : company 、 date 、 Content
String company="** Technology Co., Ltd ";
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String waterMarkDate=sdf.format(date);
String content=" Controlled documents ";
// Watermark the file first and then output
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);// Get the input file that needs to be watermarked
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(outFile)));
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages();// Get the number of pages of the input file
for (int i = 1; i <= total; i++) {
PdfContentByte contentOver = stamper.getOverContent(i);// Watermark the content
/* Add image watermarking */
String imgUrl="C:/Users/solexit06/Desktop/Border.png";
Image img = Image.getInstance(imgUrl);// Get the picture to be used as a watermark
img.setAbsolutePosition(200, 450);// The image is at the bottom left margin of the document
img.scaleToFit(150, 240);// The size of the image watermark
img.setRotationDegrees(10);// rotate
contentOver.addImage(img);// Add image watermarking
/* Add text watermark */
contentOver.beginText();
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED);// Font instance , Affect Chinese font display
contentOver.setColorFill(Color.RED);// Watermark color
contentOver.setFontAndSize(bf, 16);// Font style and size
contentOver.showTextAligned(Element.ALIGN_LEFT, company, 215, 520,10);// The left margin 、 Bottom margin
contentOver.setFontAndSize(bf, 10);// Font style and size
contentOver.showTextAligned(Element.ALIGN_LEFT, waterMarkDate, 260, 505,10);// The left margin 、 Bottom margin
contentOver.setFontAndSize(bf, 24);// Font style and size
contentOver.showTextAligned(Element.ALIGN_LEFT, content, 240, 465,10);// The left margin 、 Bottom margin
contentOver.endText();
}
stamper.close();
System.out.println(" Watermark added successfully ");
}
}
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230734168694.html
边栏推荐
- 【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
- QT compilation qtxlsx Library
- One click cleanup of pycharm and jupyter cache files under the project
- SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
- QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
- An article understands variable lifting
- 怎么读书读论文
- Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
- How to encrypt devices under the interconnection of all things
- ATSS(CVPR2020)
猜你喜欢
如何保护开源项目免遭供应链攻击-安全设计(1)
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
pgsql想实现mysql一样样的列子查询操作
[C语言] 文件操作《一》
怎么读书读论文
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
监控智能回放是什么,如何使用智能回放查询录像
RPC过程
My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
Qt读写XML文件
随机推荐
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
常用正则表达式
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
谈谈那些基础但不简单的股票数据
My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
form表单 post提交 数据量大的问题
作文以记之 ~ 二叉树的后序遍历
AAAI 2022 recruit speakers!!
An article understands variable lifting
Knowledge points and problem solutions related to information collection
How to generate assembly file
CGM优化血糖监测管理——移宇科技亮相四川省国际医学交流促进会
How to encrypt devices under the interconnection of all things
【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
分布式消息中间件框架选型-数字化架构设计(7)
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
vslam PPT
Talk about the basic but not simple stock data