import java.io.File;java
import com.jacob.activeX.*;session
import com.jacob.com.Dispatch;svg
import com.jacob.com.Variant;函數
public class VisioExtractor {ui
// 建立一個組件。spa
private ActiveXComponent msVisioApp = null;.net
// 整個模板對象
private Dispatch document = null;圖片
// 選中的控件get
private Dispatch session = null;
// 構造函數
VisioExtractor() {
super();
}
/**
* 開啓visio檔案
*
* @param makeVisible
* 顯示或是不顯示(true:顯示;false:不顯示)
*/
public void openVisio(boolean makeVisible) {
try {
if (msVisioApp == null) {
msVisioApp = new ActiveXComponent("Visio.Application");
}
Dispatch.put(msVisioApp, "Visible", new Variant(makeVisible));
} catch (RuntimeException e) {
e.printStackTrace();
}
}
/**
* 創建viso的文本內容
*
*/
public void createNewDocument() {
// 獲得
Dispatch documents = msVisioApp.getProperty("Documents").toDispatch();
// 添加 document world 能夠 , visio 不能夠 多是缺乏參數
Dispatch document = Dispatch.call(documents, "add").toDispatch();
// 添加 page頁籤
Dispatch page = Dispatch.call(document, "add").toDispatch();
// 添加 頁籤選中 當前頁籤
Dispatch.call(page, "Select");
}
public void openDocument(String _filePath) {
Dispatch documents = Dispatch.get(msVisioApp, "Documents").toDispatch();
// 打開viso文件
try {
document = Dispatch.call(documents, "Open", _filePath).toDispatch();
} catch (Exception e) {
e.printStackTrace();
}
// 獲得全部的Pages
Dispatch pages = Dispatch.get(document, "Pages").toDispatch();
}
/**
* 添加一個頁籤
*/
public void addPage() {
Dispatch pages = Dispatch.get(document, "Pages").toDispatch();
session = Dispatch.call(pages, "add").toDispatch();
// Dispatch.call(session, "Name", "XXX流程"); // 寫入標題內容 // 標題格行
}
/**
* 得到頁籤集合 說明:
*
* @return
* @throws Exception
* 建立時間:2011-6-4 下午05:33:07
*/
public void getShapes(Dispatch page) {
// Shapes/Shape
Dispatch vshapes = Dispatch.get(page, "Shapes").toDispatch();
// 獲得Shapes的數量
int pagesCount = Integer.parseInt(Dispatch.get(vshapes, "Count")
.toString());
for (int i = 1; i <= pagesCount; i++) {
Dispatch Shape = Dispatch.call(vshapes, "Item", new Variant(i))
.toDispatch();
String shapeid = Dispatch.get(Shape, "Id").toString();
String shapetype = Dispatch.get(Shape, "Type").toString();
String shapetext = Dispatch.get(Shape, "Text").toString();
// String shapename = Dispatch.get(Shape, "Name").toString() ;
// String shapenameU = Dispatch.get(Shape, "NameU").toString() ;
// String shapelineStyle = Dispatch.get(Shape,
// "LineStyle").toString() ;
// String shapefillStyle = Dispatch.get(Shape,
// "FillStyle").toString() ;
// String shapetextStyle = Dispatch.get(Shape,
// "TextStyle").toString() ;
System.out.print(" " + i + "shape id:" + shapeid);
System.out.print(" " + i + "shape type:" + shapetype);
System.out.print(" " + i + "shape text:" + shapetext);
System.out.println();
}
}
public void documentToString() {
Dispatch pages = Dispatch.get(document, "Pages").toDispatch();
// 獲得Page的數量
int pagesCount = Integer.parseInt(Dispatch.get(pages, "Count")
.toString());
System.out.println("圖片數量:" + pagesCount);
// 循環獲得每一個Page
for (int i = 1; i <= pagesCount; i++) {
Dispatch page = Dispatch.call(pages, "Item", new Variant(i))
.toDispatch();
String pageid = Dispatch.get(page, "Id").toString();
String pagename = Dispatch.get(page, "Name").toString();
String pagenameU = Dispatch.get(page, "NameU").toString();
System.out.print(i + " page id:" + pageid);
System.out.print(i + " page name:" + pagename);
System.out.print(i + " page nameU:" + pagenameU);
getShapes(page);
}
}
/**
* 另存爲
*
* @param type
*/
public void savePageAs(String outFilePath, String type) {
Dispatch pages = Dispatch.get(document, "Pages").toDispatch();
// 獲得Page的數量
int pagesCount = Integer.parseInt(Dispatch.get(pages, "Count")
.toString());
System.out.println("圖片數量:" + pagesCount);
// 循環獲得每一個Page
for (int i = 1; i <= pagesCount; i++) {
Dispatch page = Dispatch.call(pages, "Item", new Variant(i))
.toDispatch();
getShapes(page);
// 輸出Page的名稱
String pageName = Dispatch.get(page, "Name").toString();
if ("jpg".equals(type)) {
// 將該Page保存爲圖片
Dispatch.call(page, "Export", new Object[] { outFilePath + "//"
+ i + "_" + pageName + ".jpg" });
} else if ("vdx".equals(type)) {
// 將該Page保存爲圖片 vdx
Dispatch.call(page, "Export", new Object[] { outFilePath + "//"
+ i + "_" + pageName + ".vdx" });
} else if ("svg".equals(type)) {
// 將該Page保存爲svg
Dispatch.call(page, "Export", new Object[] { outFilePath + "//"
+ i + "_" + pageName + ".svg" });
} else {
}
}
}
/**
* 關閉文本內容(若是未開啓visio編輯時,釋放ActiveX執行緒)
*/
public void closeDocument() {
// visio 的關閉, 沒有參數或者參數不對。 一致沒有找到不保存關閉的方法。
Dispatch.call(document, "Save");
Dispatch.call(document, "Close");
document = null;
}
/**
* 關閉visio(若是未開啓visio編輯時,釋放ActiveX執行緒)
*/
public void closeVisio() {
Dispatch.call(msVisioApp, "Quit");
msVisioApp = null;
document = null;
}
public void saveJPG(String fromFile, String toFile) throws Exception {
if (msVisioApp == null) {
msVisioApp = new ActiveXComponent("Visio.Application");
}
Dispatch.put(msVisioApp, "Visible", new Variant(false));
Dispatch documents = Dispatch.get(msVisioApp, "Documents").toDispatch();
// 打開viso文件
document = Dispatch.call(documents, "Open", fromFile).toDispatch();
Dispatch pages = Dispatch.get(document, "Pages").toDispatch();
// 獲得Page的數量
int pagesCount = Integer.parseInt(Dispatch.get(pages, "Count")
.toString());
System.out.println("圖片數量:" + pagesCount);
// 循環獲得每一個Page
for (int i = 1; i <= pagesCount; i++) {
Dispatch page = Dispatch.call(pages, "Item", new Variant(i))
.toDispatch();
getShapes(page);
// 輸出Page的名稱
String pageName = Dispatch.get(page, "Name").toString();
// 將該Page保存爲圖片
Dispatch.call(page, "Export", new Object[] { toFile + ".jpg" });
}
}
/**
* @param args
*/
public static void main(String[] args) {
String toFile = "D:"+ File.separator +"listen.jpg";
String fromFile = "D:"+ File.separator +"test.vsd";
System.out.println("path: " + toFile);
VisioExtractor visio = new VisioExtractor(); // 創建一個VisioExtractor對象
// visio.openVisio(false); // 設定Visio開啓顯示
// visio.createNewDocument();
// visio.openDocument(otFile); // 創建文件內容
// visio.addPage();
// visio.documentToString();
try {
visio.saveJPG(fromFile, toFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
visio.closeDocument();
visio.closeVisio();
}
// visio.saveFileAs(otFile, "jpg");
}
}