Skip to main content

Replace Text - Text Replacement API

PDF4me Replace Text enables you to find and replace text in Word documents with advanced search options. This API service processes Word files and performs multiple replacement operations with case-sensitive matching and whole word matching. The API receives Word document content through REST API calls, utilizing Base64 encoding for secure transmission. With support for multiple find and replace operations in a single request, this solution is ideal for document automation, template processing, and content management workflows.

Authenticating Your API Request

To access the PDF4me REST API, every request must include proper authentication credentials. Authentication ensures secure communication and validates your identity as an authorized user of the REST API.

Key Features

  • Multiple Text Replacements: Perform multiple find and replace operations in a single action
  • Advanced Search Options: Case-sensitive matching and whole word matching
  • Sequential Processing: Multiple replacements processed in specified order
  • Flexible Matching: Exact matches, partial matches, or whole word matches
  • Simple Structure: Easy-to-use array of find and replace operations

REST API Endpoint

The PDF4me REST API uses standard HTTP methods to interact with resources. All text replacement operations are performed through a single endpoint:

  • Method: POST
  • Endpoint: office/ApiV2Word/ReplaceText

REST API Parameters

Complete list of parameters for the Replace Text REST API. Parameters are organized by category for better understanding and implementation.

Important: Parameters marked with an asterisk (*) are required. The phrases array must contain at least one replacement phrase.

Required Parameters

ParameterTypeDescriptionExample
document*ObjectDocument reference. Must contain Name (string) — Word file name with .docx extension{ "Name": "document.docx" }
docContent*Base64Word document content encoded in Base64. Text replacement is performed on this documentUEsDBBQABgAIAAAA...
phrases*ArrayArray of replacement objects. Must contain at least one replacement. Each replacement is processed sequentially in the order specifiedSee example below

Text Replacement Object Structure (phrases array)

Each item in the phrases array must be an object with the following structure:

ParameterTypeDescriptionExample
findText*StringText string to search for in the document. Cannot be null or empty. Case-sensitive or case-insensitive based on matchCase setting. All instances of this text will be replaced"old text"
replaceTextStringText to replace the found text with. Can be empty string to remove found text. Default: empty string (removes found text)"new text"
matchCaseBooleanCase-sensitive matching. true = Search is case-sensitive (exact case match required), false = Search is case-insensitive (default). Controls whether "The" matches "the" or only "The"false
matchWholeWordBooleanWhole word matching. true = Match only complete words (not partial matches), false = Match partial text within words (default). Prevents "the" from matching "there" or "other"false

Optional Parameters

ParameterTypeDescriptionExample
cultureNameStringCulture code for document processing (e.g., "en-US", "de-DE", "fr-FR")en-US

Text Replacement Processing

  • Each replacement is processed sequentially in the order specified
  • Multiple replacements can be performed on the same document
  • All instances of Find Text are replaced (unless using whole word matching)

Output

The PDF4me Replace Text REST API returns different responses based on the processing mode. The API returns the Word document as a Base64-encoded string in JSON format, not as binary data.

Synchronous Processing (Default)

The API processes the request and returns:

Status Code: 200 OK

Content-Type: application/json

Response Body:

{
"document": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"fileName": "document.docx",
"success": true,
"errorMessage": null
}

Response Fields:

  • document (string): The Word document with text replacements applied, encoded as Base64 string
  • fileName (string): The output Word file name
  • success (boolean): Indicates whether the request succeeded
  • errorMessage (string or null): Error details when success is false

How to Use:

  1. Extract the document field from the JSON response (Base64)
  2. Decode the Base64 string to get the binary Word document data
  3. Save or process the Word file as needed

Example (JavaScript):

const response = await fetch(url, options);
const data = await response.json();
const wordBytes = atob(data.document); // Decode Base64
// Save or process wordBytes

Request Example

Content-Type: application/json
Authorization: Basic YOUR_BASE64_ENCODED_API_KEY

Note:

  • Get your API key from the PDF4me Dashboard
  • The API key must be Base64 encoded and prefixed with "Basic " in the Authorization header
  • Example: If your API key is abc123, encode it to Base64 and use Authorization: Basic YWJjMTIz

Payload

Basic Example (Single Replacement):

{
"document": { "Name": "document.docx" },
"docContent": "base64EncodedDocumentContent",
"phrases": [
{
"findText": "old text",
"replaceText": "new text",
"matchCase": false,
"matchWholeWord": false
}
],
"cultureName": "en-US"
}

Advanced Example (Multiple Replacements):

{
"document": { "Name": "document.docx" },
"docContent": "base64EncodedDocumentContent",
"phrases": [
{
"findText": "[COMPANY_NAME]",
"replaceText": "Acme Corporation",
"matchCase": false,
"matchWholeWord": false
},
{
"findText": "[DATE]",
"replaceText": "2024-01-15",
"matchCase": false,
"matchWholeWord": false
},
{
"findText": "[CONTACT_EMAIL]",
"replaceText": "[email protected]",
"matchCase": false,
"matchWholeWord": false
}
],
"cultureName": "en-US"
}

Code Samples

The PDF4me Replace Text REST API provides code samples in multiple programming languages. Choose the language that best fits your development environment:

C# (CSharp) Sample

Complete C# implementation for replacing text in Word documents:

Industry Use Cases & Applications

Legal & Professional Services Use Cases

  • Contract Processing: Replace placeholder text with client-specific information
  • Legal Document Automation: Update case numbers, client names, and dates
  • Template Standardization: Standardize legal terminology and formatting
  • Client Communication: Personalize legal documents for specific clients

Get Help