当前位置:网站首页>导出带水印的PDF
导出带水印的PDF
2022-06-23 03:55:00 【力不竭!!!战不止!!!】
背景
有时候我们需要导出单个pdf数据,上面需要加上一些水印或者图片
导出样图

maven依赖
<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>
测试demo
public class PDFCellUtils {
/** * 创建单元格(指定字体) * @param value 单元格展示值文字 * @param 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;
}
/** * 创建单元格(指定字体、水平对齐方式) * @param value 值 * @param font 字体 * @param align 水平对齐方式 * @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;
}
/** * 创建单元格(指定字体、水平居..、单元格跨x列合并) * @param value 值 * @param font 字体 * @param align 水平对齐方式 * @param colspan 单元格跨x合并 * @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;
}
/** * 创建单元格(指定字体、水平对齐方式、单元格跨x列合并, 单元格跨y行合并) * @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;
}
/** * 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距) * @param value 值 * @param font 字体 * @param align 水平对齐方式 * @param colspan 单元格跨x列合并 * @param boderFlag 设置单元格内边距 * @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;
}
/** * 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距) * @param value 值 * @param font 子图 * @param align 水平对齐方式 * @param borderWidth 边框宽度:0表示无边框 * @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 {
// 标题字体
private static Font title_font;
private static Font text_font;
// 正文加粗字体
private static Font bold_text_Font;
// 缩进符
private static String TABLE = "\t";
// 最大宽度
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("付款单: PDF字体初始化异常. 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 {
// 标题
Paragraph title = new Paragraph("xxx审批单", title_font);
//设置文字居中 (0靠左/1居中/2靠右)
title.setAlignment(1);
title.setSpacingBefore(80f); //设置段落上空白
title.setSpacingAfter(10f); //设置段落下空白
document.add(title);
// 表格 默认7列
PdfPTable table = createTable(8, Element.ALIGN_CENTER);
table.addCell(PDFCellUtils.createCell("xxx部门", text_font, Element.ALIGN_LEFT, 4, false));
table.addCell(PDFCellUtils.createCell("申请时间:2022年4月28日", text_font, Element.ALIGN_RIGHT, 4, false));
table.addCell(PDFCellUtils.createCell("审批编码", bold_text_Font, Element.ALIGN_CENTER, 2, 1));
table.addCell(PDFCellUtils.createCell("CG3378612964", text_font, Element.ALIGN_CENTER, 1));
table.addCell(PDFCellUtils.createCell("编号", bold_text_Font));
table.addCell(PDFCellUtils.createCell("BM1890", text_font));
table.addCell(PDFCellUtils.createCell("申请人", bold_text_Font, Element.ALIGN_CENTER, 2));
table.addCell(PDFCellUtils.createCell("吴彦祖", text_font, Element.ALIGN_LEFT, 6));
table.addCell(PDFCellUtils.createCell("申请人部门", bold_text_Font, Element.ALIGN_CENTER, 2));
table.addCell(PDFCellUtils.createCell("xx部门", text_font, Element.ALIGN_LEFT, 6));
table.addCell(PDFCellUtils.createCell("款项", bold_text_Font, Element.ALIGN_CENTER, 2));
table.addCell(PDFCellUtils.createCell("云服务费用", text_font, Element.ALIGN_LEFT, 6));
table.addCell(PDFCellUtils.createCell("明细", bold_text_Font, Element.ALIGN_CENTER, 2, 2));
table.addCell(PDFCellUtils.createCell("事由", text_font));
table.addCell(PDFCellUtils.createCell("rds", text_font));
table.addCell(PDFCellUtils.createCell("金额(元)", bold_text_Font));
table.addCell(PDFCellUtils.createCell("99999", text_font));
table.addCell(PDFCellUtils.createCell("备注", bold_text_Font));
table.addCell(PDFCellUtils.createCell("B0321", text_font));
table.addCell(PDFCellUtils.createCell("事由", bold_text_Font));
table.addCell(PDFCellUtils.createCell("云服务器ECS", text_font));
table.addCell(PDFCellUtils.createCell("金额(元)", bold_text_Font));
table.addCell(PDFCellUtils.createCell("99999", text_font));
table.addCell(PDFCellUtils.createCell("备注", bold_text_Font));
table.addCell(PDFCellUtils.createCell("B0322", text_font));
byte[] bFile = Files.readAllBytes(new File(imgPath_native).toPath());
//本地文件方式 ,也可以直接使用图片url
Image image = Image.getInstance(bFile);
image.setAlignment(Image.ALIGN_CENTER);
image.scalePercent(10); //依照比例缩放
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();
}
}
/** * 创建默认列宽,指定列数、水平(居中、右、左)的表格 * @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;
}
}
图片

边栏推荐
- MySQL stored procedure
- Zygote进程
- PRCS-1016 : Failed to resolve Single Client Access Name
- 气象绘图软件Panoply使用教程 (不定时更新)
- Hcip switch experiment
- 应用挂了~
- 微信小程序:凑单满减计算神器
- MVC three-tier architecture
- 985 test engineer is hanged. Who is more important in terms of education and experience?
- ② Cocoapods principle and podspec file uploading operation
猜你喜欢
随机推荐
Snippet Manager snippetslab
[MAC] there is no source option in security and privacy
OSPF shunt test
使用PX4的ECL进行多传感器数据融合的后处理
Introduction to s file generated by TEQC for GNSS data quality analysis
功能测试人员如何做到花一个月的时间进阶自动化软件测试工程师
BGP第二次试验
经济发展由新技术推动着来
UI automation positioning edge -xpath actual combat
Raspberry pie network remote access
Fund performance evaluation
618如何冲出重围?海尔智家:做好用户的数字化
Laravel8 implementation of picture verification code
【微服务|Nacos】Nacos版本相关问题一览
This markdown artifact will be charged!
When SBAS encounters rtklib
UI自动化定位利器-xpath实战
JSP入门级笔记
Investment risk management
Introduction and use of precise ephemeris








