word转pdf

1.功能实现

使用poi对进行word文件进行解析处理,使用aspose进行pdf转换

aspose下载路径:https://pan.baidu.com/s/1vJWgYAAaHpnXmyKhZa4ljg 提取码:jdwp

注:如果导入了easyExcel,则只需要导入一部分poi(关于doc文件的解析),防止版本不一致的问题

2.导入依赖

       
          org.apache.poi
          poi-ooxml
          3.17
      
      
          org.apache.poi
          poi
          3.17
      
      
          org.apache.poi
          poi-ooxml-schemas
          3.17
      
      
      
              org.apache.poi
              poi-scratchpad
              3.17
          
      
     
              com.aspose.words
              com-aspose-words-1.1
              1.1.1
          

3.获取word文件,替换其中的特殊字符:${name}

处理docx文件必须表格和段落分开处理,doc文件则不需要单独处理

   /**
     * 替換word模板的字符预览或者下载
     *
     * @param inputStream 文件输入流
     * @param outputStream 文件输出流
     * @param map ${name}的key的数据
     * @param type 下载或者预览
     * @return
     */
    public static byte[] replaceWord(InputStream inputStream, OutputStream outputStream, Map map, Integer type, String fileType) {
        try {
             //doc文件处理
            if ("doc".equals(fileType)) {
                HWPFDocument document = new HWPFDocument(inputStream);
                replaceDocText(map, document);
                //预览
                if (type == 1) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    //3.输出流
                    document.write(baos);
                    return baos.toByteArray();
                }
                //下载
                document.write(outputStream);
            }
            if ("docx".equals(fileType)) {
              //docx文件处理
                XWPFDocument document = new XWPFDocument(inputStream);
                //替換段落
                replaceDocxTable(map, document);
                //替換文本
                replaceDocxText(map, document);
                //预览
                if (type == 1) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    //3.输出流
                    document.write(baos);
                    return baos.toByteArray();
                }
                //下载
                document.write(outputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

/**
     * 替換Docx表格
     *
     * @param map
     * @param document
     */
    private static void replaceDocxTable(Map map, XWPFDocument document) {
        //2. 替换表格中的指定文字(本人模板中 对应的姓名、性别等)
        Iterator itTable = document.getTablesIterator();
        XWPFTable table;
        int rowsCount;
        while (itTable.hasNext()) {
            table = itTable.next();
            rowsCount = table.getNumberOfRows();
            for (int i = 0; i < rowsCount; i++) {
                XWPFTableRow row = table.getRow(i);
                List cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    for (Map.Entry e : map.entrySet()) {
                        if (cell.getText().equals(e.getKey())) {
                            XWPFParagraph xwpfParagraph = cell.addParagraph();
                            cell.removeParagraph(0);
                            //设置单元格文本样式
                            XWPFRun run1 = xwpfParagraph.createRun();
                            run1.setFontSize(11);
                            run1.setText(e.getValue());
                            //设置内容水平居中
                            CTTc cttc = cell.getCTTc();
                            CTTcPr ctPr = cttc.addNewTcPr();
                            ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                        }
                    }
                }
            }
        }
    }


    /**
     * 替換Docx文本或者段落
     *
     * @param map
     * @param document
     */
    private static void replaceDocxText(Map map, XWPFDocument document) {
        // 替换段落中的指定文字
        Iterator itPara = document.getParagraphsIterator();
        while (itPara.hasNext()) {
            XWPFParagraph paragraph = itPara.next();
            List runs = paragraph.getRuns();
            for (XWPFRun run : runs) {
                String oneparaString = run.getText(run.getTextPosition());
                if (StringUtils.isBlank(oneparaString)) {
                    continue;
                }
                for (Map.Entry entry : map.entrySet()) {
                    oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());
                }
                run.setText(oneparaString, 0);
            }
        }
    }

    /**
     * 替换doc文件
     *
     * @param map
     * @param document
     */
    private static void replaceDocText(Map map, HWPFDocument document) {
        Range range = document.getRange();
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == null) {
                entry.setValue("  ");
            }
            range.replaceText(entry.getKey(), entry.getValue());
        }
    }

4.生成pdf预览

破解生成的水印



    
        
            Aspose.Total for Java
            Aspose.Words for Java
        
        Enterprise
        20991231
        20991231
        8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
    
    sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=

读取破解文件

    /**
     * 去掉aspose的水印
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            ClassPathResource cpr = new ClassPathResource("static" + File.separator + "license.xml");
            InputStream is = cpr.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //word转pdf
    public static void wordToPdf(InputStream inputStream, OutputStream outputStream) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            com.aspose.words.Document doc = new com.aspose.words.Document(inputStream); //inputStream是将要被转化的word文档
            doc.save(outputStream, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            outputStream.close();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5.下载和预览中的response设置

  //预览
  response.setContentType("application/pdf;charset=UTF-8");
  //inline设置是强制浏览器显示,attachment设置时强制浏览器下载
  response.setHeader("Content-Disposition", "inline; filename= file");

//下载
 response.setContentType("application/octet-stream");
 String fileName = fileEntity.getFileName();
 response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
response.flushBuffer();

6.问题

1.如果发现读取word文件失败,可能是word文件后缀与原始后缀不一致,如:新建一个doc,修改为后缀docx,这是读取不了的。
2.部署在linux,预览出现乱码或者空白,是因为缺少字体导致
3.建议优先选择doc格式文件,因为docx文件替换出来的字体不能匹配。

你可能感兴趣的:(word转pdf)