首先結(jié)論 : 給現(xiàn)有 ppt 添加一頁設置背景圖片置底,修改文字顏色
- 聲明兩個私有變量
private ActiveXComponent ppt;
private ActiveXComponent presentation;
- 根據(jù)傳入的 ppt
路徑構(gòu)建一個ppt對象
/***
*
* Title: JacobPptUtil
* Description: 構(gòu)建ppt對象
* @param filePath ppt路徑
* @param isVisble 是否可見
*/
public JacobPptUtil(String filePath, boolean isVisble) throws Exception {
if (null == filePath || "".equals(filePath)) {
throw new Exception("文件路徑為空!");
}
File file = new File(filePath);
if (!file.exists()) {
throw new Exception("文件不存在!");
}
ppt = new ActiveXComponent("PowerPoint.Application");
//設置是否可見
Dispatch.put(ppt, "Visible", isVisble);
// 打開一個現(xiàn)有的 Presentation 對象
ActiveXComponent presentations = ppt.getPropertyAsComponent("Presentations");
presentation = presentations.invokeGetComponent("Open", new Variant(filePath), new Variant(true));
}
- 獲取幻燈片對象
Dispatch windows = presentation.getProperty("Windows").toDispatch();
Dispatch window = Dispatch.call(windows, "Item", new Variant(1)).toDispatch();
Dispatch selection = Dispatch.get(window, "Selection").toDispatch();
//獲取幻燈片對象
ActiveXComponent slides = presentation.getPropertyAsComponent("Slides");
- 在之前添加一頁
/**
* @param type
* 2:標題+文本
* 3:標題+左右對比文本
* 4:標題+表格
* 5:標題+左文本右圖表
* 6:標題+左圖表右文本
* 7:標題+SmartArt圖形
* 8:標題+圖表
*/
private static Variant addPptPage(ActiveXComponent slides,int pptPage,int type)throws Exception{
return slides.invoke("Add", new Variant(pptPage), new Variant(type));
}
- 給選中元素添加值 (每頁幻燈片的元素在添加值的時候已經(jīng)確定了
例: 如果創(chuàng)建類型為 2,那么元素就只有
標題和文本
/**
* 在Selection對象上修改TEXT對象的值
*
* @param selectionObj
* @param value
* @throws Exception
*/
public void addTextValue(Dispatch selectionObj, String value) throws Exception {
Dispatch shapeRange = (Dispatch) Dispatch.get(selectionObj, "ShapeRange").getDispatch();
Dispatch textFrame = (Dispatch) Dispatch.get(shapeRange, "TextFrame").getDispatch();
Dispatch textRange = (Dispatch) Dispatch.get(textFrame, "TextRange").getDispatch();
Dispatch.call(textRange, "Select");
Dispatch.put(textRange, "Text", value);
}
- 修改文字顏色
/***
*
* @Title: addTextCloc
* @Description: 設置字體顏色
* @param selectionObj
* @param color
* @throws Exception
* void 返回類型
* @throws
*/
public void addTextColor(Dispatch selectionObj,String color) throws Exception {
Dispatch shapeRange = (Dispatch) Dispatch.get(selectionObj, "ShapeRange").getDispatch();
Dispatch textFrame = (Dispatch) Dispatch.get(shapeRange, "TextFrame").getDispatch();
Dispatch textRange = (Dispatch) Dispatch.get(textFrame, "TextRange").getDispatch();
Dispatch Font = (Dispatch) Dispatch.get(textRange, "Font").getDispatch();
Dispatch Color = (Dispatch) Dispatch.get(Font, "Color").getDispatch();
Dispatch.put(Color, "RGB",color);
}
- 添加背景圖片
//后面參數(shù)值依次是 left top whith hight
Dispatch picture = Dispatch.call(shapes, "AddPicture","圖片路徑", 1, 1, 1, 1).toDispatch();
// 選中圖片
Dispatch.call(picture, "Select");
//設置寬度高度
//Dispatch.put(picture, "Width", new Variant(1920));
//Dispatch.put(picture, "Height", new Variant(1200));
//設置圖片的層級關(guān)系
Dispatch.call(picture,"ZOrder",1);
總結(jié)
- 有上面的代碼可知
修改字體顏色的代碼是
Dispatch shapeRange = (Dispatch) Dispatch.get(selectionObj, "ShapeRange").getDispatch();
Dispatch textFrame = (Dispatch) Dispatch.get(shapeRange, "TextFrame").getDispatch();
Dispatch textRange = (Dispatch) Dispatch.get(textFrame, "TextRange").getDispatch();
Dispatch Font = (Dispatch) Dispatch.get(textRange, "Font").getDispatch();
Dispatch Color = (Dispatch) Dispatch.get(Font, "Color").getDispatch();
Dispatch.put(Color, "RGB",color);
有 ShapeRange 對象得到 TextFrame,再由 TextFrame 得到 TextRange 對象再得到 Font 對象
與之對應的ppt宏的VBA代碼是
Sub Macro1()
' 宏由 User 記錄,日期: 2018-7-2
ActiveWindow.Selection.TextRange.Font.Color.SchemeColor = ppAccent3
End Sub
通過觀察對比JAVA和VBA代碼得知(是的...不會 vba 全程用猜):
ActiveWindow.Selection
等同于
(Dispatch) Dispatch.get(selectionObj, "ShapeRange").getDispatch()
- &&&
Selection.TextRange
等同于
Dispatch textRange = (Dispatch) Dispatch.get(textFrame, "TextRange").getDispatch();
由觀察得志,JACOB 的 Dispath 對象可以抽象理解為 VBA 的對象,而 Dispath 的 get() 方法的意思是通過傳入的第一個參數(shù)得到 VBA 意義上的和 get() 方法第二個字符串參數(shù)值匹配的 VBA 對象,那么知道這個之后大部分的 VBA 代碼都是可以用 Dispath對象的get()方法翻譯。
后記
綜上所述~VBA 是可以翻譯為 JAVA 代碼的,(累死個仙人
值得一提的是 :在顏色的選擇方面,一般來說顏色都是通過 RGB 或者是 16 進制來確定的。但是 jacob 在設置字體顏色的時候傳入的值是一個由三個","分隔的數(shù)字。最后發(fā)現(xiàn)一條線索是把 16 進制的顏色數(shù)字轉(zhuǎn)化為 10 進制,之后兩位一組","隔開傳入.....最后試了試還是不行.....但是有些是可以,現(xiàn)在能確定的顏色是:
Dispatch.put(font, "Color", "16,77,72,15");//白色字體
Dispatch.put(font, "Color", "1,0,0,0");//紅色字體