当前位置:网站首页>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

边栏推荐
- Ams:startactivity desktop launch application
- Penetration test basis | attached test points and test scenarios
- Go 分组 & 排序
- MCS:离散随机变量——Uniform分布
- Cloud native database is the world of future databases
- Web 应用程序安全测试指南
- Konva series tutorial 1:what is konva?
- 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
- JDBC入门学习(二)之封装工具类
- CF【1700D】D. River Locks(dp、二分、数学)
猜你喜欢
随机推荐
H5 adaptive full screen
Onnxoptimizer, onnxsim usage records
When SBAS encounters rtklib
(IntelliJ) plug in background image plus
云原生架构(04)-CNCF
电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
JVM原理之内存模型
弱者易怒如虎,强者平静如水,真正厉害的人早已戒掉了情绪
What do Niu B programmers pay attention to when "creating an index"?
物联网开源开发平台 Shifu 开放内测!第一版技术文档发布
pkav简单爆破
WebRTC[47] - WebRTC 保存 YUV 数据的常用方式
Zygote process
Stm32cube serial port uses dma+idle to receive variable length data
What does it mean to open more accounts? Why open more accounts? How to implement it safely?
hash---------history
Implementation of MySQL custom sequence number
IDEA 代码开发完毕后,提交代码,提交后发现分支不对,怎么撤回
Mysql入门学习(二)之子查询+关联
JDBC入门学习(二)之封装工具类









