利用jacob实现Word转PDF

·  阅读 54
利用jacob转PDF,poi生成Word(注:仅支持windows平台,需要jacob-1.14.3-x64.dll文件,存放在C:\Windows\System32路径下)

一、步骤:

1.导入所需Jar2.生成Word文档
3.转为PDF文件
复制代码

二、所需Jar包

image.png

三、生成Word文档代码

// 返回Docx中需要替换的特殊字符,没有重复项
// 推荐传入正则表达式参数"\\$\\{[^{}]+\\}"
public ArrayList<String> getReplaceElementsInWord(String filePath, String regex) {
    String[] p = filePath.split("\\.");
    if (p.length > 0) {// 判断文件有无扩展名
        // 比较文件扩展名
        if (p[p.length - 1].equalsIgnoreCase("doc")) {
            ArrayList<String> al = new ArrayList<>();
            File file = new File(filePath);
            HWPFDocument document = null;
            try {
                InputStream is = new FileInputStream(file);
                document = new HWPFDocument(is);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Range range = document.getRange();
            String rangeText = range.text();
            CharSequence cs = rangeText.subSequence(0, rangeText.length());
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(cs);
            int startPosition = 0;
            while (matcher.find(startPosition)) {
                if (!al.contains(matcher.group())) {
                    al.add(matcher.group());
                }
                startPosition = matcher.end();
            }
            return al;
        } else if (p[p.length - 1].equalsIgnoreCase("docx")) {
            ArrayList<String> al = new ArrayList<>();
            XWPFDocument document = null;
            try {
                document = new XWPFDocument(POIXMLDocument.openPackage(filePath));
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 遍历段落
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
            while (itPara.hasNext()) {
                XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
                String paragraphString = paragraph.getText();
                CharSequence cs = paragraphString.subSequence(0, paragraphString.length());
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(cs);
                int startPosition = 0;
                while (matcher.find(startPosition)) {
                    if (!al.contains(matcher.group())) {
                        al.add(matcher.group());
                    }
                    startPosition = matcher.end();
                }
            }
            // 遍历表
            Iterator<XWPFTable> itTable = document.getTablesIterator();
            while (itTable.hasNext()) {
                XWPFTable table = (XWPFTable) itTable.next();
                int rcount = table.getNumberOfRows();
                for (int i = 0; i < rcount; i++) {
                    XWPFTableRow row = table.getRow(i);
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        String cellText = "";
                        cellText = cell.getText();
                        CharSequence cs = cellText.subSequence(0, cellText.length());
                        Pattern pattern = Pattern.compile(regex);
                        Matcher matcher = pattern.matcher(cs);
                        int startPosition = 0;
                        while (matcher.find(startPosition)) {
                            if (!al.contains(matcher.group())) {
                                al.add(matcher.group());
                            }
                            startPosition = matcher.end();
                        }
                    }
                }
            }
            return al;
        } else {
            return null;
        }
    } else {
        return null;
    }
}

// 替换word中需要替换的特殊字符
public static boolean replaceAndGenerateWord(String srcPath, String destPath, Map<String, String> map) {
    String[] sp = srcPath.split("\\.");
    String[] dp = destPath.split("\\.");
    if ((sp.length > 0) && (dp.length > 0)) {// 判断文件有无扩展名
        // 比较文件扩展名
        if (sp[sp.length - 1].equalsIgnoreCase("docx")) {
            try {
                XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(srcPath));
                // 替换段落中的指定文字
                Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
                while (itPara.hasNext()) {
                    XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
                    List<XWPFRun> runs = paragraph.getRuns();
                    for (int i = 0; i < runs.size(); i++) {
                        String onegaString = runs.get(i).getText(runs.get(i).getTextPosition());
                        for (Map.Entry<String, String> entry : map.entrySet()) {
                            onegaString = onegaString.replace(entry.getKey(), entry.getValue());
                        }
                        runs.get(i).setText(onegaString, 0);
                    }
                }
                // 替换表格中的指定文字
                Iterator<XWPFTable> itTable = document.getTablesIterator();
                while (itTable.hasNext()) {
                    XWPFTable table = (XWPFTable) itTable.next();
                    int remount = table.getNumberOfRows();
                    for (int i = 0; i < remount; i++) {
                        XWPFTableRow row = table.getRow(i);
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (XWPFTableCell cell : cells) {
                            String cellTextString = cell.getText();
                            for (Map.Entry<String, String> e : map.entrySet()) {
                                if (cellTextString.contains(e.getKey()))
                                    cellTextString = cellTextString.replace(e.getKey(), e.getValue());
                            }
                            cell.removeParagraph(0);
                            cell.setText(cellTextString);
                        }
                    }
                }
                FileOutputStream outStream = null;
                outStream = new FileOutputStream(destPath);
                document.write(outStream);
                outStream.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            // doc只能生成doc,如果生成docx会出错
        } else if ((sp[sp.length - 1].equalsIgnoreCase("doc")) && (dp[dp.length - 1].equalsIgnoreCase("doc"))) {
                HWPFDocument document = null;
                try {
                    document = new HWPFDocument(new FileInputStream(srcPath));
                    Range range = document.getRange();
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        range.replaceText(entry.getKey(), entry.getValue());
                    }
                    FileOutputStream outStream = new FileOutputStream(destPath);
                    document.write(outStream);
                    outStream.close();
                    return true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            } else {
                return false;
            }
    } else {
        return false;
    }
}
复制代码

四、Word转PDF代码

public static void wordToPdf(String fromAddress, String toAddress) {
    ActiveXComponent ax = null;
    try {
        long startTime = System.currentTimeMillis();
        ax = new ActiveXComponent("Word.Application");
        //设置打开word文档不可见
        ax.setProperty("Visible", false);
        //获取Word文档中所有内容
        Dispatch docs = ax.getProperty("Documents").toDispatch();
        //打开word文档,并设置word为不可编辑和不需确认
        Dispatch doc = Dispatch.call(docs,
                "Open",
                fromAddress,// FileName
                false,// ConfirmConversions
                true // ReadOnly
        ).toDispatch();

        File tofile = new File(toAddress);
        if (tofile.exists()) {
            tofile.delete();
        }
        //word文件另存为pdf文件
        Dispatch.call(doc,//
                "SaveAs", //
                toAddress, // FileName
                wdFormatPDF);
        //关闭word文档
        Dispatch.call(doc, "Close", false);
        long endTime = System.currentTimeMillis();
        System.out.println("转化完成,总共耗时" + (endTime - startTime) + "ms。");
    } catch (Exception e) {
        System.out.println("========Error:文档转换失败:" + e.getMessage());
    } finally {
        if (ax != null)
            ax.invoke("Quit", new Variant[]{});
    }
}
复制代码

注:代码于17年编写,Jar可能做了更新

分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改