In previous topics, we demonstrated how to create, format, protect and copy chart in PowerPoint. In this article, we'll show you how to remove chart from a specific slide by using Spire.Presentation.
Below is a sample document which contains a chart and a textbox on the first slide, then we'll remove the chart from the slide.
Code Snippets:
Step 1: Instantiate a Presentation object and load the PowerPoint document.
Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx");
Step 2: Get the first slide from the document.
ISlide slide = ppt.Slides[0];
Step 3: Remove chart from the slide.
for(int i = 0; i < slide.Shapes.Count; i++) { IShape shape = slide.Shapes[i] as IShape; if(shape is IChart) { slide.Shapes.Remove(shape); } }
Step 4: Save the document.
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
Effective screenshot:
Full code:
[C#]
using Spire.Presentation; using Spire.Presentation.Charts; namespace Remove_Chart_in_PowerPoint { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx"); ISlide slide = ppt.Slides[0]; for(int i = 0; i < slide.Shapes.Count; i++) { IShape shape = slide.Shapes[i] as IShape; if(shape is IChart) { slide.Shapes.Remove(shape); } } ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); } } }
[VB.NET]
Imports Spire.Presentation Imports Spire.Presentation.Charts Namespace Remove_Chart_in_PowerPoint Class Program Private Shared Sub Main(args As String()) Dim ppt As New Presentation() ppt.LoadFromFile("Sample.pptx") Dim slide As ISlide = ppt.Slides(0) For i As Integer = 0 To slide.Shapes.Count - 1 Dim shape As IShape = TryCast(slide.Shapes(i), IShape) If TypeOf shape Is IChart Then slide.Shapes.Remove(shape) End If Next ppt.SaveToFile("result.pptx", FileFormat.Pptx2010) End Sub End Class End Namespace