Jacob开发文件转PDF

这三种方法我都有试过word转PDF,第2种、第3种对于图片,表格、中文转换效果都不好,方法1效果最好。但方法1 只支持Windows环境下。

1.开发环境

    Windows系统;

2.准备工作:

    step1:下载jacob文件,里面包含jacob.jar、jacob-版本号-x64.dll,jacob-版本号-x86.dll三个文件。

    step2:a.把dll文件放在%JAVA_HOME%in下(注意系统是32位还是64位),也可以放在C:WindowsSystem32下,如果是64位应该放在C:WindowsSysWOW64 下。建议放在jdk的bin目录下

                b.如果是在eclipse下开发,需要重新引入jdk(Preference/Java/Installed JREs);

                c.将jacab.jar包放在项目lib下并add到liabraries中即可.

3.代码:

   Word.Excel、PPT文件转PDDF(注意自己电脑注册表是office还是WPS,决定了代码里的ActiveXComponent使用选择,我的是office,在引用资料里有使用WPS的案例

   /**
     * 调用转PDF的方法
     * @param inputFile 传入文件路径
     * @param ouputFile 生成文件路径
     * @return
     */
    public static boolean PdfManager(String inputFile,String ouputFile) {
        boolean flag = TransferToPDFUtil.convert2PDF(inputFile, ouputFile);
        return flag;
    }
    
    /***
     * 判断文件类型
     * 
     * @param fileName
     * @return
     */
    public static String getFileSufix(String fileName) {
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(splitIndex + 1);
    }
    
    /***
     * 判断需要转化文件的类型(Excel、Word、ppt)
     * 
     * @param inputFile  传入文件
     * @param pdfFile    转化文件
     */
    public static boolean convert2PDF(String inputFile, String pdfFile) {
        String suffix = getFileSufix(inputFile);
        File file = new File(inputFile);
        if (!file.exists()) {
            System.out.println("文件不存在!");
            return false;
        }
        if (suffix.equals("pdf")) {
            System.out.println("PDF not need to convert!");
            return true;
        }
        if (suffix.equals("doc") || suffix.equals("docx")) {
            return word2PDF(inputFile, pdfFile);
        } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
            return ppt2PDF(inputFile, pdfFile);
        } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
            return excel2PDF(inputFile, pdfFile);
        } else {
            System.out.println("文件格式不支持转换!");
            return false;
        }
    }
  /***
     * 
     * Word转PDF
     * 
     * @param inputFile
     * @param pdfFile
     * @return
     */
    private static boolean word2PDF(String inputFile, String pdfFile) {
        try {
            ActiveXComponent app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
                    .toDispatch();
            File tofile = new File(pdfFile);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF 
                    );
            Dispatch.call(doc, "Close", false);
            app.invoke("Quit", 0);
            File fromfile = new File(inputFile);
            if (fromfile.exists()) {
                fromfile.delete();
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    private static boolean excel2PDF(String inputFile, String pdfFile) {
        try {
            ActiveXComponent app = new ActiveXComponent("Excel.Application");
            app.setProperty("Visible", false);
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
                    true).toDispatch();
            File tofile = new File(pdfFile);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(excel, "ExportAsFixedFormat", xlsFormatPDF, pdfFile);
            Dispatch.call(excel, "Close", false);
            app.invoke("Quit");
            File fromfile = new File(inputFile);
            if (fromfile.exists()) {
                fromfile.delete();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    private static boolean ppt2PDF(String inputFile, String pdfFile) {
        try {
            ActiveXComponent app = new ActiveXComponent(
                    "PowerPoint.Application");
            Dispatch ppts = app.getProperty("Presentations").toDispatch();
            Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
                    true,// Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
                    ).toDispatch();
            File tofile = new File(pdfFile);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(ppt, "SaveAs", pdfFile, pptFormatPDF);
            Dispatch.call(ppt, "Close");
            app.invoke("Quit");
            File fromfile = new File(inputFile);
            if (fromfile.exists()) {
                fromfile.delete();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

 4.引用资料:

      java将doc文件转换为pdf文件的三种方法

     Java使用Jacob将Word、Excel、PPT转化成PDF

      java使用jacob将office转pdf

原文地址:https://www.cnblogs.com/Dream2hc/p/tools4952190.html