WordToPDFMerger.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.goafanti.common.utils;
  2. import com.goafanti.common.utils.excel.FileUtils;
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.PageSize;
  6. import com.itextpdf.text.Paragraph;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  9. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  10. import org.apache.poi.xwpf.usermodel.XWPFTable;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.util.List;
  17. /**
  18. * Word文档合并为PDF工具类
  19. */
  20. public class WordToPDFMerger {
  21. /**
  22. * 将两个Word文档合并为一个PDF文件并提供下载
  23. *
  24. * @param wordFile1 第一个Word文件路径
  25. * @param wordFile2 第二个Word文件路径
  26. * @param response HttpServletResponse对象
  27. * @param pdfFileName 生成的PDF文件名
  28. * @param uploadPath 上传路径
  29. */
  30. public static void mergeWordDocumentsToPdf(String wordFile1, String wordFile2,
  31. HttpServletResponse response, String pdfFileName, String uploadPath) {
  32. String realFileName = uploadPath + "/tmp/" + System.currentTimeMillis() + ".pdf";
  33. Document document = new Document(PageSize.A4);
  34. try {
  35. // 设置响应头,用于文件下载
  36. FileUtils.setAttachmentResponseHeader(response, pdfFileName, pdfFileName + ".pdf");
  37. // 创建PDF写入器
  38. FileOutputStream outputStream = new FileOutputStream(realFileName);
  39. PdfWriter.getInstance(document, outputStream);
  40. document.open();
  41. // 处理第一个Word文档
  42. processWordDocument(wordFile1, document);
  43. // 添加分页
  44. document.newPage();
  45. // 处理第二个Word文档
  46. processWordDocument(wordFile2, document);
  47. document.close();
  48. outputStream.close();
  49. // 提供文件下载
  50. FileUtils.writeBytes(realFileName, response.getOutputStream());
  51. FileUtils.deleteFile(realFileName);
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. /**
  57. * 处理单个Word文档并将其内容添加到PDF文档中
  58. *
  59. * @param wordFilePath Word文件路径
  60. * @param document PDF文档对象
  61. * @throws IOException
  62. * @throws DocumentException
  63. */
  64. private static void processWordDocument(String wordFilePath, Document document)
  65. throws IOException, DocumentException {
  66. try (InputStream in = new FileInputStream(wordFilePath)) {
  67. XWPFDocument xwpfDocument = new XWPFDocument(in);
  68. // 获取文档中的所有段落
  69. List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
  70. for (XWPFParagraph paragraph : paragraphs) {
  71. // 创建PDF段落
  72. String text = paragraph.getText();
  73. if (text != null && !text.isEmpty()) {
  74. document.add(new Paragraph(text));
  75. }
  76. }
  77. // 处理表格(如果有)
  78. List<XWPFTable> tables = xwpfDocument.getTables();
  79. for (XWPFTable table : tables) {
  80. // 添加表格标识(实际项目中可以进一步处理表格内容)
  81. document.add(new Paragraph("[表格内容]"));
  82. }
  83. }
  84. }
  85. }