As a powerful and easy to use .NET component, Spire.Presentation enable developers to generate, modify, convert, render, and print documents without installing Microsoft PowerPoint. In this article let's see how to remove textbox.
Make sure Spire.Presentation for .NET has been installed correctly and then add Spire.Presentation.dll as reference in the downloaded Bin folder through the below path, for example .NET 4.0 : "..\Spire.Presentation\Bin\NET4.0\ Spire. Presentation.dll". Here comes to the details of how to remove textbox from PPT files:
Step 1: Create a PPT document and load sample PPT file. In this sample file three textboxes are added by order with their name number.
Presentation ppt = new Presentation("Sample.pptx", FileFormat.Pptx2010);
Step 2: Traverse all the shapes in Slide[0], then remove them as long as they were found.
ISlide slide = ppt.Slides[0]; for (int i = 0; i < slide.Shapes.Count;) { IAutoShape shape = slide.Shapes[i] as IAutoShape; slide.Shapes.Remove(shape); }
Step 3: Save the document as "Result.pptx" and review.
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("Result.pptx");
The result is just as we thought.
If we need to delete textbox1, just remove the first shape. Check if this shape is textbox and its contents is “Textbox1” and then remove it, modify Step2 as:
ISlide slide = ppt.Slides[0]; for (int i = 0; i < slide.Shapes.Count; ) { IAutoShape shape = slide.Shapes[i] as IAutoShape; if (shape.IsTextBox ||shape.TextFrame.Text.Equals("Textbox1")) { slide.Shapes.Remove(shape); } }
Here is the result:
Full Code:
using Spire.Presentation; namespace RemoveText { class Program { static void Main(string[] args) { Presentation ppt = new Presentation("Sample.pptx", FileFormat.Pptx2010); ISlide slide = ppt.Slides[0]; for (int i = 0; i < slide.Shapes.Count; ) { IAutoShape shape = slide.Shapes[0] as IAutoShape; slide.Shapes.Remove(shape); } ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("Result.pptx"); } } }
Remove Textbox1:
using Spire.Presentation; namespace RemoveText { class Program { static void Main(string[] args) { Presentation ppt = new Presentation("Sample.pptx", FileFormat.Pptx2010); ISlide slide = ppt.Slides[0]; for (int i = 0; i < slide.Shapes.Count; ) { IAutoShape shape = slide.Shapes[i] as IAutoShape; if (shape.IsTextBox || shape.TextFrame.Text.Equals("Textbox1")) { slide.Shapes.Remove(shape); } } ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("Result.pptx"); } }