Java使用IText根据pdf模板创建pdf文件

1.导包

java 复制代码
 <dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
 </dependency>
 <dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>itext-asian</artifactId>
     <version>5.2.0</version>
 </dependency>
 <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

2.绘制pdf模板

3.示例代码

java 复制代码
/**
 * 读取pdf模板填充生成pdf文件并转为字节数组
 * @return
 */
public byte[] createPdf() {
	//获取源数据(一般从数据库查询,此处通过构造数据简化处理)
	List<Plan> plans = this.getPlans();
       final int size = plans.size();
       //临时文件
       final String outputFilePath = "D:/" + System.currentTimeMillis() + ".pdf";
       OutputStream os = null;
       PdfStamper ps = null;
       PdfReader reader = null;
       PdfReader reader2 = null;
       Document document = null;
       ByteArrayOutputStream byteArrayOutputStream = null;
       PdfCopy pdfNew = null;
       try {
           //读入pdf表单
           reader = new PdfReader(this.getClass().getResourceAsStream("/template/test.pdf"));
           //根据表单生成一个新的pdf
           os = Files.newOutputStream(new File(outputFilePath).toPath());
           ps = new PdfStamper(reader, os);
           //获取pdf表
           AcroFields form = ps.getAcroFields();
           form.setField("contractNo", "HT2024042501");//合同号
           form.setField("paymentDate", "2022-12-01");//放款日期
           form.setField("expiredDate", "2023-12-01");//到期日期
           ps.setFormFlattening(true);
           List<AcroFields.FieldPosition> table = form.getFieldPositions("table");
           Rectangle rect = table.get(0).position;
           PdfPTable pTable = new PdfPTable(8);
           BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
           Font fontZH = new Font(bfChinese, 10f, 0);
           pTable.getDefaultCell().setHorizontalAlignment(1);
           pTable.setWidthPercentage(100.0f);
           float totalWidth = rect.getRight() - rect.getLeft() - 1;
           //计算表格宽度
           float[] columnWidth = {(float) (totalWidth * 0.05), (float) (totalWidth * 0.15), (float) (totalWidth * 0.15), (float) (totalWidth * 0.15), (float) (totalWidth * 0.15), (float) (totalWidth * 0.13), (float) (totalWidth * 0.13), (float) (totalWidth * 0.09)};
           pTable.setTotalWidth(columnWidth);
           pTable.setLockedWidth(true);
           for (Plan plan : plans) {
               Integer periodNo = plan.getPeriodNo();
               int border = (periodNo % 12 == 0 || periodNo == size) ? Rectangle.BOTTOM : Rectangle.NO_BORDER;
               pTable.addCell(generateCell(String.valueOf(periodNo), border, fontZH));
               pTable.addCell(generateCell(plan.getMatchDate(), border, fontZH));
               pTable.addCell(generateCell(plan.getRepayOriginal(), border, fontZH));
               pTable.addCell(generateCell(plan.getPenalty(), border, fontZH));
               pTable.addCell(generateCell(plan.getRepayAmount(), border, fontZH));
               pTable.addCell(generateCell(plan.getRestOriginal(), border, fontZH));
               pTable.addCell(generateCell(plan.getRepayDate(), border, fontZH));
               pTable.addCell(generateCell(plan.getRepayStatus(), border, fontZH));
           }
           pTable.setKeepTogether(true);
           pTable.setSplitLate(false);
           pTable.setSplitRows(true);
           //计算需要分页的总页数
           int totalpage = size % 12 == 0 ? size / 12 : size / 12 + 1;
           if (totalpage == 1) {
               //获table页面
               PdfContentByte under = ps.getOverContent(1);
               //添加table
               pTable.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), under);
           } else {
               for (int i = 1; i <= totalpage; i++) {
                   PdfContentByte under = ps.getOverContent(i);
                   if (i == 1) {
                       pTable.writeSelectedRows(0, 12, rect.getLeft(), rect.getTop(), under);
                   } else {
                       pTable.writeSelectedRows((i - 1) * 12, i * 12, rect.getLeft(), rect.getTop(), under);
                   }
               }
           }
           ps.close();
           reader2 = new PdfReader(outputFilePath);
           document = new Document(reader2.getPageSizeWithRotation(1));
           byteArrayOutputStream = new ByteArrayOutputStream();
           pdfNew = new PdfCopy(document, byteArrayOutputStream);
           document.open();
           PdfImportedPage page;
           for (int i = 1; i <= totalpage; i++) {
               page = pdfNew.getImportedPage(reader2, i);
               pdfNew.addPage(page);
           }
           document.close();
           os.close();
           pdfNew.close();
           reader.close();
           reader2.close();
           FileUtils.delete(new File(outputFilePath));
           return byteArrayOutputStream.toByteArray();
       } catch (Exception e) {
       	e.printStackTrace();
       } finally {
           try {
               if (ps != null) {
                   ps.close();
               }
               if (reader != null) {
                   reader.close();
               }
               if (os != null) {
                   os.close();
               }
               if (document != null) {
                   document.close();
               }
               if (pdfNew != null) {
                   pdfNew.close();
               }
               if (reader2 != null) {
                   reader2.close();
               }
               if (byteArrayOutputStream != null) {
                   byteArrayOutputStream.close();
               }
           } catch (Exception e) {
           	e.printStackTrace();
           }
       }
	return null;
}

private List<Plan> getPlans() {
	List<Plan> plans = new ArrayList<Plan>();
	for(int i=1;i<=12;i++) {
		plans.add(new Plan(i,"2023-"+String.format("%02d", i)+"-01","999","-", "999","0","2023-"+String.format("%02d", i)+"-01", "已结清"));
	}
	return plans;
}

   private PdfPCell generateCell(String value, int border, Font fontZH) {
       PdfPCell cell = new PdfPCell(new Phrase(value, fontZH));
       cell.setBorder(border);
       cell.setHorizontalAlignment(Element.ALIGN_CENTER);
       return cell;
   }
   
   public static void main(String [] args) throws FileNotFoundException, IOException {
   	//将字节流转为pdf文件放于D盘
       OutputStream fileOut = new FileOutputStream("D:/a.pdf");
       fileOut.write(new TestPdf().createPdf());
   }

4.执行结果

5.所用到jar包下载地址
itextpdf-5.5.10.jar

欢迎大家积极留言交流学习心得,点赞的人最美丽!

相关推荐
秋意钟7 分钟前
Spring新版本
java·后端·spring
椰椰椰耶9 分钟前
【文档搜索引擎】缓冲区优化和索引模块小结
java·spring·搜索引擎
mubeibeinv11 分钟前
项目搭建+图片(添加+图片)
java·服务器·前端
青莳吖12 分钟前
Java通过Map实现与SQL中的group by相同的逻辑
java·开发语言·sql
Buleall19 分钟前
期末考学C
java·开发语言
重生之绝世牛码21 分钟前
Java设计模式 —— 【结构型模式】外观模式详解
java·大数据·开发语言·设计模式·设计原则·外观模式
小蜗牛慢慢爬行27 分钟前
有关异步场景的 10 大 Spring Boot 面试问题
java·开发语言·网络·spring boot·后端·spring·面试
新手小袁_J1 小时前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
呆呆小雅1 小时前
C#关键字volatile
java·redis·c#
Monly211 小时前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat