Run Cloudmersive DOCX Replace Multi Convert API in Salesforce Apex Developer Console |
10/1/2025 - Cloudmersive Support |
In this sample we will call the Cloudmersive Document Conversion API from the Salesforce Apex Developer Console to replace strings in a DOCX file.
First, we will do some basic configuration:
Allow the Callout
- In Salesforce, navigate to
Setup . Then click on Security and Remote Site Settings and then click on New .
- Name the
Remote Site Name as CloudmersiveAPI , and set the Remote Site URL to either https://api.cloudmersive.com or your Managed Instance endpoint. Click on Save.
Upload a test DOCX as a static resource
- In Salesforce, navigate to
Setup . Then click on Static Resources and then New .
- Under
Name enter DocxSample2 . Under File select a valid DOCX (not DOC) file.
Execute the code
- Navigate to the
Settings Gear and then Developer Console and then Debug and then Open Execute Anonymous . Check Open Log . Paste the below source code. Replace YOUR-API-KEY-HERE with the appropriate API key. If needed, update the API base path URL to match your Managed Instance base path.
Execute your code
- In the logs that appear click on
Debug Logs .
- Look for
Status: OK (200) - this indicates the API call was successful
- You can view the output PDF and verify it is correct by looking at the log after this
Open the file - copy the displayed URL and paste it into a new tab in your browser. After loading you will be able to view the replaced/updated version of the DOCX.
- Update
replaceStrings with your own replacements. Note that you can add as many as you wish. It will find the target MatchString and replace with the ReplaceString . We recommend keeping MatchCase set to false .
// Quick DOCX multi-replace callout using Cloudmersive Convert API
// Prereqs: Remote Site Setting for https://api.cloudmersive.com
// Static Resource: DocxSample (a small .docx)
// Load the DOCX bytes from a Static Resource
Blob templateBlob = [SELECT Body FROM StaticResource WHERE Name = 'DocxSample2'].Body;
// Build the JSON body as Apex objects, then serialize
Map<String, Object> body = new Map<String, Object>();
body.put('InputFileBytes', EncodingUtil.base64Encode(templateBlob)); // or use InputFileUrl instead
// Build ReplaceStrings array (add as many items as you need)
List<Object> replaceStrings = new List<Object>();
replaceStrings.add(new Map<String, Object>{
'MatchString' => 'Street Address',
'ReplaceString'=> 'Cloudmersive',
'MatchCase' => false
});
replaceStrings.add(new Map<String, Object>{
'MatchString' => 'Quote',
'ReplaceString'=> 'Product 1134',
'MatchCase' => false
});
body.put('ReplaceStrings', replaceStrings);
String jsonBody = JSON.serialize(body);
// Send the request
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.cloudmersive.com/convert/edit/docx/replace-all/multi');
req.setMethod('POST');
req.setHeader('Apikey', 'YOUR-API-KEY-HERE'); // <-- paste your key here
req.setHeader('Content-Type', 'application/json');
req.setTimeout(120000); // up to 120s
req.setBody(jsonBody);
Http http = new Http();
HttpResponse res = http.send(req);
System.debug('Status: ' + res.getStatus() + ' (' + res.getStatusCode() + ')');
// If successful, save the edited DOCX in Files so you can open it
if (res.getStatusCode() == 200) {
Blob editedDocx = res.getBodyAsBlob();
ContentVersion cv = new ContentVersion(
Title = 'Edited-from-docx',
PathOnClient = 'Edited-from-docx.docx',
VersionData = editedDocx,
IsMajorVersion = true
);
insert cv;
// Get the ContentDocumentId to form a clickable URL
cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id];
String host = System.Url.getOrgDomainUrl().getHost();
System.debug('Open the file: https://' + host + '/lightning/r/ContentDocument/' + cv.ContentDocumentId + '/view');
} else {
System.debug('Response body: ' + res.getBody());
}
|