---
title: "Java操作visio文档"
author: "jacky"
date: "2007-08-25T14:53:07Z"
tags: []
categories: ["编程语言"]
slug: "java-visio"
draft: false
lastmod: "2007-08-25T14:53:07Z"
id: 116640
url: "https://jacky.iteye.com/blog/116640"
---

最近写了个java操作visio文档的小工具.使用了javacom & jacob,参考了c++操作visio的com技术,并请教了javacom的作者Miika.

这里同大家分享一下.

java 代码

001. /\**  
      * @author:Jacky Huang  
      * @date 2007-8-23  
      \*/
002. public class JVisio {
003. private static Log log = LogFactory.getLog(JVisio.class);
004. /\**
005. * Visio 程序
006. \*/
007. IVApplication visioApp = null;
008. /\**
009. \*
010. * 默认visio可见
011. \*
012. \*/
013. public JVisio() {
014. this(true);
015. }
016. /\**
017. \*
018. * @param visible
019. \*/
020. public JVisio(boolean visible) {
021. this.visioApp = new IVApplication();
022. this.visioApp.setVisible(visible);
023. }
024. /\**
025. * 退出
026. \*
027. \*/
028. public void quit() {
029. this.visioApp.Quit();
030. }
031. /\**
032. * 打开文档
033. \*
034. * @param visioFile
035. *            visio file name.
036. * @return
037. \*/
038. public IVDocument addDocument(String visioFile) {
039. IVDocument doc = null;
040. try {
041. doc = this.visioApp.getDocuments().Add(visioFile);
042. } catch (Exception ex) {
043. log.error("Error of open the file '" + visioFile + "'!");
044. ex.printStackTrace();
045. }
046. return doc;
047. }
048. /\**
049. * 文件另存为
050. \*
051. * @param document
052. * @param distFile
053. *            这里的路径分隔符一定是要是 \\\\,例如E:\\\\workspace\\\\jvisio\\\\test\\\\tt\_out.vsd
054. * @return
055. \*/
056. public short saveAs(IVDocument document, String distFile) {
057. return document.SaveAs(distFile);
058. }
059. /\**
060. * 获取visio文档里的一个master
061. \*
062. * @param document
063. *            文档
064. * @param masterNameUIDOrIndex
065. *            master的索引或者名称
066. * @return
067. \*/
068. public IVMaster getMaster(IVDocument document, String masterNameUIDOrIndex) {
069. IVMasters masters = document.getPages().getItem(new Integer(1))
070. .getDocument().getMasters();
071. IVMaster master = masters.getItem(masterNameUIDOrIndex);
072. log.info("Get the master :"
073. + (master == null ? null : master.getName()));
074. return master;
075. }
076. /\**
077. * 获取单元格
078. \*
079. * for example :
080. \*
081. * double pageWidth = getCells(bts,"PageWidth").getResultIU();
082. \*
083. * @param master
084. * @param localeSpecificCellName
085. * @return
086. \*/
087. public IVCell getCells(IVMaster master, String localeSpecificCellName) {
088. return master.getPageSheet().getCells(localeSpecificCellName);
089. }
090. /\**
091. * 画模具
092. \*
093. * @param document
094. *            文档
095. * @param master
096. *            模具
097. * @param xPos
098. *            x坐标
099. * @param yPos
100. *            y坐标
101. * @return
102. \*/
103. public IVShape drop(IVDocument document, IVMaster master, double xPos,
104. double yPos) {
105. IVPage tpage = document.getPages().getItem(new Integer(1));
106. return tpage.Drop(master.getDispatch(), xPos, yPos);
107. }
108. /\**
109. * 画折线
110. \*
111. * @param document 目标document
112. * @param fromShape 起点的模具
113. * @param fromPointName 起点的名称
114. * @param toShape 目标点的模具
115. * @param toPointName 目标点的名称
116. * @param connectLine 线模具
117. * @param needTab
118. \*/
119. public void visioDrawOrthLine(IVDocument document, IVShape fromShape,
120. String fromPointName, IVShape toShape, String toPointName,
121. IVShape connectLine, boolean needTab) {
122. // 要连线的起点
123. IVCell fromCell = fromShape.getCells(fromPointName);
124. // 要连线的终点
125. IVCell toCell = toShape.getCells(toPointName);
126. // 线的起终点
127. IVCell beginOfLine = connectLine.getCells("BeginX");
128. IVCell endOfLine = connectLine.getCells("EndX");
129. // 连接
130. beginOfLine.GlueTo(fromCell);
131. endOfLine.GlueTo(toCell);
132. if (needTab) {
133. IVCell x2 = connectLine.getCells("Geometry1.X2");
134. double k = x2.getResultIU();
135. String v = String.valueOf(k * 2);
136. x2.setFormulaU(v);
137. connectLine.getCells("Geometry1.X3").setFormulaU(v);
138. }
139. }
140. /\**
141. * 标注文字
142. \*
143. * @param document
144. * @param text
145. *            标注的文字
146. * @param rectangle
147. *            矩形
148. * @param vertAlign
149. *            1表示yes,0表示no
150. * @param horzAlign
151. *            1表示yes,0表示no
152. * @param textColor
153. *            "RGB(128,32,64)"
154. \*/
155. public void visioDrawText(IVDocument document, String text,
156. Rectangle rectangle, int vertAlign, int horzAlign, String textColor) {
157. IVPage page = document.getPages().getItem(new Integer(1));
158. IVShape textShape = page.DrawRectangle(rectangle.getX1(), rectangle
159. .getY1(), rectangle.getX2(), rectangle.getY2());
160. // some properties:
161. // 字体大小
162. IVCell cell = textShape.getCells("Char.Size");
163. if (cell != null)
164. cell.setFormulaU("8pt");
165. // 垂直居中
166. cell = textShape.getCells("VerticalAlign");
167. if (cell != null)
168. cell.setFormulaU(String.valueOf(vertAlign));
169. // Para.HorzAlign
170. cell = textShape.getCells("Para.HorzAlign");
171. if (cell != null)
172. cell.setFormulaU(String.valueOf(horzAlign));
173. // text color
174. cell = textShape.getCells("Char.Color");
175. if (cell != null)
176. cell.setFormulaU(textColor);
177. textShape.setText(text);
178. }

 

使用示例:

java 代码

01. public class Demo {
02. private static Log log = LogFactory.getLog(Demo.class);
03. public static void main(String\[] args) {
04. JVisio visio = new JVisio();
05. String basedir = "E:\\\\workspace\\\\jvisio\\\\test\\\\";
06. try {
07. // 打开模具
08. IVDocument model = visio.addDocument(basedir + "model.vss");
09. // 打开模板
10. IVDocument template = visio.addDocument(basedir + "template.vst");
11. // 获取bts模型
12. IVMaster bts = visio.getMaster(model, "BTS");
13. IVMaster gfq = visio.getMaster(model, "功分器");
14. log.info(bts.getName());
15. log.info(gfq.getName());
16. // 标注文字
17. /*
18. * Rectangle rectangle = new Rectangle(0, 5, 2, 7);
19. * visio.visioDrawText(template, "哈哈", rectangle, 0, 0,
20. * "RGB(128,32,64)");
21. \*/
22. // 连线
23. IVShape btsShape = visio.drop(template, bts, 1, 5);
24. IVShape gfqShape = visio.drop(template, gfq, 2, 7);
25. IVMaster line = visio.getMaster(model, "1/2馈线");
26. IVShape lineShape = visio.drop(template, line, 1, 1);
27. visio.visioDrawOrthLine(template, btsShape, "Connections.X1",
28. gfqShape, "Connections.X1", lineShape, false);
29. // 另存为
30. visio.saveAs(template, basedir + "out.vsd");
31. } catch (Exception ex) {// 捕捉Runtime Exception,并关闭visio.
32. visio.quit();
33. }
34. }
35. }