How to Replace a String in a PowerPoint Presentation using C# .NET Framework
PowerPoint PPTX files are notoriously big and bulky, which can make sharing them and manually editing them a pain. Thankfully, when we only need to make simple changes to a document, we can handle the process programmatically by incorporating an API into a file processing workflow.
Using the below code, we can take advantage of a free API that allows us to find and replace text strings within our PowerPoint PPTX files. Our request is extremely simple — we just need to pass our file bytes, match string & replace string in with our request (as demonstrated in the below example):
{
"InputFileBytes": "string",
"InputFileUrl": "string",
"MatchString": "string",
"ReplaceString": "string",
"MatchCase": true
}
This will return the encoding for a new PPTX file with the changes included. On our own, we can choose to save this file separately or overwrite the previous contents.
We can authorize our API calls with a free API key, which will enable us to make up to 800 API calls per month with no additional commitments.
To structure our call, let’s start by installing the SDK. To install via NuGet, let’s run the below command in our Package Manager console:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Now let’s call the function, remembering to include our file bytes and API key along with our find & replace text strings:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class EditDocumentPptxReplaceExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditDocumentApi();
var reqConfig = new ReplaceStringRequest(); // ReplaceStringRequest | Replacement document configuration input
try
{
// Replace string in PowerPoint PPTX presentation
byte[] result = apiInstance.EditDocumentPptxReplace(reqConfig);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentPptxReplace: " + e.Message );
}
}
}
}
That’s all there is to it — now we can make quick programmatic changes to text in our PPTX files with minimal code.