翻譯|使用教程|編輯:李顯亮|2020-12-30 10:26:06.990|閱讀 579 次
概述:PowerPoint提供了多種形狀,您可以將它們添加到演示文稿幻燈片中,例如橢圓形,直線,矩形,連接器等。為了自動執(zhí)行此功能,本文介紹如何使用C#以編程方式在PowerPoint幻燈片中添加,克隆和刪除形狀。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
形狀是使PowerPoint演示文稿更加精美和吸引人的好方法。PowerPoint提供了多種形狀,您可以將它們添加到演示文稿幻燈片中,例如橢圓形,直線,矩形,連接器等。為了自動執(zhí)行此功能,本文介紹如何使用C#以編程方式在PowerPoint幻燈片中添加,克隆和刪除形狀。
Aspose.Slides for .NET是一個C#API,旨在與.NET應用程序中的PowerPoint演示文稿一起使用。與其他演示文稿操縱功能一起,API提供了在PowerPoint幻燈片中使用形狀的簡便方法。
>>你可以點擊這里下載Aspose.Slides v20.12測試體驗。
為了添加一個形狀,如橢圓、直線、矩形等,Aspose.Slides提供了IShapeCollection.AddAutoShape(ShapeType, Single, Single, Single, Single)方法。ShapeType枚舉可以讓您指定要添加的形狀類型。以下是向PowerPoint幻燈片添加形狀的步驟。
下面的代碼示例演示如何使用C#將形狀添加到PowerPoint幻燈片中。
// Instantiate a Presentation object that represents a presentation file using (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); }
連接器是用于連接形狀以便將它們連接起來的線。連接器可以是直線或曲線。讓我們看看如何在PowerPoint幻燈片的兩個形狀之間添加連接器。
下面的代碼示例演示如何使用C#在PowerPoint幻燈片中連接形狀。
// Instantiate a Presentation object that represents a presentation file using (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); }
還可以使用Aspose.Slides for .NET將形狀從一張PowerPoint幻燈片克隆到另一張幻燈片。以下是執(zhí)行此操作的步驟。
下面的代碼示例演示如何使用C#在PowerPoint幻燈片中克隆形狀。
// Instantiate a Presentation object that represents a presentation file using (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); }
以下是從PowerPoint幻燈片中刪除形狀的步驟。
下面的代碼示例演示如何使用C#從PowerPoint幻燈片中刪除形狀。
// Instantiate a Presentation object that represents a presentation file using (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); }
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn