本文共 5454 字,大约阅读时间需要 18 分钟。
如何使用iTextPDF生成PDF在实际开发中,我们需要导入以下依赖包:依赖包:- com.itextpdf:itextpdf:5.4.3- com.itextpdf:itext-asian:5.2.0PDF生成方法public boolean getPdf(String ids, HttpServletRequest request, HttpServletResponse response, String isAll, YdTemplateOfSpfc ydTemplateOfSpfc) throws Exception { List list = new ArrayList<>(); // 处理isAll参数 if (StringUtil.isNotBlank(isAll)) { EntityWrapper wrapper = new EntityWrapper<>(); // 根据申请ID过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getApplicantId())) { wrapper.where("applicant_id={0}", ydTemplateOfSpfc.getApplicantId()); } // 根据吨次年份过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getTonYear())) { wrapper.where("ton_year={0}", ydTemplateOfSpfc.getTonYear()); } // 根据卸船船名过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getUnloadingVesselName())) { wrapper.like("unloading_vessel_name", ydTemplateOfSpfc.getUnloadingVesselName()); } // 根据卸船船舶ID过滤 if (StringUtil.isNotBlank(ydTemplateOfSpfc.getUnloadingVesselId())) { wrapper.like("receiving_vessel_name", ydTemplateOfSpfc.getReceivingVesselName()); } // 过滤未删除的记录 wrapper.where("isdelete={0}", "0"); // 过滤未上报的状态 wrapper.where("report_state={0}", "1"); list = ydTemplateOfSpfcService.selectList(wrapper); } else { // 处理ids参数 String[] split = ids.split(","); for (int i = 0; i < split.length; i++) { String id = split[i]; // 读取对应的YDTemplateOfSpfc对象 YdTemplateOfSpfc ydTemplateOfSpfc = list.get(i); // 将对象添加到列表中 list.add(ydTemplateOfSpfc); } } // 检查列表是否为空 if (list.isEmpty()) { return false; } // 设置JAXB解析器 System.setProperty("javax.xml.parsers.documentbuilderfactory", "com.sun.org.apache.xerces.internal.jaxp.documentbuilderfactoryimpl"); try { // 读取PDF模板 PDFStamper ps = new PDFStamper(new PDFReader("src/main/resources/nantaireprinttable_model/ceshi.pdf")); // 创建输出流 FileOutputStream fos = new FileOutputStream("src/main/resources/NantaiReprintTable_pdf" + ydTemplateOfSpfc.getAnticipatedDate() + ydTemplateOfSpfc.getAndTime() + "nantaireprinttable.pdf"); // 初始化PDF表单 PDFReader reader = new PDFReader(fos); PDFStamper pdfStamper = new PDFStamper(reader, fos); // 设置默认字体 BaseFont bf = BaseFont.createFont("src/main/resources/NantaiReprintTable_zip/Fonts/simsun.ttc", 0); BaseFont baseFont = BaseFont.IDENTITY_H; pdfStamper.getAcroFields().addSubstitutionFont(bf); // 读取数据并填充到PDF表单 Map data = new HashMap<>(); data.put("fill_1", ydTemplateOfSpfc.getAnticipatedDate()); data.put("fill_2", ydTemplateOfSpfc.getAndTime()); // 处理经度和纬度格式 if (ydTemplateOfSpfc.getAnticipatedLongitude().equals("W")) { ydTemplateOfSpfc.setLongitudeNumber("-" + ydTemplateOfSpfc.getLongitudeNumber()); } if (ydTemplateOfSpfc.getAnticipatedLatitude().equals("S")) { ydTemplateOfSpfc.setLatitudeNumber("-" + ydTemplateOfSpfc.getLatitudeNumber()); } data.put("fill_3", ydTemplateOfSpfc.getLatitudeNumber()); data.put("fill_4", ydTemplateOfSpfc.getLongitudeNumber()); data.put("fill_5", ydTemplateOfSpfc.getTranshippedFishSpecies()); data.put("fill_6", ydTemplateOfSpfc.getVesselName()); data.put("fill_7", ydTemplateOfSpfc.getUnloadingVesselName()); data.put("fill_8", ydTemplateOfSpfc.getUnloadingRegistrationNumber()); data.put("fill_9", ydTemplateOfSpfc.getUnloadingRadioCallSign()); data.put("fill_10", ydTemplateOfSpfc.getUnloadingVesselFlag()); data.put("fill_11", ydTemplateOfSpfc.getUnloadingImoNumber()); data.put("fill_12", ydTemplateOfSpfc.getNameOfUnloadingVesselMaster()); data.put("fill_13", ydTemplateOfSpfc.getNationalityOfUnloadingVesselMaster()); data.put("fill_14", ydTemplateOfSpfc.getReceivingVesselName()); data.put("fill_15", ydTemplateOfSpfc.getReceivingRegistrationNumber()); data.put("fill_16", ydTemplateOfSpfc.getReceivingRadioCallSign()); data.put("fill_17", ydTemplateOfSpfc.getReceivingVesselFlag()); data.put("fill_18", ydTemplateOfSpfc.getUnloadingImoNumber()); data.put("fill_19", ydTemplateOfSpfc.getNameOfReceivingVesselMaster()); data.put("fill_20", ydTemplateOfSpfc.getNationalityOfReceivingVesselMaster()); // 将数据填充到PDF表单 for (String key : data.keySet()) { pdfStamper.getAcroFields().setField(key, data.get(key).toString()); } pdfStamper.setFormFlattening(true); System.out.println("PDF导出成功"); } catch (Exception e) { System.out.println("PDF导出失败"); e.printStackTrace(); } finally { try { ps.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } // 压缩文件 CompressUtil zipFiles(new File("src/main/resources/NantaiReprintTable_zip/"), new File("src/main/resources/NantaiReprintTable_zip/NantaiReprintTable_pdf.zip")); return true;} 以上代码实现了以下功能:
代码中主要使用了以下iTextPDF功能:
代码中还包含了对经纬度格式的处理逻辑,确保输出的PDF文件符合指定的格式要求。
转载地址:http://lovfk.baihongyu.com/