当前位置:网站首页>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
边栏推荐
- LeetCode简单题之计算字符串的数字和
- An article understands variable lifting
- 网赚APP资源下载类网站源码
- An idea plug-in that doesn't work, but can install X
- dmp引擎工作总结(2021,光剑)
- How to read books and papers
- 2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
- Manipulator motion planning in 3C assembly
- Jetson Xavier NX (3) bazel mediapipe installation
- Penetration test interview collection -- HVV---
猜你喜欢

校园转转二手市场源码下载

MySQL数据库中delete、truncate、drop原理详解

Qt读写XML文件

Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code

LeetCode簡單題之計算字符串的數字和

An article understands variable lifting

ELK生产实践

Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American

Vowel substring in statistical string of leetcode simple problem

ASAN 极简原理
随机推荐
怎么读书读论文
【路科V0】验证环境2——验证环境组件
Sword finger offer Day24 math (medium)
LeetCode中等题之旋转函数
Data deletion and modification (MySQL)
室内定位技术对比
Samsung, March to the west again
单点登录 SSO
Usage of databinding
作文以记之 ~ 二叉树的前序遍历
Common regular expressions
396. Rotate Function
C language learning record -- use and analysis of string function (2)
User manual of Chinese version of solidity ide Remix
Idea: export Yapi interface using easyyapi plug-in
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
There are some problems when using numeric type to query string type fields in MySQL
Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American