java生成pdf itext-Document代码生成 附图
程序员文章站
2022-07-10 20:59:09
因公司业务需要需要java画pdf作报价单踩了一下午坑总算搞出来个能看的秉承码农共同进步(代码互抄)的原则 在这边分享下因为是测试类 没写太多注释和注意规范附生成好的切图一张 以供参考pom com.itextpdf itextpdf
因公司业务需要
需要java画pdf作报价单
踩了一下午坑总算搞出来个能看的
秉承码农共同进步(代码互抄)的原则 在这边分享下
因为是测试类 没写太多注释和注意规范
附
生成好的切图一张 以供参考
pom
<!--生成pdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<!--中文字体-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--html xml 转为pdf-->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
public static void main(String[] args) {
try {
File PDFFilePath = new File( "D:\\1.pdf");
if (PDFFilePath.exists()) {
PDFFilePath.delete();
}
PDFFilePath.createNewFile();
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Integer discountId =1;
Font fonttitle = new Font(bfChinese, 18, Font.BOLD, BaseColor.GRAY);
Font bottom = new Font(bfChinese, 12, Font.BOLD, BaseColor.BLACK);
Font fontbold = new Font(bfChinese, 10, Font.BOLD, BaseColor.BLACK);
Font discount=new Font(bfChinese, 15, Font.BOLD, BaseColor.RED);
Font flicking = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);
ExportTemplateCreateTag(PDFFilePath, fonttitle, fontbold, flicking,bottom,discount);
// String strResult = PDFToBase64(PDFFilePath);
// strResult = strResult.replaceAll("\r\n", "");
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void ExportTemplateCreateTag(File PDFFilePath, Font fontTitle, Font fontbold, Font flicking, Font bottom,Font discount) {
try {
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDFFilePath.getPath()));
document.open();
PdfPTable columnKeyTable = new PdfPTable(1);
columnKeyTable.setWidthPercentage(97);
columnKeyTable.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell orderMain1Box = new PdfPCell();
orderMain1Box.setBorder(0);
orderMain1Box.setFixedHeight(70);
orderMain1Box.setPaddingTop(20);
Paragraph orderRemark = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", fontTitle);
orderRemark.setAlignment(Element.ALIGN_CENTER);
orderMain1Box.addElement(orderRemark);
columnKeyTable.addCell(orderMain1Box);
//详细信息字段
PdfPCell orderMain2Box = new PdfPCell();
orderMain2Box.setBorder(0);
orderMain2Box.setPaddingBottom(5);
orderMain2Box.setPadding(0);
//四列 宽度百分比100
PdfPTable detailColumnTable = new PdfPTable(4);
detailColumnTable.setWidthPercentage(100);
PdfPCell column3box = new PdfPCell();
column3box.setFixedHeight(30);
column3box.setBorderWidth(0.5f);
column3box.setBorderColor(BaseColor.GRAY);
// column3box.setBackgroundColor(BaseColor.BLUE);
Paragraph column3key = new Paragraph("versionName", flicking);
column3key.setAlignment(Element.ALIGN_CENTER);
column3box.addElement(column3key);
detailColumnTable.addCell(column3box);
PdfPCell column4box = new PdfPCell();
column4box.setFixedHeight(30);
column4box.setBorderWidth(0.5f);
column4box.setBorderColor(BaseColor.GRAY);
// column4box.setBackgroundColor(BaseColor.BLUE);
Paragraph column4key = new Paragraph("xxx", flicking);
column4key.setAlignment(Element.ALIGN_CENTER);
column4box.addElement(column4key);
detailColumnTable.addCell(column4box);
PdfPCell column5box = new PdfPCell();
column5box.setFixedHeight(30);
column5box.setBorderWidth(0.5f);
column5box.setBorderColor(BaseColor.GRAY);
// column5box.setBackgroundColor(BaseColor.BLUE);
Paragraph column5key = new Paragraph("xxxx", flicking);
column5key.setAlignment(Element.ALIGN_CENTER);
column5box.addElement(column5key);
detailColumnTable.addCell(column5box);
PdfPCell column6box = new PdfPCell();
column6box.setFixedHeight(30);
column6box.setBorderWidth(0.5f);
column6box.setBorderColor(BaseColor.GRAY);
// column6box.setBackgroundColor(BaseColor.BLUE);
Paragraph column6key = new Paragraph("xxx", flicking);
column6key.setAlignment(Element.ALIGN_CENTER);
column6box.addElement(column6key);
detailColumnTable.addCell(column6box);
orderMain2Box.addElement(detailColumnTable);
columnKeyTable.addCell(orderMain2Box);
document.add(columnKeyTable);
//测试数据 使用后删除
List<Map<String,Object>> list = new ArrayList<>();
HashMap<String, Object> map1 = new HashMap<>();
map1.put("name","版本一");
map1.put("size","1");
map1.put("unit","人");
list.add(map1);
HashMap<String, Object> map2 = new HashMap<>();
map2.put("name","版本二");
map2.put("size","2");
map2.put("unit","人");
list.add(map2);
HashMap<String, Object> map3 = new HashMap<>();
map3.put("name","版本三");
map3.put("size","3");
map3.put("unit","人");
list.add(map3);
PdfPTable text = new PdfPTable(4);
text.setWidthPercentage(97);
PdfPCell textName = new PdfPCell();
textName.setBorderWidth(0.5f);
textName.setFixedHeight(20*list.size());
textName.setBorderColor(BaseColor.GRAY);
textName.setPaddingRight(15);
Paragraph textSize = new Paragraph("xxx",fontbold);
//水平垂直且居中
textName.setUseAscender(true);
textName.setVerticalAlignment(textName.ALIGN_MIDDLE);
textName.setHorizontalAlignment(Element.ALIGN_CENTER);
textName.setVerticalAlignment(Element.ALIGN_MIDDLE);
textSize.setAlignment(Element.ALIGN_RIGHT);
textName.addElement(textSize);
text.addCell(textName);
PdfPTable headerTable1 =new PdfPTable(1);
for (int i =0;i<list.size();i++) {
Map<String, Object> map = list.get(i);
PdfPCell textName1 = new PdfPCell();
textName1.setBorderWidth(0.5f);
textName1.setFixedHeight(20);
textName1.setBorderColor(BaseColor.GRAY);
textName1.setPaddingRight(15);
Paragraph textSize1 = new Paragraph(map.get("name").toString(), fontbold);
textSize1.setAlignment(Element.ALIGN_RIGHT);
//水平垂直且居中
textName1.setUseAscender(true);
textName1.setVerticalAlignment(textName1.ALIGN_MIDDLE);
textName1.setHorizontalAlignment(Element.ALIGN_CENTER);
textName1.setVerticalAlignment(Element.ALIGN_MIDDLE);
textName1.addElement(textSize1);
headerTable1.addCell(textName1);
}
PdfPTable headerTable2 =new PdfPTable(1);
for (int q =0;q<list.size();q++) {
Map<String, Object> map = list.get(q);
PdfPCell textName2 = new PdfPCell();
textName2.setBorderWidth(0.5f);
textName2.setFixedHeight(20);
textName2.setBorderColor(BaseColor.GRAY);
// textName2.setColspan(3);
textName2.setPaddingRight(15);
//水平垂直且居中
textName2.setUseAscender(true);
textName2.setVerticalAlignment(textName2.ALIGN_MIDDLE);
textName2.setHorizontalAlignment(Element.ALIGN_CENTER);
textName2.setVerticalAlignment(Element.ALIGN_MIDDLE);
Paragraph textSize2 = new Paragraph(map.get("size").toString(), fontbold);
textSize2.setAlignment(Element.ALIGN_RIGHT);
textName2.addElement(textSize2);
headerTable2.addCell(textName2);
}
PdfPTable headerTable3 =new PdfPTable(1);
for (int j =0;j<list.size();j++) {
Map<String, Object> map = list.get(j);
PdfPCell textName3 = new PdfPCell();
textName3.setBorderWidth(0.5f);
textName3.setFixedHeight(20);
textName3.setBorderColor(BaseColor.GRAY);
//水平垂直且居中
textName3.setUseAscender(true);
textName3.setVerticalAlignment(textName3.ALIGN_MIDDLE);
textName3.setHorizontalAlignment(Element.ALIGN_CENTER);
textName3.setVerticalAlignment(Element.ALIGN_MIDDLE);
// textName3.setColspan(4);
textName3.setPaddingRight(15);
Paragraph textSize3 = new Paragraph(map.get("unit").toString(),fontbold);
textSize3.setAlignment(Element.ALIGN_RIGHT);
textName3.addElement(textSize3);
headerTable3.addCell(textName3);
}
text.addCell(headerTable1);
text.addCell(headerTable2);
text.addCell(headerTable3);
document.add(text);
PdfPTable apolegamyModule = new PdfPTable(4);
apolegamyModule.setWidthPercentage(97);
if(list.size()>1&&list.size()!=0){
PdfPTable headerTable11 =new PdfPTable(1);
PdfPCell apolegamy5 = new PdfPCell();
apolegamy5.setBorderWidth(0.5f);
apolegamy5.setFixedHeight(20*list.size());
apolegamy5.setBorderColor(BaseColor.GRAY);
apolegamy5.setPaddingRight(15);
Paragraph value11 = new Paragraph("xxxxxx", fontbold);
value11.setAlignment(Element.ALIGN_RIGHT);
//水平垂直且居中
apolegamy5.setUseAscender(true);
apolegamy5.setVerticalAlignment(apolegamy5.ALIGN_MIDDLE);
apolegamy5.setHorizontalAlignment(Element.ALIGN_CENTER);
apolegamy5.setVerticalAlignment(Element.ALIGN_MIDDLE);
apolegamy5.addElement(value11);
headerTable11.addCell(apolegamy5);
apolegamyModule.addCell(headerTable11);
PdfPTable headerTable6 =new PdfPTable(1);
for (int i =0;i<list.size();i++) {
Map<String, Object> map = list.get(i);
PdfPCell apolegamy1 = new PdfPCell();
apolegamy1.setBorderWidth(0.5f);
apolegamy1.setFixedHeight(20);
apolegamy1.setBorderColor(BaseColor.GRAY);
apolegamy1.setPaddingRight(15);
Paragraph value1 = new Paragraph(map.get("name").toString(), fontbold);
value1.setAlignment(Element.ALIGN_RIGHT);
//水平垂直且居中
apolegamy1.setUseAscender(true);
apolegamy1.setVerticalAlignment(apolegamy1.ALIGN_MIDDLE);
apolegamy1.setHorizontalAlignment(Element.ALIGN_CENTER);
apolegamy1.setVerticalAlignment(Element.ALIGN_MIDDLE);
apolegamy1.addElement(value1);
headerTable6.addCell(apolegamy1);
}
PdfPTable headerTable7 =new PdfPTable(1);
for (int q =0;q<list.size();q++) {
Map<String, Object> map = list.get(q);
PdfPCell apolegamy2 = new PdfPCell();
apolegamy2.setBorderWidth(0.5f);
apolegamy2.setFixedHeight(20);
apolegamy2.setBorderColor(BaseColor.GRAY);
// textName2.setColspan(3);
apolegamy2.setPaddingRight(15);
//水平垂直且居中
apolegamy2.setUseAscender(true);
apolegamy2.setVerticalAlignment(apolegamy2.ALIGN_MIDDLE);
apolegamy2.setHorizontalAlignment(Element.ALIGN_CENTER);
apolegamy2.setVerticalAlignment(Element.ALIGN_MIDDLE);
Paragraph value2 = new Paragraph(map.get("size").toString(), fontbold);
value2.setAlignment(Element.ALIGN_RIGHT);
apolegamy2.addElement(value2);
headerTable7.addCell(apolegamy2);
}
PdfPTable headerTable8 =new PdfPTable(1);
for (int j =0;j<list.size();j++) {
Map<String, Object> map = list.get(j);
PdfPCell apolegamy3 = new PdfPCell();
apolegamy3.setBorderWidth(0.5f);
apolegamy3.setFixedHeight(20);
apolegamy3.setBorderColor(BaseColor.GRAY);
//水平垂直且居中
apolegamy3.setUseAscender(true);
apolegamy3.setVerticalAlignment(apolegamy3.ALIGN_MIDDLE);
apolegamy3.setHorizontalAlignment(Element.ALIGN_CENTER);
apolegamy3.setVerticalAlignment(Element.ALIGN_MIDDLE);
// textName3.setColspan(4);
apolegamy3.setPaddingRight(15);
Paragraph value3 = new Paragraph(map.get("unit").toString(),fontbold);
value3.setAlignment(Element.ALIGN_RIGHT);
apolegamy3.addElement(value3);
headerTable8.addCell(apolegamy3);
}
apolegamyModule.addCell(headerTable6);
apolegamyModule.addCell(headerTable7);
apolegamyModule.addCell(headerTable8);
document.add(apolegamyModule);
}
PdfPTable serverItemMoneyTable = new PdfPTable(4);
serverItemMoneyTable.setWidthPercentage(97);
PdfPCell columntBox = new PdfPCell();
columntBox.setBorderWidth(0.5f);
columntBox.setFixedHeight(20);
columntBox.setBorderColor(BaseColor.GRAY);
columntBox.setColspan(4);
columntBox.setPaddingRight(15);
//水平垂直且居中
columntBox.setUseAscender(true);
columntBox.setVerticalAlignment(columntBox.ALIGN_MIDDLE);
columntBox.setHorizontalAlignment(Element.ALIGN_CENTER);
columntBox.setVerticalAlignment(Element.ALIGN_MIDDLE);
Paragraph server1Number = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",fontbold);
server1Number.setAlignment(Element.ALIGN_RIGHT);
columntBox.addElement(server1Number);
serverItemMoneyTable.addCell(columntBox);
PdfPCell columnTotalBox = new PdfPCell();
columnTotalBox.setBorderWidth(0.5f);
columnTotalBox.setFixedHeight(35);
columnTotalBox.setColspan(4);
columnTotalBox.setPaddingRight(15);
column3box.setBorderColor(BaseColor.GRAY);
Paragraph server1Total = new Paragraph("xxxxx",discount);
server1Total.setAlignment(Element.ALIGN_RIGHT);
columnTotalBox.addElement(server1Total);
serverItemMoneyTable.addCell(columnTotalBox);
document.add(serverItemMoneyTable);
// 下面不变文本信息 不用管
PdfPTable describeColumnTable = new PdfPTable(1);
describeColumnTable.setWidthPercentage(97);
PdfPCell column1box = new PdfPCell();
column1box.setFixedHeight(30);
column1box.setBorderWidth(0);
Paragraph column1key = new Paragraph("xxxxxx:", fontbold);
column1key.setAlignment(Element.ALIGN_LEFT);
column1box.addElement(column1key);
describeColumnTable.addCell(column1box);
PdfPCell column2box = new PdfPCell();
column2box.setFixedHeight(20);
column2box.setBorderWidth(0);
Paragraph column2key = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", flicking);
column2key.setAlignment(Element.ALIGN_LEFT);
column2box.addElement(column2key);
describeColumnTable.addCell(column2box);
PdfPCell columnbox3 = new PdfPCell();
columnbox3.setFixedHeight(20);
columnbox3.setBorderWidth(0);
columnbox3.setColspan(2);
Paragraph columnkey3 = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", flicking);
columnkey3.setAlignment(Element.ALIGN_LEFT);
columnbox3.addElement(columnkey3);
describeColumnTable.addCell(columnbox3);
PdfPCell columnbox4 = new PdfPCell();
columnbox4.setFixedHeight(20);
columnbox4.setBorderWidth(0);
Paragraph columnkey4 = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", flicking);
columnkey4.setAlignment(Element.ALIGN_LEFT);
columnbox4.addElement(columnkey4);
describeColumnTable.addCell(columnbox4);
PdfPCell columnbox5 = new PdfPCell();
columnbox5.setFixedHeight(20);
columnbox5.setBorderWidth(0);
Paragraph columnkey5 = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", flicking);
columnkey5.setAlignment(Element.ALIGN_LEFT);
columnbox5.addElement(columnkey5);
describeColumnTable.addCell(columnbox5);
PdfPCell columnBox6 = new PdfPCell();
columnBox6.setBorderWidth(0);
columnBox6.setFixedHeight(30);
columnBox6.setBorderColor(BaseColor.GRAY);
columnBox6.setColspan(4);
columnBox6.setPaddingRight(15);
Paragraph columnkey6 = new Paragraph("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx公司", bottom);
columnkey6.setAlignment(Element.ALIGN_RIGHT);
columnBox6.addElement(columnkey6);
describeColumnTable.addCell(columnBox6);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
PdfPCell columnBox7 = new PdfPCell();
columnBox7.setBorderWidth(0);
columnBox7.setFixedHeight(30);
columnBox7.setBorderColor(BaseColor.GRAY);
columnBox7.setColspan(4);
columnBox7.setPaddingRight(15);
Paragraph columnkey7 = new Paragraph(dateFormat.format(new Date()), bottom);
columnkey7.setAlignment(Element.ALIGN_RIGHT);
columnBox7.addElement(columnkey7);
describeColumnTable.addCell(columnBox7);
document.add(describeColumnTable);
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
本文地址:https://blog.csdn.net/weixin_45847074/article/details/107468312
上一篇: 手写快读
下一篇: 【JavaIO流】JavaIO流的使用