当前位置:网站首页>Export PDF with watermark
Export PDF with watermark
2022-06-23 05:32:00 【Endless!!! More than one battle!!!】
background
Sometimes we need to export a single pdf data , You need to add some watermarks or pictures on it
Export sample

maven rely on
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
test demo
public class PDFCellUtils {
/** * Create cells ( Specified font ) * @param value Cells display value text * @param font Text font * @return */
public static PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(6.0f);
return cell;
}
/** * Create cells ( Specified font 、 Horizontal alignment ) * @param value value * @param font typeface * @param align Horizontal alignment * @return */
public static PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(6.0f);
return cell;
}
/** * Create cells ( Specified font 、 Horizontal residence ..、 Cells span x columns ) * @param value value * @param font typeface * @param align Horizontal alignment * @param colspan Cells span x Merge * @return */
public static PdfPCell createCell(String value, Font font, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(6.0f);
return cell;
}
/** * Create cells ( Specified font 、 Horizontal alignment 、 Cells span x columns , Cells span y merger ) * @param value * @param font * @param align * @param colspan * @param rowspan * @return */
public static PdfPCell createCell(String value, Font font, int align, int colspan, int rowspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(6.0f);
return cell;
}
/** * Create cells ( Specified font 、 Horizontal residence ..、 Cells span x columns 、 Set the margin inside the cell ) * @param value value * @param font typeface * @param align Horizontal alignment * @param colspan Cells span x columns * @param boderFlag Set the margin inside the cell * @return */
public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(6.0f);
if (!boderFlag) {
cell.setBorder(0);
} else if (boderFlag) {
cell.setBorder(0);
}
return cell;
}
/** * Create cells ( Specified font 、 level ..、 Border width :0 No border 、 padding ) * @param value value * @param font Subgraphs * @param align Horizontal alignment * @param borderWidth Border width :0 No border * @param paddingSize * @param flag * @return */
public static PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
cell.setBorderWidthLeft(borderWidth[0]);
cell.setBorderWidthRight(borderWidth[1]);
cell.setBorderWidthTop(borderWidth[2]);
cell.setBorderWidthBottom(borderWidth[3]);
cell.setPaddingTop(paddingSize[0]);
cell.setPaddingBottom(paddingSize[1]);
if (flag) {
cell.setColspan(2);
}
return cell;
}
}
public class PdfText {
// Title font
private static Font title_font;
private static Font text_font;
// Bold text
private static Font bold_text_Font;
// Indent
private static String TABLE = "\t";
// Maximum width
private static int maxWidth = 520;
static {
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
title_font = new Font(bfChinese, 26, Font.BOLD);
text_font = new Font(bfChinese, 10, Font.NORMAL);
bold_text_Font = new Font(bfChinese, 10, Font.BOLD);
} catch (Exception e) {
log.error(" Payment note : PDF Font initialization exception . STSong-Light, UniGB-UCS2-H", e);
}
}
public static final String imgPath_native = "/Users/dasouche/IdeaProjects/test/image/test2.png";
public static void generatePDF(Document document) throws Exception {
// title
Paragraph title = new Paragraph("xxx Approval form ", title_font);
// Center text (0 Keep to the left /1 In the middle /2 Keep right )
title.setAlignment(1);
title.setSpacingBefore(80f); // Set the space on the paragraph
title.setSpacingAfter(10f); // Set the space below the paragraph
document.add(title);
// form Default 7 Column
PdfPTable table = createTable(8, Element.ALIGN_CENTER);
table.addCell(PDFCellUtils.createCell("xxx department ", text_font, Element.ALIGN_LEFT, 4, false));
table.addCell(PDFCellUtils.createCell(" Application time :2022 year 4 month 28 Japan ", text_font, Element.ALIGN_RIGHT, 4, false));
table.addCell(PDFCellUtils.createCell(" Approval code ", bold_text_Font, Element.ALIGN_CENTER, 2, 1));
table.addCell(PDFCellUtils.createCell("CG3378612964", text_font, Element.ALIGN_CENTER, 1));
table.addCell(PDFCellUtils.createCell(" Number ", bold_text_Font));
table.addCell(PDFCellUtils.createCell("BM1890", text_font));
table.addCell(PDFCellUtils.createCell(" Applicant ", bold_text_Font, Element.ALIGN_CENTER, 2));
table.addCell(PDFCellUtils.createCell(" Wu Yanzu ", text_font, Element.ALIGN_LEFT, 6));
table.addCell(PDFCellUtils.createCell(" Applicant department ", bold_text_Font, Element.ALIGN_CENTER, 2));
table.addCell(PDFCellUtils.createCell("xx department ", text_font, Element.ALIGN_LEFT, 6));
table.addCell(PDFCellUtils.createCell(" Money ", bold_text_Font, Element.ALIGN_CENTER, 2));
table.addCell(PDFCellUtils.createCell(" Cloud service fees ", text_font, Element.ALIGN_LEFT, 6));
table.addCell(PDFCellUtils.createCell(" Detailed ", bold_text_Font, Element.ALIGN_CENTER, 2, 2));
table.addCell(PDFCellUtils.createCell(" Subject matter ", text_font));
table.addCell(PDFCellUtils.createCell("rds", text_font));
table.addCell(PDFCellUtils.createCell(" amount of money ( element )", bold_text_Font));
table.addCell(PDFCellUtils.createCell("99999", text_font));
table.addCell(PDFCellUtils.createCell(" remarks ", bold_text_Font));
table.addCell(PDFCellUtils.createCell("B0321", text_font));
table.addCell(PDFCellUtils.createCell(" Subject matter ", bold_text_Font));
table.addCell(PDFCellUtils.createCell(" Cloud server ECS", text_font));
table.addCell(PDFCellUtils.createCell(" amount of money ( element )", bold_text_Font));
table.addCell(PDFCellUtils.createCell("99999", text_font));
table.addCell(PDFCellUtils.createCell(" remarks ", bold_text_Font));
table.addCell(PDFCellUtils.createCell("B0322", text_font));
byte[] bFile = Files.readAllBytes(new File(imgPath_native).toPath());
// Local file mode , You can also use pictures directly url
Image image = Image.getInstance(bFile);
image.setAlignment(Image.ALIGN_CENTER);
image.scalePercent(10); // Scale to scale
image.setAbsolutePosition(400f, 720f);
document.add(image);
document.add(table);
}
public static void main(String[] args) {
try {
Document document = new Document();
File fileDir = new File("./pdf");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File("./pdf/PDFDemo.pdf");
try {
file.createNewFile();
} catch (IOException e) {
return;
}
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
generatePDF(document);
document.close();
System.out.println(file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
/** * Create default column width , Specify the number of columns 、 level ( In the middle 、 Right 、 Left ) Table for * @param colNumber * @param align * @return */
public static PdfPTable createTable(int colNumber, int align) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(align);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
}
picture

边栏推荐
- 数学分析_笔记_第1章:集合与映射
- Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling
- Introduction and use of precise ephemeris
- 云原生大趋势下的容器化技术现状与发展
- JVM原理之内存模型
- B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)
- MCS:离散随机变量——Uniform分布
- 云原生数据库是未来数据库的天下
- Mysql入门学习(一)之语法
- 电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
猜你喜欢

QT QWidget nesting relative position acquisition (QT drawing nesting)

JDBC入门学习(三)之事务回滚功能的实现

Sift特征点提取

Jenkins安装部署以及自动构建和发布jar应用

VMware network connection error unit network service not found

After the idea code is developed, the code is submitted. If the branch is found to be incorrect after submission, how can I withdraw it

Xa mode explanation and code implementation of four Seata modes

如何进行探索性数据分析

Seata四大模式之XA模式详解及代码实现

JDBC introductory learning (II) encapsulation tool class
随机推荐
导出带水印的PDF
App automated test appium advanced
【opencv450】 图像相减、二值化、阈值分割
Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling
抽奖 ddd 代码
The weak are as irritable as tigers, the strong are as calm as water, and the really powerful have already given up their emotions
Mathematical analysis_ Notes_ Chapter 1: set and mapping
JDBC introductory learning (II) encapsulation tool class
Go language - use of packages
markdown给图片加背景色
Get bat command results in bat
数学分析_笔记_第1章:集合与映射
GO语言-包的使用
弱者易怒如虎,强者平静如水,真正厉害的人早已戒掉了情绪
Sift特征点提取
H5 adaptive full screen
奇门遁甲辅助决策软件
The tiobe programming language ranking is an indicator of the popular trend of programming languages
关于信息泄露和防御
read 文件一个字节实际会发生多大的磁盘IO?