| 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 onSecurityandRemote Site Settingsand then click onNew.Name the Remote Site NameasCloudmersiveAPI, and set the Remote Site URL to eitherhttps://api.cloudmersive.comor your Managed Instance endpoint.  Click on Save. Upload a test DOCX as a static resource
In Salesforce, navigate to Setup.  Then click onStatic Resourcesand thenNew.Under NameenterDocxSample2.  UnderFileselect a valid DOCX (not DOC) file. Execute the code
Navigate to the Settings Gearand thenDeveloper Consoleand thenDebugand thenOpen Execute Anonymous.  CheckOpen Log.  Paste the below source code.  ReplaceYOUR-API-KEY-HEREwith the appropriate API key.  If needed, update the API base path URL to match your Managed Instance base path.Executeyour codeIn the logs that appear click on Debug Logs.Look for Status: OK (200)- this indicates the API call was successfulYou 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 replaceStringswith your own replacements.  Note that you can add as many as you wish.  It will find the targetMatchStringand replace with theReplaceString.  We recommend keepingMatchCaseset tofalse. // 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());
}
 |