docx格式的word文档内容替换 并转换为pdf
需求场景:要批量生成一批合同 ,替换姓名、身份证、手机号,并转成pdf。
word格式如下:

Excel名单的格式如下:

1.首先引入依赖包:
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>2.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.pdf -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
2.word内容替换函数
private static void replaceText(XWPFDocument document, String searchText, String replacement) { for (XWPFParagraph paragraph : document.getParagraphs()) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { String text = run.getText(0); if (text != null && text.contains(searchText)) { text = text.replace(searchText, replacement); run.setText(text, 0); } } } }
3.替换后转为pdf
public static void convertToPDF(String inputFilePath,String outputPDFFile, String name,String licenceID,String phone){ try (InputStream fis = new FileInputStream(inputFilePath); XWPFDocument document = new XWPFDocument(fis)) { // 替换文本 replaceText(document, "${name}", name); replaceText(document, "${licenceid}", licenceID); replaceText(document, "${phone}", phone); // 保存修改后的文档 try (FileOutputStream fos = new FileOutputStream(outputPDFFile)) { PdfOptions options = PdfOptions.create(); PdfConverter.getInstance().convert(document,fos, options); //document.write(fos); Thread.sleep(1); document.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } System.out.println("文档处理完成,已保存到 " + outputPDFFile); fis.close(); } catch (IOException e) { e.printStackTrace(); } }
4.完整的如下:
package com.springboot2x.util; import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.*; import java.util.List; public class ContractReplace { public static void main(String[] args) throws Exception { String inputFilePath = "D:\\合同.docx"; // 输入模板文件路径 //String name = "namexxx"; //String licenceID = "354565465767676676"; //String phone = "1500065655665"; // 1. 输入流中获取工作簿 // 通过文件流工厂方式来处理xls 和xlsx 两种类型文件 String filepath = "员工三要素表.xlsx"; Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(filepath))); // 2. 在工作簿中获取目标工作表 Sheet sheet = workbook.getSheetAt(0); // 3. 获取工作表中的行数(先获取第一行数据,因为模板中第一行数据包含对应的字段表头信息) int rowNum = sheet.getPhysicalNumberOfRows(); //int lastRowNum = sheet.getLastRowNum(); // 获取表格最后一行数() if (rowNum == 0) { throw new Exception(filepath + "文件中Sheet0不存在或Sheet0页中没有数据"); } System.out.println(rowNum); for (int i = 1; i < rowNum; i++) { Thread.sleep(10); System.out.println("第" + i + "个数据"); Row rowValue = sheet.getRow(i); DataFormatter formatter = new DataFormatter(); String Name = formatter.formatCellValue(rowValue.getCell(1)); String licenceID = formatter.formatCellValue(rowValue.getCell(2)); String Phone = formatter.formatCellValue(rowValue.getCell(3)); String outputPDFFile = "D:\\Download\\"+licenceID+".pdf"; // 输出文件路径 convertToPDF(inputFilePath,outputPDFFile,Name,licenceID,Phone); } } public static void convertToPDF(String inputFilePath,String outputPDFFile, String name,String licenceID,String phone){ try (InputStream fis = new FileInputStream(inputFilePath); XWPFDocument document = new XWPFDocument(fis)) { // 替换文本 replaceText(document, "${name}", name); replaceText(document, "${licenceid}", licenceID); replaceText(document, "${phone}", phone); // 保存修改后的文档 try (FileOutputStream fos = new FileOutputStream(outputPDFFile)) { PdfOptions options = PdfOptions.create(); PdfConverter.getInstance().convert(document,fos, options); //document.write(fos); Thread.sleep(1); document.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } System.out.println("文档处理完成,已保存到 " + outputPDFFile); fis.close(); } catch (IOException e) { e.printStackTrace(); } } private static void replaceText(XWPFDocument document, String searchText, String replacement) { for (XWPFParagraph paragraph : document.getParagraphs()) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { String text = run.getText(0); if (text != null && text.contains(searchText)) { text = text.replace(searchText, replacement); run.setText(text, 0); } } } } }
处理完毕。

浙公网安备 33010602011771号