演示文稿中的 SmartArt 用於以視覺形式提供信息。有時,它會選擇使簡單的文本更具吸引力。而在其他情況下,它用於演示流程圖、過程、不同實體之間的關係等。在本文中,您將學習如何使用 C# 以編程方式在 PowerPoint 演示文稿中創建 SmartArt。

用於在 PowerPoint 中創建 SmartArt 的 .NET API

要在 PowerPoint 演示文稿中使用 SmartArt,我們將使用 Aspose.Slides for .NET。它是一個強大的類庫,用於創建和操作 PowerPoint 和 OpenOffice 演示文稿。您可以通過 NuGet下載 其 DLL 安裝 API。

PM> Install-Package Aspose.Slides.NET

使用 C# 在 PowerPoint 中創建 SmartArt 形狀

Aspose.Slides for .NET 提供了在演示文稿中創建 SmartArt 形狀的最簡單方法。為了演示,讓我們使用 C# 在 PowerPoint 演示文稿中從頭開始創建 SmartArt 形狀。

以下代碼示例演示如何在 PowerPoint 演示文稿中創建 SmartArt 形狀。

// 創建演示文稿或加載現有演示文稿
using (Presentation pres = new Presentation())
{
    // 訪問演示幻燈片
    ISlide slide = pres.Slides[0];

    // 添加 SmartArt 形狀
    ISmartArt smart = slide.Shapes.AddSmartArt(0, 0, 400, 400, SmartArtLayoutType.BasicBlockList);
    smart.AllNodes[0].TextFrame.Text = "First Block";
    smart.AllNodes[1].TextFrame.Text = "Second Block";
    
    // 保存演示文稿
    pres.Save("SimpleSmartArt_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

以下屏幕截圖顯示了上述代碼示例的輸出。

在 PowerPoint C# 中創建 SmartArt

使用 C# 在 PowerPoint 中訪問 SmartArt 形狀

您還可以訪問現有 PowerPoint 演示文稿中的 SmartArt 形狀。訪問後,您可以根據需要修改它們。以下是使用 C# 訪問 PowerPoint 演示文稿中的 SmartArt 形狀的步驟。

  • 使用 Presentation 類創建新演示文稿或加載現有演示文稿。
  • 將所需幻燈片的引用獲取到 ISlide 對像中。
  • 使用 ISlide.Shapes 集合循環遍歷幻燈片中的形狀。
  • 如果形狀是 ISmartArt 類型,則將其引用獲取到 ISmartArt 對像中。
  • 如果需要,使用 ISmartArt.Layout 屬性過濾特定佈局的 SmartArt 形狀。

以下代碼示例演示如何訪問 PowerPoint 演示文稿中的 SmartArt 形狀。

// 加載演示文稿
using (Presentation pres = new Presentation("AccessSmartArtShape.pptx"))
{
    // 遍歷所需幻燈片中的每個形狀
    foreach (IShape shape in pres.Slides[0].Shapes)
    {
        // 檢查形狀是否為 SmartArt 類型
        if (shape is ISmartArt)
        {
            // 將形狀類型轉換為 SmartArt
            ISmartArt smart = (ISmartArt)shape;
            System.Console.WriteLine("Shape Name:" + smart.Name);
            
            // 檢查 SmartArt 佈局
            //如果(smart.Layout == SmartArtLayoutType.BasicBlockList)
            //{
            //   Console.WriteLine("在這裡做一些事情......");
            //}
        }
    }
}

使用 C# 更改 SmartArt 形狀的樣式

訪問 SmartArt 形狀後,您也可以更改其樣式。以下步驟演示瞭如何使用 C# 更改 PowerPoint 演示文稿中 SmartArt 形狀的樣式。

下面的代碼示例演示如何更改 PowerPoint 演示文稿中 SmartArt 形狀的樣式。

// 負載演示
using (Presentation presentation = new Presentation("AccessSmartArtShape.pptx"))
{
    // 遍歷第一張幻燈片中的每個形狀
    foreach (IShape shape in presentation.Slides[0].Shapes)
    {
        // 檢查形狀是否為 SmartArt 類型
        if (shape is ISmartArt)
        {
            // 將形狀類型轉換為 SmartArt
            ISmartArt smart = (ISmartArt)shape;

            // 檢查 SmartArt 樣式
            if (smart.QuickStyle == SmartArtQuickStyleType.SimpleFill)
            {
                // 更改 SmartArt 樣式
                smart.QuickStyle = SmartArtQuickStyleType.Cartoon;
            }
            
            // 檢查 SmartArt 顏色類型
            if (smart.ColorStyle == SmartArtColorType.ColoredFillAccent1)
            {
                // 更改 SmartArt 顏色類型
                smart.ColorStyle = SmartArtColorType.ColorfulAccentColors;
            }
        }
    }

    // 保存演示文稿
    presentation.Save("ChangeSmartArtStyle_out.pptx", SaveFormat.Pptx);
}

獲取免費的 API 許可證

獲得免費的臨時許可,以在沒有評估限制的情況下使用 Aspose.Slides for .NET。

結論

在本文中,您了解瞭如何使用 C# 在 PowerPoint 演示文稿中創建 SmartArt。此外,您還了解瞭如何訪問 SmartArt 形狀並以編程方式更改它們的樣式。您可以瀏覽 文檔 以了解有關 Aspose.Slides for .NET 的更多信息。此外,您可以通過我們的 論壇 提問。

也可以看看