PPT处理控件Aspose.Slides功能演示:使用 C# 在 PowerPoint 演示文稿中创建 SmartArt

演示文稿中的 SmartArt 用于以视觉形式提供信息。有时,选择使简单的文本更具吸引力。而在其他情况下,它用于演示流程图、流程、不同实体之间的关系等。在本文中,您将学习如何使用 C# 以编程方式在 PowerPoint 演示文稿中创建 SmartArt。

演示文稿中的 SmartArt 用于以视觉形式提供信息。有时,选择使简单的文本更具吸引力。而在其他情况下,它用于演示流程图、流程、不同实体之间的关系等。在本文中,您将学习如何使用 C# 以编程方式在 PowerPoint 演示文稿中创建 SmartArt。

  • 在 PowerPoint 中创建 SmartArt 的 .NET API
  • 在 PowerPoint 中创建 SmartArt 形状
  • 在 PowerPoint 中访问 SmartArt 形状
  • 更改 SmartArt 形状的样式

为了在 PowerPoint 演示如何使用 C# 以编程方式在 PowerPoint 演示文稿中创建 SmartArt。,我们将使用Aspose.Slides for .NET,它是一个强大且功能丰富的 API,用于在 .NET 应用程序中创建和操作演示文稿。

>>你可以点击这里下载Aspose.Slides 最新版测试体验。

在 PowerPoint 中创建 SmartArt 的 .NET API

为了在 PowerPoint 演示文稿中使用 SmartArt,我们将使用 Aspose.Slides for .NET。它是一个强大的类库,用于创建和操作 PowerPoint 和 OpenOffice 演示文稿。您可以通过NuGet安装 API  或 下载 其 DLL。

PM> Install-Package Aspose.Slides.NET

使用 C# 在 PowerPoint 中创建 SmartArt 形状

Aspose.Slides for .NET 提供了在演示文稿中创建 SmartArt 形状的最简单方法。为了演示,让我们使用 C# 在 PowerPoint 演示文稿中从头开始创建 SmartArt 形状。

  • 使用Presentation类创建一个新的演示文稿或加载一个现有的演示文稿。
  • 将所需幻灯片的引用获取到ISlide对象中。
  • 使用ISlide.Shapes.AddSmartArt()方法创建 SmartArt 。
  • 使用Presentation.Save(String, SaveFormat)方法保存更新的演示文稿。

以下代码示例展示了如何在 PowerPoint 演示文稿中创建 SmartArt 形状。

// Create a presentation or load existing oneusing (Presentation pres = new Presentation()){    // Access the presentation slide    ISlide slide = pres.Slides[0];    // Add SmartArt Shape    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";     // Save presentation    pres.Save("SimpleSmartArt_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);}

以下屏幕截图显示了上述代码示例的输出。

PPT处理控件Aspose.Slides功能演示:使用 C# 在 PowerPoint 演示文稿中创建 SmartArt

使用 C# 在 PowerPoint 中访问 SmartArt 形状

您还可以访问现有 PowerPoint 演示文稿中的 SmartArt 形状。访问后,您可以根据需要修改它们。以下是使用 C# 访问 PowerPoint 演示文稿中的 SmartArt 形状的步骤。

  • 使用Presentation类创建一个新的演示文稿或加载一个现有的演示文稿。
  • 将所需幻灯片的引用获取到ISlide对象中。
  • 使用ISlide.Shapes集合遍历幻灯片中的形状。
  • 如果形状是ISmartArt类型,则将其引用放入ISmartArt对象中。
  • 如果需要,使用ISmartArt.Layout属性过滤特定布局的 SmartArt 形状。

以下代码示例展示了如何访问 PowerPoint 演示文稿中的 SmartArt 形状。

// Load the presentationusing (Presentation pres = new Presentation("AccessSmartArtShape.pptx")){    // Iterate through every shape inside desired slide    foreach (IShape shape in pres.Slides[0].Shapes)    {        // Check if shape is of SmartArt type        if (shape is ISmartArt)        {            // Typecast shape to SmartArt            ISmartArt smart = (ISmartArt)shape;            System.Console.WriteLine("Shape Name:" + smart.Name);                // Checking SmartArt Layout            //if (smart.Layout == SmartArtLayoutType.BasicBlockList)            //{            //   Console.WriteLine("Do some thing here....");            //}        }    }}

使用 C# 更改 SmartArt 形状的样式

访问 SmartArt 形状后,您也可以更改其样式。以下步骤演示了如何使用 C# 更改 PowerPoint 演示文稿中 SmartArt 形状的样式。

  • 使用Presentation类创建一个新的演示文稿或加载一个现有的演示文稿。
  • 将所需幻灯片的引用获取到ISlide对象中。
  • 使用ISlide.Shapes集合遍历幻灯片中的形状。
  • 如果形状是ISmartArt类型,则将其引用放入ISmartArt对象中。
  • 更改所需的样式,即ISmartArt.ColorStyle、ISmartArt.QuickStyle等。
  • 使用Presentation.Save(String, SaveFormat)方法保存更新的演示文稿。

以下代码示例展示了如何更改 PowerPoint 演示文稿中 SmartArt 形状的样式。

// Load presentationusing (Presentation presentation = new Presentation("AccessSmartArtShape.pptx")){    // Traverse through every shape inside first slide    foreach (IShape shape in presentation.Slides[0].Shapes)    {        // Check if shape is of SmartArt type        if (shape is ISmartArt)        {            // Typecast shape to SmartArt            ISmartArt smart = (ISmartArt)shape;            // Check SmartArt style            if (smart.QuickStyle == SmartArtQuickStyleType.SimpleFill)            {                // Change SmartArt Style                smart.QuickStyle = SmartArtQuickStyleType.Cartoon;            }                // Check SmartArt color type            if (smart.ColorStyle == SmartArtColorType.ColoredFillAccent1)            {                // Change SmartArt color type                smart.ColorStyle = SmartArtColorType.ColorfulAccentColors;            }        }    }    // Save Presentation    presentation.Save("ChangeSmartArtStyle_out.pptx", SaveFormat.Pptx);}

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年9月5日
下一篇 2021年9月5日

相关推荐

发表回复

登录后才能评论