Jacob操作ppt


前幾天使用Apache 的POI操作ppt,后來發現轉成的圖片出現亂碼,而且處理了之后,還會有遺留

因此決定換一種處理方式

Jacob 是 JAVA-COM Bridge的縮寫,是一個中間件,能夠提供自動化訪問MS系統下COM組件和Win32 libraries的功能。

1.准備

(1)安裝MS Office

(2)使用spring boot 框架

(3)pom.xml 添加 jacob 依賴

<dependency>
     <groupId>net.sf.jacob-project</groupId>
     <artifactId>jacob</artifactId>
     <version>1.14.3</version>
</dependency>

(4)安裝dll

在https://mvnrepository.com/ 查詢jacob,選擇第一個

選擇適合自己機器的dll文件

將下載下來的dll文件放在

C:\Program Files\Java\jdk1.8.0_151\bin
C:\Program Files\Java\jdk1.8.0_151\jre\bin
C:\WINDOWS\system32
C:\Program Files\Java\jre1.8.0_151\bin
其實只要在 C:\WINDOWS\system32下就可以找到了

2.使用

  • MS系統提供的COM組件
COM組件 對象ID
MS Word Word.Application
MS Excel Excel.Application
MS Powerpoint Powerpoint.Application
 

重要的類和方法

JacobObject:用於Java程序MS下的COM進行通信,創建標准的API框架

ComThread:初始化COM組件線程,釋放線程,對線程進行管理

Dispatch:調度處理類,封裝了操作來從而操作Office,並表示不同MS級別調度對象

  Dispatch.get(dispatch, String name);獲取對象屬性   Dispatch.put(dispatch, String name, Object value);設置對象屬性   Dispatch.call(dispatch, String name, Object… args);調用對象方法 ActiveXComponent : 創建COM組件 Variant : 與COM通訊的參數或者返回值

可以參考VBA API,使用Jacob操作COM組件 https://docs.microsoft.com/en-us/office/vba/api/powerpoint.slides.insertfromfile 查看 

處理過程

  初始化ComThread——》初始化應用——》設置應用屬性——》獲取應用屬性或對象,設置屬性參數——》調用屬性對應的方法

(1)ppt轉pdf

public BackRS ppt2Pdf(String sourcePath, String destPath,BackRS rs){
        ActiveXComponent ppt = null;
        Dispatch presentations = null;
        try {
            ComThread.InitMTA(true);
            ppt = new ActiveXComponent("PowerPoint.application");
            Dispatch.put(ppt.getObject(), "DisplayAlerts", new Variant(false));

            presentations = ppt.invokeGetComponent("Presentations")
                    .invokeGetComponent("Open", new Variant(sourcePath));

            Dispatch.invoke(presentations,
                    "SaveAs",
                    Dispatch.Method,
                    new Object[]{destPath, new Variant(32)},
                    new int[1]);
            rs.setFlag(true);
            rs.setMsg("pdf successfully converted.");
            rs.setPath(destPath);
            rs.setFileName(sourcePath);
        } catch (Exception e) {
            rs.setFlag(false);
            rs.setMsg(e.getMessage());
        } finally {
            try {
                if (ppt != null) {
                    ppt.invoke("Quit");
                }
            } catch (Exception e) {
                rs.setFlag(false);
                rs.setMsg(e.getMessage());
            } finally {
                if (presentations != null)
                    presentations.safeRelease();
                if (ppt != null)
                    ppt.safeRelease();
                ComThread.Release();
            }
        }
        return rs;
    }

(2)ppt按頁保存圖片

public static final int PPT_SAVEAS_JPG = 17;

    public BackRS converter(String fileName){

        BackRS rs = new BackRS();
        ActiveXComponent ppt = null;
        Dispatch presentations = null;
        String source = fileName;
        File file = new File(source);
        if (!file.exists()) {
            rs.setFlag(false);
            rs.setMsg("轉換文件不存在");
            return rs;
        }
        String filePath = file.getParent()+File.separator;
        String dest = filePath + getFileNameNoEx(file.getName())+"_JPG";
        File destPath = new File(dest);
        if (!destPath.exists()) {
            destPath.mkdir();
        }
        try {
            ComThread.InitMTA(true);
            ppt = new ActiveXComponent("PowerPoint.application");

            Dispatch.put(ppt.getObject(), "DisplayAlerts", new Variant(false));
            presentations = ppt.invokeGetComponent("Presentations").invokeGetComponent("Open", new Variant(source));

            saveAs(presentations, dest, PPT_SAVEAS_JPG);
            rs.setFlag(true);
            rs.setMsg("Image successfully converted.");
            rs.setPath(dest);
            rs.setFileName(fileName);
        } catch (Exception e) {
            rs.setFlag(false);
            rs.setMsg(e.getMessage());
        } finally {
            try {
                if (ppt != null) {
                    ppt.invoke("Quit");
                }
            } catch (Exception e) {
                rs.setFlag(false);
                rs.setMsg(e.getMessage());
            } finally {
                if (presentations != null)
                    presentations.safeRelease();
                if (ppt != null)
                    ppt.safeRelease();
                ComThread.Release();
            }
        }
        return rs;
    }

    public void saveAs(Dispatch presentation, String saveTo,
                       int ppSaveAsFileType)throws Exception {
        Dispatch.call(presentation, "SaveAs", saveTo, new Variant(
                ppSaveAsFileType));
    }

(3)ppt合並

public void merge(String outPutPPTPath, List<String> mergePPTPathList) {
        // 啟動 office PowerPoint程序
        ActiveXComponent pptApp = null;
        Dispatch presentations = null;
        Dispatch outputPresentation;

        try {
            ComThread.InitMTA(true);
            pptApp = new ActiveXComponent("PowerPoint.Application");
            Dispatch.put(pptApp, "Visible", new Variant(true));
            presentations = pptApp.getProperty("Presentations").toDispatch();

            File file = new File(outPutPPTPath);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            // 打開輸出文件
            outputPresentation = Dispatch.call(presentations, "Open", outPutPPTPath, false,
                    false, true).toDispatch();

            // 循環添加合並文件
            for (String mergeFile : mergePPTPathList) {
                Dispatch mergePresentation = Dispatch.call(presentations, "Open", mergeFile, false,
                        false, true).toDispatch();

                Dispatch mergeSildes = Dispatch.get(mergePresentation, "Slides").toDispatch();
                int mergePageNum = Integer.parseInt(Dispatch.get(mergeSildes, "Count").toString());

                // 關閉合並文件
                Dispatch.call(mergePresentation, "Close");

                Dispatch outputSlides = Dispatch.call(outputPresentation, "Slides").toDispatch();
                int outputPageNum = Integer.parseInt(Dispatch.get(outputSlides, "Count").toString());

                // 追加待合並文件內容到輸出文件末尾
                Dispatch.call(outputSlides, "InsertFromFile", mergeFile, outputPageNum, 1, mergePageNum);
            }
            // 保存輸出文件,關閉退出PowerPonit.
            Dispatch.call(outputPresentation, "Save");
            Dispatch.call(outputPresentation, "Close");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pptApp != null) {
                    pptApp.invoke("Quit");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (presentations != null)
                    presentations.safeRelease();
                if (pptApp != null)
                    pptApp.safeRelease();
                ComThread.Release();
            }
        }
    }

  將mergePPTPathList 中的文件,追加到 outPutPPTPath文件中

  下面的語句

Dispatch.call(outputSlides, "InsertFromFile", mergeFile, outputPageNum, 1, mergePageNum);
InsertFromFile( _FileName_, _Index_, _SlideStart_, _SlideEnd_ )

 參考這個原型,可以實現追加指定的第umPage頁面

Dispatch.call(outputSlides, "InsertFromFile",mergeFile, outputPageNum, umPage, umPage);

說明:

  (1)出現錯誤 Can't get object clsid from progid

    檢查 dll文件都在指定位置,但還是報錯

    后來發現原來參考的其他人的文章Jacob調用WPS將Office文件轉為PDF文件,對象ID不正確

    其中 ppt = new ActiveXComponent("KWPP.Application");改為 ppt = new ActiveXComponent("PowerPoint.application");

  (2)Can’t load IA 32-bit .dll on a AMD 64-bit platform 

    出現這個報錯是因為使用的jacob.dll和系統不匹配,把32位的用在了64位的系統上了,幾位的系統就用幾位jacob.dll

  (3)如果不想用Office想用WPS,不能安裝極小安裝包

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM