Merge Documents - Document Assembly API
PDF4me Merge Documents enables you to combine multiple Word documents into a single document with advanced formatting and compliance options. This API service processes multiple Word files and merges them with flexible document ordering, customizable formatting modes, compliance level settings, timezone metadata, and intelligent style handling. The API receives document contents through REST API calls, utilizing Base64 encoding for secure transmission. With support for unlimited document merging and full control over merge behavior, this solution is ideal for document assembly, report consolidation, 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 Document Support: Merge unlimited number of documents into one
- Simple Array Structure: Easy-to-use array of Base64-encoded document contents
- Ordered Merging: Documents merged in the order they appear in the array
- Base Document Concept: First document becomes the foundation for merging
- Multiple Format Support: Merge Word documents, PDFs, and other supported formats
REST API Endpoint
The PDF4me REST API uses standard HTTP methods to interact with resources. All document merging operations are performed through a single endpoint:
- Method: POST
- Endpoint:
office/ApiV2Word/MergeDocuments
REST API Parameters
Complete list of parameters for the Merge Documents REST API. Parameters are organized by category for better understanding and implementation.
Important: Parameters marked with an asterisk (*) are required. The documents array must contain at least one document.
Required Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| documents* | Array | Array of document objects to be merged. Must contain at least one document. Each object must have filename (string) and docContent (Base64 string). Documents are merged in the order they appear in the array. Optional per-document fields: sortPosition (integer), formatMode (string) | [{ "filename": "doc1.docx", "docContent": "base64..." }, { "filename": "doc2.docx", "docContent": "base64..." }] |
Optional Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| mergeOptions | Object | Merge options (formatting, compliance, etc.). See plugin model for available properties | {} |
Document Merge Order
Documents are merged in the order they appear in the documents array:
- First document in the array becomes the base document
- All subsequent documents are appended to the base document in array order
Output
The PDF4me Merge Documents 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.
- Success Response
- Asynchronous Processing
- Error Responses
- Response Format Details
Synchronous Processing (Default)
The API processes the request and returns:
Status Code: 200 OK
Content-Type: application/json
Response Body:
{
"document": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"fileName": "merged-document.docx",
"success": true,
"errorMessage": null
}
Response Fields:
- document (string): The merged Word document containing all input documents, 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:
- Extract the
documentfield from the JSON response (Base64) - Decode the Base64 string to get the binary Word document data
- 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
Asynchronous Processing
Asynchronous behavior (202 Accepted with polling) is controlled by server configuration, not by a request body parameter. When enabled, the API may return a 202 status with a polling URL in the Location header. Poll the URL with GET requests until you receive 200 OK with the same response shape (document, fileName, success, errorMessage).
Error Responses
The API returns standard HTTP error codes with error details:
- Invalid request parameters
- Missing required fields (
documentsarray) - No documents to merge (empty documents array)
- Invalid Base64 encoding in document content
- Invalid or corrupted documents
- Invalid or missing API key
- API key not properly Base64 encoded in Authorization header
- Missing
Authorization: Basicheader
- Server-side processing error
- Word document processing failure
- Document merging failure
Error Response Format:
{
"error": "Error message describing what went wrong"
}
Response Format Details
Important: The API always returns JSON, never binary Word data directly.
Response Structure:
{
"document": "string", // Base64-encoded Word document content
"fileName": "string", // Output Word filename
"success": true,
"errorMessage": "string or null"
}
Content-Type Header:
- Success:
application/json - The Word document is embedded as a Base64 string within the JSON response
Why Base64?
- JSON-safe encoding for binary data
- Easy to transmit over HTTP
- Compatible with all programming languages
- Can be directly embedded in JSON without escaping issues
Decoding Base64 to Word Document:
JavaScript/Node.js:
const base64 = response.document;
const binary = atob(base64); // Browser
// OR
const binary = Buffer.from(base64, 'base64'); // Node.js
Python:
import base64
word_bytes = base64.b64decode(response['document'])
with open('output.docx', 'wb') as f:
f.write(word_bytes)
C#:
byte[] wordBytes = Convert.FromBase64String(response.document);
File.WriteAllBytes("output.docx", wordBytes);
Request Example
Header
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 useAuthorization: Basic YWJjMTIz
Payload
Basic Example (Two Documents):
{
"documents": [
{ "filename": "doc1.docx", "docContent": "base64EncodedDocument1" },
{ "filename": "doc2.docx", "docContent": "base64EncodedDocument2" }
]
}
Advanced Example (Multiple Documents with Merge Options):
{
"documents": [
{ "filename": "doc1.docx", "docContent": "base64EncodedDocument1" },
{ "filename": "doc2.docx", "docContent": "base64EncodedDocument2" },
{ "filename": "doc3.docx", "docContent": "base64EncodedDocument3" }
],
"mergeOptions": {}
}
Code Samples
The PDF4me Merge Documents REST API provides code samples in multiple programming languages. Choose the language that best fits your development environment:
- C#
- Java
- JavaScript
- Python
- Salesforce
- n8n
- Google Script
- AWS Lambda
Google Script Sample
Google Apps Script implementation for Google Workspace integration:
Industry Use Cases & Applications
- Legal & Professional Services
- Business & Enterprise
- Education & Research
- Finance & Banking
Legal & Professional Services Use Cases
- Contract Assembly: Combine contract sections into complete legal documents
- Legal Brief Consolidation: Merge multiple brief sections into comprehensive legal briefs
- Compliance Documentation: Consolidate compliance documents for regulatory submissions
- Client Document Assembly: Combine client-specific documents into complete packages
Business & Enterprise Use Cases
- Proposal Assembly: Combine proposal sections into complete client proposals
- Marketing Material Consolidation: Merge marketing documents for campaigns
- Sales Presentation Assembly: Combine presentation sections into complete presentations
- Client Deliverable Assembly: Consolidate client-specific materials
Education & Research Use Cases
- Medical Report Assembly: Combine medical report sections into complete patient reports
- Protocol Documentation: Merge protocol sections into comprehensive medical protocols
- Research Documentation: Consolidate research documents for study reports
- Compliance Manual Assembly: Combine healthcare compliance documents
Finance & Banking Use Cases
- Financial Report Assembly: Merge financial report sections into complete reports
- Audit Documentation: Consolidate audit documents for comprehensive audit reports
- Budget Consolidation: Combine budget sections from different departments
- Regulatory Filing Assembly: Merge regulatory documents for submission