ITeye博客

Java操作visio文档

jacky · 2007-08-25 14:53 · java · 编程语言 · Markdown

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

这里同大家分享一下.

java 代码

  1. /**
     * @author:Jacky Huang
     * @date 2007-8-23
     */
  2. public class JVisio {
  3. private static Log log = LogFactory.getLog(JVisio.class);
  4. /**
  5. * Visio 程序
  6. */
  7. IVApplication visioApp = null;
  8. /**
  9. *
  10. * 默认visio可见
  11. *
  12. */
  13. public JVisio() {
  14. this(true);
  15. }
  16. /**
  17. *
  18. * @param visible
  19. */
  20. public JVisio(boolean visible) {
  21. this.visioApp = new IVApplication();
  22. this.visioApp.setVisible(visible);
  23. }
  24. /**
  25. * 退出
  26. *
  27. */
  28. public void quit() {
  29. this.visioApp.Quit();
  30. }
  31. /**
  32. * 打开文档
  33. *
  34. * @param visioFile
  35. *            visio file name.
  36. * @return
  37. */
  38. public IVDocument addDocument(String visioFile) {
  39. IVDocument doc = null;
  40. try {
  41. doc = this.visioApp.getDocuments().Add(visioFile);
  42. } catch (Exception ex) {
  43. log.error("Error of open the file '" + visioFile + "'!");
  44. ex.printStackTrace();
  45. }
  46. return doc;
  47. }
  48. /**
  49. * 文件另存为
  50. *
  51. * @param document
  52. * @param distFile
  53. *            这里的路径分隔符一定是要是 \\,例如E:\\workspace\\jvisio\\test\\tt_out.vsd
  54. * @return
  55. */
  56. public short saveAs(IVDocument document, String distFile) {
  57. return document.SaveAs(distFile);
  58. }
  59. /**
  60. * 获取visio文档里的一个master
  61. *
  62. * @param document
  63. *            文档
  64. * @param masterNameUIDOrIndex
  65. *            master的索引或者名称
  66. * @return
  67. */
  68. public IVMaster getMaster(IVDocument document, String masterNameUIDOrIndex) {
  69. IVMasters masters = document.getPages().getItem(new Integer(1))
  70. .getDocument().getMasters();
  71. IVMaster master = masters.getItem(masterNameUIDOrIndex);
  72. log.info("Get the master :"
  73. + (master == null ? null : master.getName()));
  74. return master;
  75. }
  76. /**
  77. * 获取单元格
  78. *
  79. * for example :
  80. *
  81. * double pageWidth = getCells(bts,"PageWidth").getResultIU();
  82. *
  83. * @param master
  84. * @param localeSpecificCellName
  85. * @return
  86. */
  87. public IVCell getCells(IVMaster master, String localeSpecificCellName) {
  88. return master.getPageSheet().getCells(localeSpecificCellName);
  89. }
  90. /**
  91. * 画模具
  92. *
  93. * @param document
  94. *            文档
  95. * @param master
  96. *            模具
  97. * @param xPos
  98. *            x坐标
  99. * @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 代码

  1. public class Demo {
  2. private static Log log = LogFactory.getLog(Demo.class);
  3. public static void main(String[] args) {
  4. JVisio visio = new JVisio();
  5. String basedir = "E:\\workspace\\jvisio\\test\\";
  6. try {
  7. // 打开模具
  8. IVDocument model = visio.addDocument(basedir + "model.vss");
  9. // 打开模板
  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. }

评论(1)

  1. #1 楼 · justinWing · 2007-09-08 14:29

    javacom,jacob 在哪儿下载阿?给个连接吧

评论功能维护中,当前仅展示历史评论。