当前位置:网站首页>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
边栏推荐
- LeetCode简单题之重新排列日志文件
- 队列(c语言/链表)
- Misunderstanding of flush () method of OutputStream class
- Qt编译QtXlsx库
- 让地球少些“碳”息 度能在路上
- 数论求a^b(a,b为1e12级别)的因子之和
- ELK生产实践
- Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
- Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
- SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
猜你喜欢

Kubernetes in browser and IDE | interactive learning platform killercoda

通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解

Campus transfer second-hand market source code download

Shell脚本进阶

QT compilation qtxlsx Library

CSV column extract column extraction

2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry

Install MySQL for Ubuntu and query the average score
![[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel](/img/7c/0adc0940b6d5c8a61d34bfa5f66ee7.png)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel

Qt利用QtXlsx操作excel文件
随机推荐
vslam PPT
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
记录:js删除数组中某一项或几项的几种方法
AAAI 2022 recruit speakers!!
Using qlst excel file
LeetCode-199-二叉树的右视图
Let the earth have less "carbon" and rest on the road
对OutputStream类的flush()方法的误解
LeetCode簡單題之計算字符串的數字和
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
Redis master-slave server problem
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
Input / output system
英语课小记(四)
Use of applicationreadyevent
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
Qt读写XML文件
An article understands variable lifting
396. Rotate Function