- jacky
- 等级:
- 性别:
- 文章: 31
- 积分: 194
- 来自: 广州
|
最近写了个java操作visio文档的小工具.使用了javacom & jacob,参考了c++操作visio的com技术,并请教了javacom的作者Miika.
这里同大家分享一下.
java 代码
/**
* @author:Jacky Huang
* @date 2007-8-23
*/
- public class JVisio {
-
- private static Log log = LogFactory.getLog(JVisio.class);
-
-
-
-
- IVApplication visioApp = null;
-
-
-
-
-
-
- public JVisio() {
- this(true);
- }
-
-
-
-
-
- public JVisio(boolean visible) {
- this.visioApp = new IVApplication();
- this.visioApp.setVisible(visible);
- }
-
-
-
-
-
- public void quit() {
- this.visioApp.Quit();
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
- public short saveAs(IVDocument document, String distFile) {
- return document.SaveAs(distFile);
- }
-
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public IVCell getCells(IVMaster master, String localeSpecificCellName) {
- return master.getPageSheet().getCells(localeSpecificCellName);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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());
-
-
- IVCell cell = textShape.getCells("Char.Size");
- if (cell != null)
- cell.setFormulaU("8pt");
-
-
- cell = textShape.getCells("VerticalAlign");
- if (cell != null)
- cell.setFormulaU(String.valueOf(vertAlign));
-
-
- cell = textShape.getCells("Para.HorzAlign");
- if (cell != null)
- cell.setFormulaU(String.valueOf(horzAlign));
-
-
- cell = textShape.getCells("Char.Color");
- if (cell != null)
- cell.setFormulaU(textColor);
-
- textShape.setText(text);
-
- }
使用示例:
java 代码
- public class Demo {
-
- private static Log log = LogFactory.getLog(Demo.class);
-
- public static void main(String[] args) {
-
- JVisio visio = new JVisio();
-
- String basedir = "E:\\workspace\\jvisio\\test\\";
-
- try {
- // 打开模具
- IVDocument model = visio.addDocument(basedir + "model.vss");
- // 打开模板
- IVDocument template = visio.addDocument(basedir + "template.vst");
-
- // 获取bts模型
- IVMaster bts = visio.getMaster(model, "BTS");
- IVMaster gfq = visio.getMaster(model, "功分器");
- log.info(bts.getName());
- log.info(gfq.getName());
- // 标注文字
- /*
- * Rectangle rectangle = new Rectangle(0, 5, 2, 7);
- * visio.visioDrawText(template, "哈哈", rectangle, 0, 0,
- * "RGB(128,32,64)");
- */
-
- // 连线
- IVShape btsShape = visio.drop(template, bts, 1, 5);
- IVShape gfqShape = visio.drop(template, gfq, 2, 7);
-
- IVMaster line = visio.getMaster(model, "1/2馈线");
- IVShape lineShape = visio.drop(template, line, 1, 1);
-
- visio.visioDrawOrthLine(template, btsShape, "Connections.X1",
- gfqShape, "Connections.X1", lineShape, false);
-
- // 另存为
- visio.saveAs(template, basedir + "out.vsd");
-
- } catch (Exception ex) {
- visio.quit();
- }
-
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|