Edit PPT/PPTX Presentation using .NET API

The most common and widely used presentation file formats are PPT, PPTX, and ODP. The famous Microsoft PowerPoint, OpenOffice Impress and Apple Keynote support these formats and we normally use these formats for making spectacular presentations. As a developer, we can edit presentations in our applications programmatically. In this article, we will discuss how to edit PPT/PPTX presentations in C# using the .NET API for presentation editing.

The following are the topics discussed briefly in this article:

.NET API for Presentations Editing and Automation

Now, we will be using the GroupDocs.Editor for .NET in below C# examples. It is the presentation editing API and allows developers to load, edit, and save the edited presentations in other formats like PPT, PPTX, PDF. In addition to the presentation formats, the API supports editing word-processing documents, spreadsheets, HTML, XML, JSON, TXT, TSV, and CSV formats.

Download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Editor

Edit PPTX/PPTX Presentations in C#

Just after setting up the API, you can quickly move towards editing your presentation slides. The following steps will let you edit the presentation of PPT/PPTX and other supported formats.

  • Load the presentation.
  • Edit using the available options.
  • Save the edited presentation.

Load the PPT/PPTX Presentation

Firstly, load the presentation by providing the presentation file path and the password, if the presentation is protected.

// Load Presentation
using (FileStream fs = File.OpenRead("path/presentation.pptx"))
{
    // Load Presentation
    Options.PresentationLoadOptions loadOptions = new PresentationLoadOptions();
    loadOptions.Password = "P@$$w0Rd";
}

Edit the PPT/PPTX Presentation Slides

After loading, you can edit the loaded presentation as per requirement. Here I am replacing all the occurrences of word “documents” with the “presentation” in a PPTX presentation using the below C# code.

// Edit Presentation
using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
    Options.PresentationEditOptions editOptions = new PresentationEditOptions();
    editOptions.SlideNumber = 0;            // 1st slide
    editOptions.ShowHiddenSlides = true;

    using (EditableDocument beforeEdit = editor.Edit(editOptions))
    {
        string originalContent = beforeEdit.GetContent();
        List<IHtmlResource> allResources = beforeEdit.AllResources;
        string editedContent = originalContent.Replace("documents", "presentation");       
    }
}

Save the Edited PowerPoint presentation with Options

Lastly, while saving the edited presentation content, you can further set various options. These options include; set password, output format settings. I set the above options in the below-mentioned code and save the edited presentation as a password-protected PPTX file.

// Save Presentation
using (EditableDocument afterEdit = EditableDocument.FromMarkup(editedContent, allResources))
{
    Options.PresentationSaveOptions saveOptions = new PresentationSaveOptions(PresentationFormats.Pptm);
    saveOptions.Password = "new_pa$$word";

    using (FileStream outputStream = File.Create("path/edited-presentation.pptx"))
    {
        editor.Save(afterEdit, outputStream, saveOptions);
    }
}

Complete Code

For the convenience, here is the complete C# example that is explained above and it edits the PowerPoint presentation and then saves it in PPTX format.

The following is the output presentation in which all the occurrences are replaced using the above code.

edited pptx presentation using editing API

Output presentation - ‘documents’ occurrences are replaced by ‘presentation’

Conclusion

To conclude, we discussed editing presentations slides in C# using presentation editing API for .NET applications. You can use the API with WYSIWYG editors for the visual editing of your presentations. After that, you can move ahead to build your own presentation editor. Similarly, you can also integrate the editing feature within your .NET application.

For more details, options, and examples, you can visit the documentation and the GitHub repository. For further queries, contact the support on the forum.