Optimize Document - Document Optimization API
PDF4me Optimize Document enables you to optimize Word documents for size and performance with three different compression levels and comprehensive optimization techniques. This API service processes Word files and reduces file size, compresses images, cleans up content, normalizes formatting, and removes personal information. The API receives Word document content through REST API calls, utilizing Base64 encoding for secure transmission. With support for Low, Medium, and High optimization levels achieving 0-90% file size reduction, this solution is ideal for document preparation, storage optimization, and performance improvement 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
- Maximum Optimization: Max compression profile for maximum file size reduction
- File Size Reduction: Achieve 80-90% file size reduction with Max optimization
- Image Compression: Intelligent image optimization with quality preservation
- Content Cleanup: Remove unused macros, comments, and hidden content
- Formatting Normalization: Clean up redundant formatting and unused styles
- Personal Information Removal: Remove author, manager, and company information
- Font Optimization: Normalize fonts and remove unnecessary font properties
- Compatibility Enhancement: Optimize documents for Word 2016 compatibility
REST API Endpoint
The PDF4me REST API uses standard HTTP methods to interact with resources. All document optimization operations are performed through a single endpoint:
- Method: POST
- Endpoint:
office/ApiV2Word/OptimizeDocument
REST API Parameters
Complete list of parameters for the Optimize Document REST API. Parameters are organized by category for better understanding and implementation.
Important: Parameters marked with an asterisk (*) are required. Optimization level determines the aggressiveness of optimization techniques applied.
Required Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| document* | Object | Document reference. Must contain Name (string) — Word file name with .docx extension | { "Name": "document.docx" } |
| docContent* | Base64 | Word document content encoded in Base64. Original document is not modified - new optimized version created | UEsDBBQABgAIAAAA... |
Optional Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| optimizationLevel | String | Optimization level (e.g. Low, Medium, Max). Max - Maximum compression (~80-90%). Controls aggressiveness of optimization techniques | "Max" |
| wordVersion | String | Target Word version for compatibility | null |
| cultureName | String | Culture code for document processing (e.g., "en-US", "de-DE", "fr-FR") | en-US |
Optimization Profile
The Max optimization profile provides maximum compression:
- File Size Reduction: 80-90%
- Image Quality: Optimized for size
- Formatting: Aggressive cleanup
- Comments: Removed
- Revisions: Removed
- Personal Info: Removed
- Macros: Removed
- Unused Styles: Removed
- Hidden Content: Removed
Output
The PDF4me Optimize Document 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": "document.docx",
"success": true,
"errorMessage": null
}
Response Fields:
- document (string): The optimized Word document with reduced file size, 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 (
documentwithName,docContent) - Invalid Base64 encoding in
docContent - Invalid or corrupted Word document
- Invalid optimization level
- 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 optimization failure
- Error applying compression settings
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 (Default Max Optimization):
{
"document": { "Name": "document.docx" },
"docContent": "base64EncodedDocumentContent",
"optimizationLevel": "Max"
}
Advanced Example (With Optional Fields):
{
"document": { "Name": "document.docx" },
"docContent": "base64EncodedDocumentContent",
"optimizationLevel": "Max",
"wordVersion": null,
"cultureName": "en-US"
}
Code Samples
The PDF4me Optimize Document 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
- Document Distribution: Optimize legal documents for client distribution
- Public Filing: Prepare documents for public court filings
- Archive Storage: Optimize legal documents for long-term storage
- Compliance Documents: Clean up documents for regulatory submissions
Business & Enterprise Use Cases
- Proposal Distribution: Optimize proposals for client delivery
- Marketing Material Sharing: Optimize marketing documents for public distribution
- Presentation Optimization: Optimize presentations for email distribution
- Client Deliverable Preparation: Clean up documents for client delivery
Education & Research Use Cases
- Patient Report Distribution: Optimize medical reports for secure sharing
- Research Documentation: Optimize research documents for publication
- Protocol Sharing: Optimize medical protocols for department distribution
- Compliance Documentation: Clean up documents for regulatory submissions
Finance & Banking Use Cases
- Financial Report Distribution: Optimize financial reports for stakeholder distribution
- Audit Documentation: Clean up audit documents for external sharing
- Budget Document Sharing: Optimize budget documents for department distribution
- Regulatory Filing: Prepare documents for regulatory submissions