有时候我们需要导出单个pdf数据,上面需要加上一些水印或者图片
导出样图 maven依赖测试democom.itextpdf itextpdf5.5.13 com.itextpdf itext-asian5.2.0
public class PDFCellUtils {
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;
}
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;
}
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;
}
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;
}
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;
}
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();
}
}
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;
}
}
图片



