最近写了个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();
}
}
}
分享到:
相关推荐
通过java操作visio的方式:开源com4j
支持读写Visio文件,含demo,模具模板等文件必须是自己本机有的(可以到Visio安装目录去查找类似文件)VisioMain是读 Demo是创建写
Java二次开发Visio
Java主流的Native调用库,其中的args4j-2.0.1.jar,tlbimp.jar,com4j.jar包可以用于处理visio文档,还可以和word,excel交互.
2、cmd进入JDK安装目录,运行 java -jar tlbimp....在JDK安装目录出现一套操作visio的类库(可以不做,资源包括生成的类库,test文件夹就是) 3、把args4j-2.0.1.jar加入到你的项目里。运行VisioMain.java类就成功了
做项目时实现的功能,客户要求上传visio文件后,把visio中的图片读取出来,然后用图片展示出来,这个功能就应运而生了。 PS:包中附有jcom.dll文件,这个是关键,需要将jcom.dll文件放到服务器的bin目录下
java使用Com4j读取visio的文档说明,文档属个人撰写,表达能力有限,如有描述不清,敬请谅解 压缩包内容:个人撰写文档、项目实例、完整的com4j包
关于Visio使用技巧的文章和书籍已经大量存在了,但是对于如何编程处理Visio文档,现存资料较少。为了弥补这一缺失,本文对于编程处理Visio文档进行了研究和整理。文章首先介绍了Visio编程的相关基础知识,这些知识...
Java软件开发流程图Visio 2013.vsdx 文档管理软件、自动化打包软件、禅道、svn、maven等都在流程里面有规划
此资源为viso文档,主要为Java 输入与输出的UML类,包括:输入流与输出流的层次结构、Reader和Writer的层次结构、Closeable、Flushable、Readable和Appendable接口。
java使用Com4j读取visio的文档说明,并附上实例,文档属个人撰写,表达能力有限,如有描述不清,敬请谅解
将visio画的图转化为pdf格式的文件,以供latex使用所需的软件。
1.数据库课程设计(内包含代码、doc文档、E-R图流程图的visio文件等) 2.技术栈是java+mysql,有GUI窗口,内容很完整,先在Navicat或者IDEA里运行StudentSystemDao.sql文件建库,然后把后端的代码运行一下,连接...
使开发人员可以对 VSD, VSDX, VSS, VST, VSX, VTX, VDW and VDX文件进行打开、操作和导出的控件,在ASP .NET网页应用程序、web服务器和Windows应用程序上的VSD和VDX 文件中,开发者都可以以使用
附件是java poi 3.17的lib库及其依赖库,全部下载引用后,可以进行excel的读写操作,无需引用其他lib库。poi-3.17适用于jdk1.7。 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft ...
com4j基础开发包,用于生成visio文件
这是一个用Java程序读写操作Excel文件的例子。 例子用到了Apache POI项目...Apache POI项目中还提供了Java操作Word、PPT、Visio等类型文件的方法。 这个例子是我的一个叫姚楠学生写的,现在拿出来供大家分享
Apache POI包含类和方法,来将MS Office所有OLE 2文档复合。...HDGF (可怕的图表格式) : 它包含类和方法为MS-Visio的二进制文件。 HPBF (可怕的出版商格式) : 它被用来读取和写入MS-Publisher文件。