查找替換 Visio Java

Visio 文件可以包含形狀、連接器、圖像或文本來描繪不同的圖表,這些圖表用於創建圖表,如組織結構圖、流程圖等。在某些情況下,您可能想要查找並替換 VSD 或 VSDX 格式的一些文本視覺圖。因此,本文介紹瞭如何在 Java 中以編程方式查找和替換 Visio 繪圖中的文本。

在 Visio 圖表中搜索和替換文本 – Java API 安裝

Aspose.Diagram for Java API 支持使用不同的 Visio 文件格式,包括 VSD、VSDX、VSDM、VSSX 等。您可以從 下載 部分下載 JAR 文件,或使用以下在項目的 pom.xml 文件中進行配置,以便從 Aspose Repository 訪問 API:

存儲庫:

 <repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>http://repository.aspose.com/repo/</url>
    </repository>
</repositories>

依賴:

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-diagram</artifactId>
        <version>22.4</version>
        <classifier>jdk16</classifier>
    </dependency>
</dependencies>

在 Java 中以編程方式查找和替換 Visio 圖中的文本

您可以使用以下步驟在 VSD VSDX 格式的 Visio 文件中查找和替換文本:

  1. 創建字符串集合以搜索和替換文本。
  2. 加載源 Visio 圖表文件並循環遍歷每個形狀中的文本。
  3. 編寫輸出 Visio 圖表文件。

下面的代碼示例顯示瞭如何使用 Java 以編程方式查找和替換 Visio 文件中的文本:

// 加載輸入圖
Diagram diagram = new Diagram("FindReplaceText.vsdx");

DateFormat dateFormat = new SimpleDateFormat("dd/MMMM/yyyy");
Date myDate = new Date(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();

// 準備新舊文集
Hashtable<String, String> replacements = new Hashtable<String, String>();
replacements.put("[[CompanyName]]", "Research Society of XYZ");
replacements.put("[[CompanyName]]", "Research Society of XYZ");
replacements.put("[[EmplyeeName]]", "James Bond");
replacements.put("[[SubjectTitle]]", "The affect of the internet on social behavior in the industrialize world");

cal.setTime(myDate);
cal.add(Calendar.YEAR, -1);
System.out.println(dateFormat.format(cal.getTime()));
replacements.put("[[TimePeriod]]", dateFormat.format(cal.getTime()) + " -- " + dateFormat.format(myDate));

cal.setTime(myDate);
cal.add(Calendar.DAY_OF_MONTH, -7);
System.out.println(dateFormat.format(cal.getTime()));
replacements.put("[[SubmissionDate]]", dateFormat.format(cal.getTime()));
replacements.put("[[AmountReq]]", "$100,000");

cal.setTime(myDate);
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(dateFormat.format(cal.getTime()));
replacements.put("[[DateApproved]]", dateFormat.format(cal.getTime()));

// 遍歷頁面的形狀
for (Shape shape : (Iterable<Shape>) diagram.getPages().getPage("Page-1").getShapes())
{
    Set<String> keys = replacements.keySet();
    for(String key: keys)
    {
       for (FormatTxt txt : (Iterable<FormatTxt>) shape.getText().getValue())
        {
       	    Txt tx = (Txt)((txt instanceof Txt) ? txt : null);
            if (tx != null && tx.getText().contains(key))
            {
                // 查找和替換形狀的文本
                tx.setText(tx.getText().replace(key, replacements.get(key)));
            }
        }
    }
}

// 保存圖表
diagram.save("FindReplaceText_Out.vsdx", SaveFileFormat.VSDX);

結論

總之,您已了解如何在 Visio 圖表中查找和替換文本。您還可以即興創作代碼片段以與 replace 方法的其他重載一起使用。例如,替換匹配文本的所有實例或僅替換 Visio 文件中第一次出現的搜索詞。您可以訪問 文檔 空間來了解其他幾個操作或轉換 MS Visio 文件的功能。如果您需要討論您的任何疑慮或要求,請通過 論壇 與我們聯繫。

也可以看看

在 Java 中將 Visio VSD 或 VSDX 文件轉換為 XAML