直接从 Java 代码创建 Visio 图表可以简化文档工作流并消除手动绘图的工作量。 Conholdate.Total for Java 提供了强大的 SDK,使您能够以编程方式生成 Visio 文件。在本指南中,我们将在 Java 中创建 Visio 图表,添加来自母版模板的形状,定位它,并应用文本样式。完成后,您将拥有一个可直接打开的 VSDX 文件,可在 Microsoft Visio 中进一步编辑。
Visio 图表创建要求
企业经常需要从数据源自动生成技术示意图、流程图或网络拓扑图。构建报表仪表板、配置工具或 CI 流水线的 Java 开发人员经常面临无需人工干预即可生成 Visio 文件的需求。解决方案必须支持基于母版的形状创建、精确定位以及与现有 Visio 模板匹配的文本样式。手动在 Visio 中绘图既耗时又容易出错,尤其是在每次构建时都必须重新生成图表时。
方法:使用 Conholdate.Total 生成 Visio 图表
Conholdate.Total for Java 提供了专用的图表 API,支持 VSDX 格式。它允许您加载主模板,从这些模板中添加形状,通过其 XForm 属性重新定位形状,附加文本,并应用预定义的文本样式,所有操作均通过简单的 Java 调用完成。图表类的文档可在官方文档中获取,完整的 API 参考可在API 参考查看。使用此库,您可以在服务器或桌面环境中自动化整个 Visio 创建流程。
构建解决方案:在 Java 中创建 Visio 图表
以下步骤将带您完成整个过程,从 Maven 设置到保存最终的 VSDX 文件。
添加 Maven 依赖并导入类
首先,配置 Conholdate Maven 仓库并将 SDK 依赖添加到您的 pom.xml 中。
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
本教程中使用的图表类位于 com.aspose.diagram 包中,该包作为 Conholdate.Total for Java 捆绑的一部分提供:
import com.aspose.diagram.Diagram;
import com.aspose.diagram.Shape;
import com.aspose.diagram.TypeValue;
import com.aspose.diagram.Txt;
import com.aspose.diagram.SaveFileFormat;
初始化图表并加载主形状
创建一个新的 Diagram 实例,然后加载一个模板文件并选择您想要作为形状基础的母版。在这里,我们使用内置的 “Rectangle” 母版,来自 “Basic Shapes.vss” 模板。
// Create a new instance of a diagram
Diagram diagram = new Diagram();
// Define the name of the master (template) to be used for creating shapes
String masterName = "Rectangle";
diagram.addMaster("Basic Shapes.vss", masterName);
从母版添加形状并设置其位置
使用母版向图表添加形状,然后通过 addShape 返回的 ID 查找该形状,以便调整其位置。
// Define the dimensions and position for the new shape
double width = 2, height = 2, pinX = 4.25, pinY = 4.5;
// Add a new rectangle shape to the diagram using the specified master
long rectangleId = diagram.addShape(pinX, pinY, width, height, masterName, 0);
// Retrieve the shape by its ID for modification
Shape rectangle = diagram.getPages().get(0).getShapes().getShape(rectangleId);
// Set the position of the shape by modifying its PinX and PinY properties
rectangle.getXForm().getPinX().setValue(5);
rectangle.getXForm().getPinY().setValue(5);
// Set the type of the shape to indicate it is a standard shape
rectangle.setType(TypeValue.SHAPE);
添加文本并应用文本样式
将文本运行附加到形状,并应用图表的预定义样式表之一,使标签与文档的其余部分保持一致。
// Add text to the shape
rectangle.getText().getValue().add(new Txt("Aspose Diagram"));
// Apply a predefined text style to the shape's text
rectangle.setTextStyle(diagram.getStyleSheets().get(3));
将图表保存为 VSDX
最后,将图表写入可在 Microsoft Visio 中打开的 VSDX 文件。
diagram.save("Visio_out.vsdx", SaveFileFormat.VSDX);
在 Java 中创建 Visio 图表 - 完整工作示例 - 完整代码示例
以下代码演示了从头到尾的完整工作流。
import com.aspose.diagram.Diagram;
import com.aspose.diagram.Shape;
import com.aspose.diagram.TypeValue;
import com.aspose.diagram.Txt;
import com.aspose.diagram.SaveFileFormat;
public class CreateVisioDiagram {
public static void main(String[] args) {
try {
// Create a new instance of a diagram
Diagram diagram = new Diagram();
// Define the name of the master (template) to be used for creating shapes
String masterName = "Rectangle";
diagram.addMaster("Basic Shapes.vss", masterName);
// Define the dimensions and position for the new shape
double width = 2, height = 2, pinX = 4.25, pinY = 4.5;
// Add a new rectangle shape to the diagram using the specified master
long rectangleId = diagram.addShape(pinX, pinY, width, height, masterName, 0);
// Retrieve the shape by its ID for modification
Shape rectangle = diagram.getPages().get(0).getShapes().getShape(rectangleId);
// Set the position of the shape by modifying its PinX and PinY properties
rectangle.getXForm().getPinX().setValue(5);
rectangle.getXForm().getPinY().setValue(5);
// Set the type of the shape to indicate it is a standard shape
rectangle.setType(TypeValue.SHAPE);
// Add text to the shape
rectangle.getText().getValue().add(new Txt("Aspose Diagram"));
// Apply a predefined text style to the shape's text
rectangle.setTextStyle(diagram.getStyleSheets().get(3));
// Save the modified diagram to a file
diagram.save("Visio_out.vsdx", SaveFileFormat.VSDX);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: 这段代码示例演示了核心功能。在将其用于项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中进行彻底测试。如果遇到任何问题,请参考官方文档或联系支持团队获取帮助。
部署 Visio 图表项目的注意事项
SDK 可在任何 Java 8+ 运行时上运行,适用于后端服务、批处理作业或桌面实用程序。生成大型图表时,请监控堆内存使用情况,并考虑复用单个 Diagram 实例以在多个页面之间降低内存开销。生产环境需要有效的许可证;您可以从临时许可证页面获取用于测试的临时许可证,并通过定价页面购买完整许可证。确保输出目录对执行代码的进程具有写入权限。
结论
使用 Conholdate.Total for Java 在 Java 中以编程方式创建 Visio 图表变得简单。按照上述步骤,您可以加载主模板,生成 VSDX 文件,精确定位形状,并应用一致的文本样式。请记得获取适当的正式许可证,并在 Microsoft Visio 中测试生成的图表以验证布局的准确性。拥有 SDK 后,您可以在广泛的企业场景中自动化图表创建。
常见问题
如何在 Java 中使用主模板创建 Visio 图表?
使用由 Conholdate.Total for Java 提供的图表类,使用addMaster加载模板,使用addShape从该主模板添加形状,并将文件保存为 VSDX。SDK 为 Visio 图表生成哪种文件格式?
SDK 输出现代的 VSDX 格式,完全兼容 Microsoft Visio.我是否需要许可证才能在生产环境中运行生成的 Visio 文件?
是的。请从定价页面获取生产许可证,您也可以在开发期间使用临时许可证。我可以将此图表生成集成到 Web 服务中吗?
当然可以。SDK 是一个纯 Java 库,因此您可以在任何基于 Java 的后端(如 Spring Boot 或 Jakarta EE)中调用它。
