How to Replace a String in a PowerPoint PPTX Presentation using Java

Cloudmersive
3 min readSep 9, 2022

--

PowerPoint documents are often exceptionally large, filled with dozens of slides carrying immense multimedia files and formatted text. As such, making small changes to PPTX documents is no trivial task. Thankfully, however, you can make programmatic changes to any PPTX file with our PPTX String Replacer API. As long as you know which string you’re looking to replace within the document, you can configure this API’s parameters to target & replace that string with a new value, and you can even specify if you want the case of the new value to match the original string.

Below, I’ll demonstrate how you can easily use this API & structure your API call with ready-to-run Java code examples. You can use up to 800 API calls per month completely free with zero additional commitments — all you need to do is register a free account on our website & use your secure Cloudmersive API key to authenticate the service.

Before we structure the API call itself, we first need to install the API client. We can begin to do so by adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And then we can add a reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With our installation steps out of the way, we can turn our attention to the controller & add our imports:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.TransformDocumentApi;

Now we can call the API. Let’s add our API key in the second snippet (where indicated in the comments) and pass our matchString and replaceString arguments through their respective inputs. We can then either include a URL to our PPTX file, or include the file path where indicated:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
TransformDocumentApi apiInstance = new TransformDocumentApi();
String matchString = "matchString_example"; // String | String to search for and match against, to be replaced
String replaceString = "replaceString_example"; // String | String to replace the matched values with
File inputFile = new File("/path/to/inputfile"); // File | Optional: Input file to perform the operation on.
String inputFileUrl = "inputFileUrl_example"; // String | Optional: URL of a file to operate on as input. This can be a public URL, or you can also use the begin-editing API (part of EditDocumentApi) to upload a document and pass in the secure URL result from that operation as the URL here (this URL is not public).
Boolean matchCase = true; // Boolean | Optional: True if the case should be matched, false for case insensitive match. Default is false.
try {
byte[] result = apiInstance.transformDocumentPptxReplace(matchString, replaceString, inputFile, inputFileUrl, matchCase);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling TransformDocumentApi#transformDocumentPptxReplace");
e.printStackTrace();
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet