查各龙 發表於 2025-7-28 14:25:00

图表控件Aspose.Diagram教程:使用 Java 读取 Visio 形状数据

<p><img src="https://image.evget.com/attachment/keditor/image/20250728/135306_0.png"></p>
<p>在某些情况下,您可能需要从 Visio 图表中读取形状数据。当图表包含元数据时,这通常会很有帮助。您可以在不使用 Microsoft Visio 或 Office Interop 的情况下使用 Java 提取这些信息。它非常适合用于报表、数据检查或自动化工具。在本文中,我们将向您展示如何借助图表控件<strong><u>Aspose.Diagram</u></strong>,使用 Java 以清晰简单的方式读取形状数据。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1); font-size: 14px"><strong>Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商<span style="color: rgba(35, 111, 161, 1)">慧都科技</span></strong></span></p>
<h2 style="text-align: center"><span style="color: rgba(230, 126, 35, 1); font-size: 14px"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></h2>
<h2 id="java-visio-library-to-read-shape-data">Java Visio 库读取形状数据</h2>
<p>在本文中,我们将使用<u><span id="__kindeditor_bookmark_start_61__"></span><strong>Aspose.Diagram</strong></u><strong><u>&nbsp;for Java</u></strong>从 Visio 文件中读取形状数据。该 API 提供对形状、页面和自定义属性的完全访问权限。它兼容 VSDX、VSD 和其他格式。</p>
<p>您可以在任何 Java 项目中使用此库。它易于设置,并支持大型图表和批处理。如果您的应用需要处理 Visio 文件,那么此 SDK 是一个很棒的工具。</p>
<p><strong><u>请从发行版</u></strong><strong><u>下载</u></strong>该库并将 JAR 添加到您的项目或通过Maven安装:</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1); font-size: 14px"><strong>Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商<span style="color: rgba(35, 111, 161, 1)">慧都科技</span></strong></span></p>
<h2 style="text-align: center"><span style="color: rgba(230, 126, 35, 1); font-size: 14px"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></h2>
<pre class="prettyprint lang-js highlighter-hljs"><code>&lt;dependency&gt;
    &lt;groupId&gt;com.aspose&lt;/groupId&gt;
    &lt;artifactId&gt;aspose-diagram&lt;/artifactId&gt;
    &lt;version&gt;25.7&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
<h2 id="how-to-read-visio-shape-data-in-java">如何在 Java 中读取 Visio 形状数据</h2>
<p>按照以下步骤从 Visio 文件读取形状数据:</p>
<ol>
<li>使用该类加载文件Diagram。</li>
<li>从图表中获取一页。</li>
<li>循环遍历页面上的形状。</li>
<li>打印形状 ID 和名称。</li>
</ol>
<p>下面是一个 Java 代码片段,演示了如何在 Java 中读取 Visio 形状数据:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>// 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&lt;Shape&gt;) page.getShapes()) {
    System.out.println("Shape ID: " + shape.getID());
    System.out.println("Name: " + shape.getName());
}</code></pre>
<pre class="prettyprint lang-js highlighter-hljs"><code>Total Pages: 1
Shape ID: 1
Name: Square
Shape ID: 2
Name: Rectangle
Shape ID: 3
Name: Dynamic connector</code></pre>
<h2 id="read-a-shape-property-by-name">通过名称读取形状属性</h2>
<p>您可以通过形状名称读取其属性。只需按照以下步骤操作:</p>
<ol>
<li>加载图表文件。</li>
<li>访问页面。</li>
<li>通过名称查找形状。</li>
<li>用于Props读取属性。</li>
</ol>
<p>下面是 Java 代码示例,演示了如何使用形状名称读取形状的属性:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>// 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&lt;Shape&gt;) page.getShapes()) {
    // Read shape propert by name
    if ("Process".equals(shape.getName())) {
      for (Prop prop : (Iterable&lt;Prop&gt;) shape.getProps()) {
            System.out.println("Property Name: " + prop.getLabel().getValue());
            System.out.println("Value: " + prop.getValue().getVal());
      }
    }
}</code></pre>
<h2 id="read-inheritprops-of-shape-in-java">读取 Java 中 Shape 的 InheritProps</h2>
<p>您还可以按照以下步骤获取继承的形状属性:</p>
<ol>
<li>打开 Visio 文件。</li>
<li>获取页面并循环浏览形状。</li>
<li>用于InheritProps读取继承的值。</li>
</ol>
<p>以下示例 Java 代码展示了如何在 Java 中读取 Visio 形状的 InheritProps:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>// 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&lt;Shape&gt;) page.getShapes()) {
    for (Prop prop : (Iterable&lt;Prop&gt;) shape.getInheritProps()) {
      System.out.println("Inherited Name: " + prop.getLabel().getValue());
      System.out.println("Value: " + prop.getValue().getVal());
    }
}</code></pre>
<h2 id="conclusion">结论</h2>
<p>在本文中,您学习了如何使用<strong><u>&nbsp;Aspose.Diagram&nbsp;</u></strong>在 Java 中读取 Visio 形状数据。您了解了如何加载文件、提取形状属性以及读取继承的数据。借助这个强大的 API,您可以轻松构建处理 Visio 文件的 Java 应用。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1); font-size: 14px"><strong>Aspose.Diagram 试用版下载,请联系Aspose官方授权代理商<span style="color: rgba(35, 111, 161, 1)">慧都科技</span></strong></span></p>
<h2 style="text-align: center"><span style="color: rgba(230, 126, 35, 1); font-size: 14px"><strong><em>加入Aspose技术交流QQ群(</em></strong><em>1041253375</em><strong><em>),与更多小伙伴一起探讨提升开发技能。</em></strong></span></h2><br><br>
来源:https://www.cnblogs.com/software-Development/p/19008870
頁: [1]
查看完整版本: 图表控件Aspose.Diagram教程:使用 Java 读取 Visio 形状数据