首页 > 基础资料 博客日记
图表控件Aspose.Diagram教程:使用 Java 读取 Visio 形状数据
2025-07-28 14:30:02基础资料围观132次
在某些情况下,您可能需要从 Visio 图表中读取形状数据。当图表包含元数据时,这通常会很有帮助。您可以在不使用 Microsoft Visio 或 Office Interop 的情况下使用 Java 提取这些信息。它非常适合用于报表、数据检查或自动化工具。在本文中,我们将向您展示如何借助图表控件Aspose.Diagram,使用 Java 以清晰简单的方式读取形状数据。
Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商慧都科技
加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。
Java Visio 库读取形状数据
在本文中,我们将使用Aspose.Diagram for Java从 Visio 文件中读取形状数据。该 API 提供对形状、页面和自定义属性的完全访问权限。它兼容 VSDX、VSD 和其他格式。
您可以在任何 Java 项目中使用此库。它易于设置,并支持大型图表和批处理。如果您的应用需要处理 Visio 文件,那么此 SDK 是一个很棒的工具。
请从发行版下载该库并将 JAR 添加到您的项目或通过Maven安装:
Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商慧都科技
加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-diagram</artifactId>
<version>25.7</version>
</dependency>
如何在 Java 中读取 Visio 形状数据
按照以下步骤从 Visio 文件读取形状数据:
- 使用该类加载文件Diagram。
- 从图表中获取一页。
- 循环遍历页面上的形状。
- 打印形状 ID 和名称。
下面是一个 Java 代码片段,演示了如何在 Java 中读取 Visio 形状数据:
// Load the Visio file
Diagram diagram = new Diagram("Drawing1.vsdx");
// get pages count
System.out.println("Total Pages:" + diagram.getPages().getCount());
// Access the first page
Page page = diagram.getPages().get(0);
// Iterate through shapes
for (Shape shape : (Iterable<Shape>) page.getShapes()) {
System.out.println("Shape ID: " + shape.getID());
System.out.println("Name: " + shape.getName());
}
Total Pages: 1
Shape ID: 1
Name: Square
Shape ID: 2
Name: Rectangle
Shape ID: 3
Name: Dynamic connector
通过名称读取形状属性
您可以通过形状名称读取其属性。只需按照以下步骤操作:
- 加载图表文件。
- 访问页面。
- 通过名称查找形状。
- 用于Props读取属性。
下面是 Java 代码示例,演示了如何使用形状名称读取形状的属性:
// Load the Visio file
Diagram diagram = new Diagram("Drawing1.vsdx");
// Access the first page
Page page = diagram.getPages().get(0);
// Iterate through shapes
for (Shape shape : (Iterable<Shape>) page.getShapes()) {
// Read shape propert by name
if ("Process".equals(shape.getName())) {
for (Prop prop : (Iterable<Prop>) shape.getProps()) {
System.out.println("Property Name: " + prop.getLabel().getValue());
System.out.println("Value: " + prop.getValue().getVal());
}
}
}
读取 Java 中 Shape 的 InheritProps
您还可以按照以下步骤获取继承的形状属性:
- 打开 Visio 文件。
- 获取页面并循环浏览形状。
- 用于InheritProps读取继承的值。
以下示例 Java 代码展示了如何在 Java 中读取 Visio 形状的 InheritProps:
// Load the Visio file
Diagram diagram = new Diagram("Drawing1.vsdx");
// Access the first page
Page page = diagram.getPages().get(0);
// Iterate through shapes InheritProps
for (Shape shape : (Iterable<Shape>) page.getShapes()) {
for (Prop prop : (Iterable<Prop>) shape.getInheritProps()) {
System.out.println("Inherited Name: " + prop.getLabel().getValue());
System.out.println("Value: " + prop.getValue().getVal());
}
}
结论
在本文中,您学习了如何使用 Aspose.Diagram 在 Java 中读取 Visio 形状数据。您了解了如何加载文件、提取形状属性以及读取继承的数据。借助这个强大的 API,您可以轻松构建处理 Visio 文件的 Java 应用。
Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商慧都科技
加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
上一篇:Spring AI 框架中如何集成 MCP?
下一篇:没有了