package com.ruoyi; import cn.hutool.core.io.resource.ClassPathResource; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import com.ruoyi.pdf.report.CreateReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; @SpringBootTest public class PDFTest { @Autowired private CreateReport createReport; @Test public void export() throws IOException, DocumentException { //设置具体字体格式编码,不设置的话中文可能会不显示 BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); ClassPathResource classPathResource = new ClassPathResource("template/template.pdf"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(classPathResource.getStream()); bos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, bos); AcroFields form = stamper.getAcroFields(); Map<String, String> map = new HashMap(); map.put("system_review_01_standard", "车辆生产企业应建立车辆全生命周期的汽车信息安全管理体系。"); map.put("system_review_01_result", "车辆生产企业建立了车辆全生命周期的汽车信息安全管理体系。"); map.put("system_review_01_pass", "不符合"); this.fillPdfCellForm(map, form, stamper, baseFont); // true代表生成的PDF文件不可编辑 stamper.setFormFlattening(true); stamper.close(); bos.writeTo(new FileOutputStream("./aaa.pdf")); } private void fillPdfCellForm(Map<String, String> map, AcroFields form, PdfStamper stamper, BaseFont baseFont) throws IOException, DocumentException { for (Map.Entry entry : map.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); // form.setField(key, value); form.setFieldProperty(key, "setfflags",PdfFormField.FF_MULTILINE, null); addTextToPdfCenter(form, stamper, value, key, baseFont); } } private static void addTextToPdfCenter(AcroFields form, PdfStamper stamper, String text,String fieldName,BaseFont baseFont){ // 通过模板表单单元格名获取所在页和坐标,左下角为起点 int pageNo = form.getFieldPositions(fieldName).get(0).page; Rectangle signRect = form.getFieldPositions(fieldName).get(0).position; // 获取操作的页面 PdfContentByte contentByte = stamper.getOverContent(pageNo); //创建表单 PdfPTable table = new PdfPTable(1); //获取当前模板表单宽度 float totalWidth = signRect.getRight() - signRect.getLeft() - 1; //设置新表单宽度 table.setTotalWidth(totalWidth); //设置中文格式 Font font = new Font(baseFont); // 设置字号 font.setSize(10.56F); //设置单元格格式 PdfPCell cell = new PdfPCell(new Phrase(text,font)); //设置单元格高度 cell.setFixedHeight(signRect.getTop()-signRect.getBottom()-1); cell.setBorderWidth(0); //设置垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置水平居中 // cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 设置文字在单元格中换行时的行间距 :最终行间距 :totalLeading= fixedLeading+fontSize(字体大小)* multipliedLeading; cell.setLeading(0.0f,1.2f); //添加到表单中 table.addCell(cell); //写入pdf table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), contentByte); } @Test public void createTest() throws DocumentException, IOException { createReport.create();; } }