当前位置:网站首页>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
边栏推荐
猜你喜欢

Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi

总线结构概述

Input / output system

The third divisor of leetcode simple question

vslam PPT

关于ORB——SLAM运行中关键帧位置越来越近的异常说明

Failed to convert a NumPy array to a Tensor(Unsupported Object type int)

Knowledge points and problem solutions related to information collection

synchronized 实现原理

Somme numérique de la chaîne de calcul pour un problème simple de leetcode
随机推荐
The third divisor of leetcode simple question
二维01背包
396. Rotate Function
匿名类型(C# 指南 基础知识)
Overview of bus structure
Install MySQL for Ubuntu and query the average score
vslam PPT
How to encrypt devices under the interconnection of all things
LeetCode中等题之旋转函数
让地球少些“碳”息 度能在路上
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
Online yaml to XML tool
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
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
线程的调度(优先级)
Using qlst excel file
Shell script advanced
ATSS(CVPR2020)
vslam PPT
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果