您的位置:首页 > 编程语言 > Java开发

SpringBoot--整合itextpdf

2021-06-05 14:40 246 查看

SpringBoot--整合itextpdf

Dependency

<!--SpringBoot:PDF-->
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
/**
*@Description: 生成pdf
*@Author: HuYuQiao
*/
private static final SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");

private static final Integer size = 20;
private static final String formatTimeStr = format.format(new Date());

@Test
public void testPDF(){
Document document = new Document(PageSize.A4);
try{
PdfWriter.getInstance(document, new FileOutputStream("src/main/resources/hyq.pdf"));
document.addTitle("PDF Title");
document.open();

//PDF--添加table
PdfPTable pdfPTable = generatePDFTable();
document.add(pdfPTable);

} catch (Exception e) {
e.printStackTrace();
} finally {
document.close();
}
}
/**
*@Description: 生成pdf的talble
*@Author: HuYuQiao
*/
public PdfPTable generatePDFTable() throws IOException, DocumentException {
Font font = new Font(BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED));
font.setSize(7);
PdfPTable table = new PdfPTable(10);

//PDF--第一行
PdfPCell pdfTitle= new PdfPCell(new Phrase("PDF导出模板", font));
pdfTitle.setColspan(10);    //所占列数
setStyle(pdfTitle);
table.addCell(pdfTitle);

PdfPCell officialSeal =  new PdfPCell(new Phrase("申请单位公章:", font));
officialSeal.setColspan(2);
setStyle(officialSeal);
table.addCell(officialSeal);

// 空格 第二行
PdfPCell cell3 = new PdfPCell(new Phrase(" "));
cell3.setColspan(2);
setStyle(cell3);
table.addCell(cell3);

// 空格 第二行
PdfPCell cell4 = new PdfPCell(new Phrase(" "));
cell4.setColspan(1);
setStyle(cell4);
table.addCell(cell4);

// 申请日期: 第二行
PdfPCell cell5 = new PdfPCell(new Phrase("申请日期:" + formatTimeStr, font));
cell5.setColspan(2);
setStyle(cell5);
table.addCell(cell5);

// 空格 第二行
PdfPCel
56c
l cell6 = new PdfPCell(new Phrase(" "));
cell6.setColspan(1);
setStyle(cell6);
table.addCell(cell6);

// 单位:元 第二行
PdfPCell cell7 = new PdfPCell(new Phrase("单位:元", font));
cell7.setColspan(2);
setStyle(cell7);
table.addCell(cell7);

// 序号 第三,四行
PdfPCell cell8 = new PdfPCell(new Phrase("序号", font));
cell8.setColspan(1);    // 占几列
cell8.setRowspan(2);    // 占几行
setStyle(cell8);
table.addCell(cell8);

// 资金性质 第三,四行
PdfPCell cell9 = new PdfPCell(new Phrase("资金性质", font));
cell9.setColspan(1);    // 占几列
cell9.setRowspan(2);    // 占几行
setStyle(cell9);
table.addCell(cell9);

// 编码 第三,四行
PdfPCell cell11 = new PdfPCell(new Phrase("编码", font));
cell11.setColspan(1);    // 占几列
cell11.setRowspan(2);    // 占几行
setStyle(cell11);
table.addCell(cell11);

// 名称 第三,四行
PdfPCell cell12 = new PdfPCell(new Phrase("名称", font));
cell12.setColspan(1);    // 占几列
cell12.setRowspan(2);    // 占几行
setStyle(cell12);
table.addCell(cell12);

// 项目名称 第三,四行
PdfPCell cell13 = new PdfP
1e09
Cell(new Phrase("项目名称", font));
cell13.setColspan(1);    // 占几列
cell13.setRowspan(2);    // 占几行
setStyle(cell13);
table.addCell(cell13);

// 姓名 第三,四行
PdfPCell cell14 = new PdfPCell(new Phrase("姓名", font));
cell14.setColspan(1);    // 占几列
cell14.setRowspan(2);    // 占几行
setStyle(cell14);
table.addCell(cell14);

// 银行账号 第三,四行
PdfPCell cell15 = new PdfPCell(new Phrase("银行账号", font));
cell15.setColspan(1);    // 占几列
cell15.setRowspan(2);    // 占几行
setStyle(cell15);
table.addCell(cell15);

// 开户银行 第三,四行
PdfPCell cell16 = new PdfPCell(new Phrase("开户银行", font));
cell16.setColspan(1);    // 占几列
cell16.setRowspan(2);    // 占几行
setStyle(cell16);
table.addCell(cell16);

// 申请金额 第三,四行
PdfPCell cell17 = new PdfPCell(new Phrase("申请金额", font));
cell17.setColspan(1);    // 占几列
cell17.setRowspan(2);    // 占几行
setStyle(cell17);
table.addCell(cell17);

// 核定金额 第三,四行
PdfPCell cell18 = new PdfPCell(new Phrase("核定金额", font));
cell18.setColspan(1);    // 占几列
cell18.setRowspan(2);    // 占几行
setStyle(cell18);
table.addCell(cell18);

// 根据自己业务封装数据
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
PdfPCell cell19 = new PdfPCell(new Phrase("坑位置"+i+j, font));
cell19.setColspan(1);    // 占几列
cell19.setRowspan(1);    // 占几行
setStyle(cell19);
table.addCell(cell19);
}
}

return table;
}
/**
* 设置样式
* @param cell
*/
private void setStyle(PdfPCell cell){
cell.setFixedHeight(size);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
}



Map遍历与List排序

@Test
public void testSort(){
HashMap<String, Integer> items = new HashMap<>();
items.put("coins", 5);
items.put("pens", 2);
items.put("chairs", 7);
items.forEach((key, value) -> System.out.println(key + " " + value));

for (Map.Entry<String, Integer> item : items.entrySet()){
System.out.println(item.getKey() + " " + item.getValue());
}

List<Content> contentList = new ArrayList<>();
contentList.add(new Content("A", "A", "A"));
contentList.add(new Content("B", "B", "B"));
contentList.add(new Content("C", "C", "C"));
contentList.sort(Comparator.comparing(Content::getPrice).reversed());
contentList.stream().forEach(content -> {
System.out.println(content);
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: