PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint幻灯片中添加或删除形状

PowerPoint提供了多种形状,您可以将它们添加到演示文稿幻灯片中,例如椭圆形,直线,矩形,连接器等。为了自动执行此功能,本文介绍如何使用C#以编程方式在PowerPoint幻灯片中添加,克隆和删除形状。

形状是使PowerPoint演示文稿更加精美和吸引人的好方法。PowerPoint提供了多种形状,您可以将它们添加到演示文稿幻灯片中,例如椭圆形,直线,矩形,连接器等。为了自动执行此功能,本文介绍如何使用C#以编程方式在PowerPoint幻灯片中添加,克隆和删除形状。

  • 将形状添加到PowerPoint幻灯片
  • 将连接器添加到PowerPoint幻灯片中的形状
  • 在PowerPoint幻灯片中克隆形状
  • 从PowerPoint幻灯片中删除形状

Aspose.Slides for .NET是一个C#API,旨在与.NET应用程序中的PowerPoint演示文稿一起使用。与其他演示文稿操纵功能一起,API提供了在PowerPoint幻灯片中使用形状的简便方法。

>>你可以点击这里下载Aspose.Slides v20.12测试体验。

17周年庆来啦!整合所有格式API处理控件Aspose.Total永久授权火热促销中,新购乐享85折起!联系客服立马1分钟了解全部!


使用C#向PowerPoint幻灯片添加形状

为了添加一个形状,如椭圆、直线、矩形等,Aspose.Slides提供了IShapeCollection.AddAutoShape(ShapeType, Single, Single, Single, Single)方法。ShapeType枚举可以让您指定要添加的形状类型。以下是向PowerPoint幻灯片添加形状的步骤。

  • 创建Presentation类的实例 以创建新的演示文稿或加载现有的演示文稿。
  • 使用Presentation.Slides [index]进入ISlide对象获取幻灯片的引用。
  • 使用IShapes对象公开的IShapeCollection.AddAutoShape (ShapeType,Single,Single,Single,Single,Single)方法添加一个椭圆(或任何其他形状)。
  • 使用Presentation.Save(String,SaveFormat)方法保存PPTX文件。

下面的代码示例演示如何使用C#将形状添加到PowerPoint幻灯片中。

// Instantiate a Presentation object that represents a presentation fileusing (Presentation pres = new Presentation("presentation.pptx")){    // Get the first slide    ISlide sld = pres.Slides[0];    // Add autoshape of ellipse type    sld.Shapes.AddAutoShape(ShapeType.Ellipse, 50, 150, 150, 50);    // Save presentation    pres.Save("presentation.pptx", Export.SaveFormat.Pptx);}

添加连接器以在C#中连接PowerPoint形状

连接器是用于连接形状以便将它们连接起来的线。连接器可以是直线或曲线。让我们看看如何在PowerPoint幻灯片的两个形状之间添加连接器。

  • 创建Presentation 类的实例以创建新的演示文稿。
  • 使用Presentation.Slides [index]进入ISlide对象获取幻灯片的引用。
  • 就像在上一个示例中添加的那样,添加两个形状,并在IAutoShape对象中获取它们的引用。
  • 使用IShapeCollection.AddConnector(ShapeType,Single,Single,Single,Single,Single)方法创建一个新的IConnector对象。
  • 使用IConnector.StartShapeConnectedTo和IConnector.EndShapeConnectedTo属性连接形状。
  • 调用IConnector.Reroute()方法以创建最短的自动连接路径。
  • 使用Presentation.Save(String,SaveFormat)方法保存PPTX文件。

下面的代码示例演示如何使用C#在PowerPoint幻灯片中连接形状。

// Instantiate a Presentation object that represents a presentation fileusing (Presentation pres = new Presentation("presentation.pptx")){    // Accessing shapes collection for selected slide    IShapeCollection shapes = pres.Slides[0].Shapes;    // Add autoshape Ellipse    IAutoShape ellipse = shapes.AddAutoShape(ShapeType.Ellipse, 0, 100, 100, 100);    // Add autoshape Rectangle    IAutoShape rectangle = shapes.AddAutoShape(ShapeType.Rectangle, 100, 300, 100, 100);    // Adding connector shape to slide shape collection    IConnector connector = shapes.AddConnector(ShapeType.BentConnector2, 0, 0, 10, 10);    // Joining Shapes to connectors    connector.StartShapeConnectedTo = ellipse;    connector.EndShapeConnectedTo = rectangle;    // Call reroute to set the automatic shortest path between shapes    connector.Reroute();    // Save presentation    pres.Save("presentation.pptx", Export.SaveFormat.Pptx);}

使用C#在PowerPoint幻灯片中克隆形状

还可以使用Aspose.Slides for .NET将形状从一张PowerPoint幻灯片克隆到另一张幻灯片。以下是执行此操作的步骤。

  • 创建Presentation 类的实例 。
  • 使用Presentation.Slides [index]进入ISlide对象获取幻灯片的引用。
  • 使用ISlide.Shapes集合访问源幻灯片形状。
  • 使用ISlide.Shapes集合访问目标幻灯片形状。
  • 使用IShapeCollection.AddClone(ISlide)方法将形状从源幻灯片形状集合复制到目标幻灯片。
  • 保存更新的演示文稿文件。

下面的代码示例演示如何使用C#在PowerPoint幻灯片中克隆形状。

// Instantiate a Presentation object that represents a presentation fileusing (Presentation pres = new Presentation("presentation.pptx")){    // Obtain shape collection from source slide    IShapeCollection sourceShapes = pres.Slides[0].Shapes;    ILayoutSlide blankLayout = pres.Masters[0].LayoutSlides.GetByType(SlideLayoutType.Blank);    ISlide destSlide = pres.Slides.AddEmptySlide(blankLayout);     // Get shape collection from destination slide    IShapeCollection destShapes = destSlide.Shapes;    destShapes.AddClone(sourceShapes[1], 50, 150 + sourceShapes[0].Height);    destShapes.AddClone(sourceShapes[2]);     // Clone shape    destShapes.InsertClone(0, sourceShapes[0], 50, 150);    // Save presentation    pres.Save("presentation.pptx", Export.SaveFormat.Pptx);}

使用C#从PowerPoint幻灯片中删除形状

以下是从PowerPoint幻灯片中删除形状的步骤。

  • 创建Presentation 类的实例以加载PPTX文件。
  • 从Presentation.Slides [index]访问所需的幻灯片到ISlide对象。
  • 使用特定的IShape.AlternativeText查找形状。
  • 使用ISlide.Shapes.Remove(IShape)方法删除形状。
  • 保存更新的演示文稿文件。

下面的代码示例演示如何使用C#从PowerPoint幻灯片中删除形状。

// Instantiate a Presentation object that represents a presentation fileusing (Presentation pres = new Presentation("presentation.pptx")){    // Get the first slide    ISlide sld = pres.Slides[0];    // Add autoshape of rectangle type    IShape shp1 = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 40, 150, 50);    IShape shp2 = sld.Shapes.AddAutoShape(ShapeType.Moon, 160, 40, 150, 50);     String alttext = "User Defined";    int iCount = sld.Shapes.Count;    for (int i = 0; i < iCount; i++) { // Retrieve shape AutoShape ashp = (AutoShape)sld.Shapes[0]; if (String.Compare(ashp.AlternativeText, alttext, StringComparison.Ordinal) == 0) { // Remove shape sld.Shapes.Remove(ashp); } } // Save presentation pres.Save("presentation.pptx", Export.SaveFormat.Pptx); }


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

来源:慧都

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

上一篇 2020年11月27日
下一篇 2020年11月27日

相关推荐

发表回复

登录后才能评论