Java操作visio文档

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

这里同大家分享一下.

java 代码
  1.  
/**
 * @author:Jacky Huang
 * @date 2007-8-23
 */ public class JVisio {            private   static  Log log = LogFactory.getLog(JVisio. class );            /**        * Visio 程序        */       IVApplication visioApp =  null ;            /**        *         * 默认visio可见        *         */        public  JVisio() {             this ( true );        }            /**        *         * @param visible        */        public  JVisio( boolean  visible) {             this .visioApp =  new  IVApplication();             this .visioApp.setVisible(visible);        }            /**        * 退出        *         */        public   void  quit() {             this .visioApp.Quit();        }            /**        * 打开文档        *         * @param visioFile        *            visio file name.        * @return        */        public  IVDocument addDocument(String visioFile) {            IVDocument doc =  null ;             try  {                doc =  this .visioApp.getDocuments().Add(visioFile);            }  catch  (Exception ex) {                log.error( "Error of open the file '"  + visioFile +  "'!" );                ex.printStackTrace();            }             return  doc;        }            /**        * 文件另存为        *         * @param document        * @param distFile        *            这里的路径分隔符一定是要是 \\,例如E:\\workspace\\jvisio\\test\\tt_out.vsd        * @return        */        public   short  saveAs(IVDocument document, String distFile) {             return  document.SaveAs(distFile);        }            /**        * 获取visio文档里的一个master        *         * @param document        *            文档        * @param masterNameUIDOrIndex        *            master的索引或者名称        * @return        */        public  IVMaster getMaster(IVDocument document, String masterNameUIDOrIndex) {            IVMasters masters = document.getPages().getItem( new  Integer( 1 ))                    .getDocument().getMasters();            IVMaster master = masters.getItem(masterNameUIDOrIndex);            log.info( "Get the master :"                    + (master ==  null  ?  null  : master.getName()));             return  master;        }            /**        * 获取单元格        * 

 

       * for example :        * 

 

       * double pageWidth = getCells(bts,"PageWidth").getResultIU();        *         * @param master        * @param localeSpecificCellName        * @return        */        public  IVCell getCells(IVMaster master, String localeSpecificCellName) {             return  master.getPageSheet().getCells(localeSpecificCellName);        }            /**        * 画模具        *         * @param document        *            文档        * @param master        *            模具        * @param xPos        *            x坐标        * @param yPos        *            y坐标        * @return        */        public  IVShape drop(IVDocument document, IVMaster master,  double  xPos,                 double  yPos) {            IVPage tpage = document.getPages().getItem( new  Integer( 1 ));             return  tpage.Drop(master.getDispatch(), xPos, yPos);        }            /**        * 画折线        *         * @param document 目标document        * @param fromShape 起点的模具        * @param fromPointName 起点的名称        * @param toShape 目标点的模具        * @param toPointName 目标点的名称        * @param connectLine 线模具        * @param needTab         */        public   void  visioDrawOrthLine(IVDocument document, IVShape fromShape,                String fromPointName, IVShape toShape, String toPointName,                IVShape connectLine,  boolean  needTab) {                // 要连线的起点            IVCell fromCell = fromShape.getCells(fromPointName);                // 要连线的终点            IVCell toCell = toShape.getCells(toPointName);                // 线的起终点            IVCell beginOfLine = connectLine.getCells( "BeginX" );            IVCell endOfLine = connectLine.getCells( "EndX" );                // 连接            beginOfLine.GlueTo(fromCell);            endOfLine.GlueTo(toCell);                if  (needTab) {                IVCell x2 = connectLine.getCells( "Geometry1.X2" );                 double  k = x2.getResultIU();                String v = String.valueOf(k *  2 );                x2.setFormulaU(v);                connectLine.getCells( "Geometry1.X3" ).setFormulaU(v);            }        }            /**        * 标注文字        *         * @param document        * @param text        *            标注的文字        * @param rectangle        *            矩形        * @param vertAlign        *            1表示yes,0表示no        * @param horzAlign        *            1表示yes,0表示no        * @param textColor        *            "RGB(128,32,64)"        */        public   void  visioDrawText(IVDocument document, String text,                Rectangle rectangle,  int  vertAlign,  int  horzAlign, String textColor) {               IVPage page = document.getPages().getItem( new  Integer( 1 ));            IVShape textShape = page.DrawRectangle(rectangle.getX1(), rectangle                    .getY1(), rectangle.getX2(), rectangle.getY2());             // some properties:             // 字体大小            IVCell cell = textShape.getCells( "Char.Size" );             if  (cell !=  null )                cell.setFormulaU( "8pt" );                // 垂直居中            cell = textShape.getCells( "VerticalAlign" );             if  (cell !=  null )                cell.setFormulaU(String.valueOf(vertAlign));                // Para.HorzAlign            cell = textShape.getCells( "Para.HorzAlign" );             if  (cell !=  null )                cell.setFormulaU(String.valueOf(horzAlign));                // text color            cell = textShape.getCells( "Char.Color" );             if  (cell !=  null )                cell.setFormulaU(textColor);               textShape.setText(text);           }  

 

使用示例:

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. }  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值