时间:2025-07-28 14:25
人气:
作者:admin

在某些情况下,您可能需要从 Visio 图表中读取形状数据。当图表包含元数据时,这通常会很有帮助。您可以在不使用 Microsoft Visio 或 Office Interop 的情况下使用 Java 提取这些信息。它非常适合用于报表、数据检查或自动化工具。在本文中,我们将向您展示如何借助图表控件Aspose.Diagram,使用 Java 以清晰简单的方式读取形状数据。
Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商慧都科技
在本文中,我们将使用Aspose.Diagram for Java从 Visio 文件中读取形状数据。该 API 提供对形状、页面和自定义属性的完全访问权限。它兼容 VSDX、VSD 和其他格式。
您可以在任何 Java 项目中使用此库。它易于设置,并支持大型图表和批处理。如果您的应用需要处理 Visio 文件,那么此 SDK 是一个很棒的工具。
请从发行版下载该库并将 JAR 添加到您的项目或通过Maven安装:
Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商慧都科技
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-diagram</artifactId>
<version>25.7</version>
</dependency>
按照以下步骤从 Visio 文件读取形状数据:
下面是一个 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
您可以通过形状名称读取其属性。只需按照以下步骤操作:
下面是 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 代码展示了如何在 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官方授权代理商慧都科技