Skip to main content

Replace Text with Image - Image Replacement API

PDF4me Replace Text with Image enables you to replace specified text with images in Word documents with advanced page filtering and size control options. This API service processes Word files and replaces text placeholders with images using precise text matching, customizable sizing with aspect ratio preservation, page-specific targeting, and flexible filtering options. The API receives Word document content and image data through REST API calls, utilizing Base64 encoding for secure transmission. With support for multiple image formats and full control over replacement behavior, this solution is ideal for document automation, template processing, and dynamic content insertion 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

  • Text-to-Image Replacement: Replace specific text strings with images throughout documents
  • Precise Text Matching: Case-sensitive text search and replacement
  • Image Size Control: Customize image dimensions with width and height parameters
  • Aspect Ratio Preservation: Maintain image proportions during resizing
  • Page Filtering: Target specific pages or page ranges for replacement
  • Advanced Filtering: Skip first page, target odd/even pages, or specify exact page numbers
  • Multiple Image Formats: Support for PNG, JPG, JPEG, BMP, GIF formats
  • Flexible Positioning: Insert images as Word shapes with precise positioning

REST API Endpoint

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

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

REST API Parameters

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

Important: Parameters marked with an asterisk (*) are required. Advanced parameters provide fine-grained control over image sizing and page filtering.

Required Parameters

ParameterTypeDescriptionExample
document*ObjectDocument descriptor. Must include Name (string): output Word file name with .docx extension{ "Name": "document.docx" }
docContent*Base64Word document content encoded in Base64. Text replacement is performed on this document. Must be valid Word document (.docx, .doc formats)base64EncodedDocumentContent
findText*StringText string to search for in the document. Case-sensitive. All instances will be replaced with the image. Cannot be null or empty. Examples: "[LOGO]", "SIGN_HERE""text to replace"
imageContent*Base64Replacement image file content encoded in Base64. Must be valid image (PNG, JPG, JPEG, BMP, GIF)base64EncodedImageContent

Optional Parameters

ParameterTypeDescriptionExample
widthNumber (double)Image width200
heightNumber (double)Image height100
maintainAspectRatioBooleanMaintain image proportions during resizing. Default: truetrue
skipFirstPageBooleanSkip replacement on first page. true = skip first page, false = include (default). Useful for cover/title pagesfalse
applyToStringTarget scope for replacement
pageNumbersStringSpecific page numbers or ranges (e.g., "1,2,3" or "1,3,5-7"). Empty = all pages"1,2,3"
ignorePageNumbersStringPage numbers to exclude from replacement
cultureNameStringCulture code (e.g., "en-US", "fr-FR", "de-DE"). Default: nullen-US

Page Filtering

The API supports page filtering through the following parameters:

  • skipFirstPage: Controls whether to skip the first page
  • pageNumbers: Specifies which pages to process (empty string = all pages)
  • ignorePageNumbers: Page numbers to exclude

Supported Image Formats

The Replace Text with Image API supports various image formats for maximum compatibility:

FormatExtensionDescription
PNG.pngPortable Network Graphics - best for logos and graphics with transparency
JPEG.jpg, .jpegJoint Photographic Experts Group - best for photographs and complex images
BMP.bmpBitmap - uncompressed format, larger file sizes
GIF.gifGraphics Interchange Format - supports animation and transparency

Output

The PDF4me Replace Text with Image REST API returns a JSON response (ApiV2WordApiCallRes). On success the API returns the Word document as a Base64-encoded string in the document field, not as binary data.

Success Response

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 replaced by images, encoded as Base64
  • fileName (string): The output Word file name
  • success (boolean): Whether the operation succeeded
  • errorMessage (string): Error message if success is false; otherwise null

How to Use:

  1. Check success; if true, extract the document field from the JSON response
  2. Decode the Base64 string to get the binary Word document data
  3. Save or process the Word file using fileName

Example (JavaScript):

const response = await fetch(url, options);
const data = await response.json();
if (!data.success) throw new Error(data.errorMessage);
const wordBytes = atob(data.document);
// 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 (Required Fields Only):

{
"document": { "Name": "document.docx" },
"docContent": "base64EncodedDocumentContent",
"findText": "text to replace",
"imageContent": "base64EncodedImageContent",
"cultureName": "en-US"
}

Advanced Example (With Optional Fields):

{
"document": { "Name": "document.docx" },
"docContent": "base64EncodedDocumentContent",
"findText": "text to replace",
"imageContent": "base64EncodedImageContent",
"width": 200,
"height": 100,
"maintainAspectRatio": true,
"skipFirstPage": true,
"pageNumbers": "1,2,3",
"ignorePageNumbers": null,
"cultureName": "en-US"
}

Code Samples

The PDF4me Replace Text with Image 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 with images in Word documents:

Industry Use Cases & Applications

Legal & Professional Services Use Cases

  • Contract Processing: Replace signature placeholders with authorized signatures
  • Legal Document Automation: Insert firm logos and watermarks into legal documents
  • Client Document Personalization: Add client-specific images to legal documents
  • Compliance Documentation: Insert compliance stamps and seals into documents

Get Help