Java操作visio文档

最近写了个java操作visio文档的小工具.使用了javacom & jacob,参考了c++操作visio的com技术,并请教了javacom的作者Miika.
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);

}

使用示例:

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) {// 捕捉Runtime Exception,并关闭visio.
visio.quit();
}

}

}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值