`
jacky
  • 浏览: 25355 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java操作visio文档

    博客分类:
  • java
阅读更多

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

这里同大家分享一下.

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

     

     
  87.      * for example :  
  88.      * 

     

     
  89.      * double pageWidth = getCells(bts,"PageWidth").getResultIU();  
  90.      *   
  91.      * @param master  
  92.      * @param localeSpecificCellName  
  93.      * @return  
  94.      */  
  95.     public IVCell getCells(IVMaster master, String localeSpecificCellName) {   
  96.         return master.getPageSheet().getCells(localeSpecificCellName);   
  97.     }   
  98.   
  99.     /**  
  100.      * 画模具  
  101.      *   
  102.      * @param document  
  103.      *            文档  
  104.      * @param master  
  105.      *            模具  
  106.      * @param xPos  
  107.      *            x坐标  
  108.      * @param yPos  
  109.      *            y坐标  
  110.      * @return  
  111.      */  
  112.     public IVShape drop(IVDocument document, IVMaster master, double xPos,   
  113.             double yPos) {   
  114.         IVPage tpage = document.getPages().getItem(new Integer(1));   
  115.         return tpage.Drop(master.getDispatch(), xPos, yPos);   
  116.     }   
  117.   
  118.     /**  
  119.      * 画折线  
  120.      *   
  121.      * @param document 目标document  
  122.      * @param fromShape 起点的模具  
  123.      * @param fromPointName 起点的名称  
  124.      * @param toShape 目标点的模具  
  125.      * @param toPointName 目标点的名称  
  126.      * @param connectLine 线模具  
  127.      * @param needTab   
  128.      */  
  129.     public void visioDrawOrthLine(IVDocument document, IVShape fromShape,   
  130.             String fromPointName, IVShape toShape, String toPointName,   
  131.             IVShape connectLine, boolean needTab) {   
  132.   
  133.         // 要连线的起点   
  134.         IVCell fromCell = fromShape.getCells(fromPointName);   
  135.   
  136.         // 要连线的终点   
  137.         IVCell toCell = toShape.getCells(toPointName);   
  138.   
  139.         // 线的起终点   
  140.         IVCell beginOfLine = connectLine.getCells("BeginX");   
  141.         IVCell endOfLine = connectLine.getCells("EndX");   
  142.   
  143.         // 连接   
  144.         beginOfLine.GlueTo(fromCell);   
  145.         endOfLine.GlueTo(toCell);   
  146.   
  147.         if (needTab) {   
  148.             IVCell x2 = connectLine.getCells("Geometry1.X2");   
  149.             double k = x2.getResultIU();   
  150.             String v = String.valueOf(k * 2);   
  151.             x2.setFormulaU(v);   
  152.             connectLine.getCells("Geometry1.X3").setFormulaU(v);   
  153.         }   
  154.     }   
  155.   
  156.     /**  
  157.      * 标注文字  
  158.      *   
  159.      * @param document  
  160.      * @param text  
  161.      *            标注的文字  
  162.      * @param rectangle  
  163.      *            矩形  
  164.      * @param vertAlign  
  165.      *            1表示yes,0表示no  
  166.      * @param horzAlign  
  167.      *            1表示yes,0表示no  
  168.      * @param textColor  
  169.      *            "RGB(128,32,64)"  
  170.      */  
  171.     public void visioDrawText(IVDocument document, String text,   
  172.             Rectangle rectangle, int vertAlign, int horzAlign, String textColor) {   
  173.   
  174.         IVPage page = document.getPages().getItem(new Integer(1));   
  175.         IVShape textShape = page.DrawRectangle(rectangle.getX1(), rectangle   
  176.                 .getY1(), rectangle.getX2(), rectangle.getY2());   
  177.         // some properties:   
  178.         // 字体大小   
  179.         IVCell cell = textShape.getCells("Char.Size");   
  180.         if (cell != null)   
  181.             cell.setFormulaU("8pt");   
  182.   
  183.         // 垂直居中   
  184.         cell = textShape.getCells("VerticalAlign");   
  185.         if (cell != null)   
  186.             cell.setFormulaU(String.valueOf(vertAlign));   
  187.   
  188.         // Para.HorzAlign   
  189.         cell = textShape.getCells("Para.HorzAlign");   
  190.         if (cell != null)   
  191.             cell.setFormulaU(String.valueOf(horzAlign));   
  192.   
  193.         // text color   
  194.         cell = textShape.getCells("Char.Color");   
  195.         if (cell != null)   
  196.             cell.setFormulaU(textColor);   
  197.   
  198.         textShape.setText(text);   
  199.   
  200.     }  

 

使用示例:

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

 

 

分享到:
评论
1 楼 justinWing 2007-09-08  
javacom,jacob 在哪儿下载阿?给个连接吧

相关推荐

Global site tag (gtag.js) - Google Analytics