Update Table of Contents - TOC Management API
PDF4me Update Table of Contents enables you to update or create Table of Contents in Word documents with comprehensive formatting options and professional styling capabilities. This API service processes Word files and generates TOCs with automatic detection of existing TOC fields, customizable heading levels, hyperlink support, tab leader control, text case options, and culture localization. The API receives Word document content through REST API calls, utilizing Base64 encoding for secure transmission. With full control over TOC appearance and functionality, this solution is ideal for document navigation, professional formatting, and automated TOC generation 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
- Automatic TOC Detection: Finds and updates existing TOC fields or creates new ones
- Customizable Heading Levels: Control which heading levels to include (1-6)
- Hyperlink Support: Enable or disable clickable links in the TOC
- Tab Leader Control: Show or hide dots between text and page numbers
- Text Case Options: Use uppercase or preserve original case
- Culture Localization: Support for different languages and regions
- Professional Formatting: Automatic application of professional TOC styling
- Page Number Integration: Automatic page number calculation and updates
REST API Endpoint
The PDF4me REST API uses standard HTTP methods to interact with resources. All table of contents update operations are performed through a single endpoint:
- Method: POST
- Endpoint:
office/ApiV2Word/UpdateTableOfContents
REST API Parameters
Complete list of parameters for the Update Table of Contents 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 TOC formatting and appearance.
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. TOC is generated from this document's headings. Must be valid Word document (.docx, .doc formats) | UEsDBBQABgAIAAAA... |
Optional Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| headingLevels | String | Heading levels to include in TOC. Range format: "1-3" (includes heading levels 1, 2, and 3). Single level: "1". Multiple ranges: "1-2,4-6". Default: "1-3". Maps to Word's built-in heading styles. Scans document for existing TOC fields. Creates new TOC at document start if none exists | "1-3" |
| useHyperlinks | Boolean | Include hyperlinks in Table of Contents. true = TOC entries are clickable links (default), false = plain text | true |
| hideTabLeader | Boolean | Hide tab leader dots. true = No dots between text and page numbers (default), false = Dots between text and page numbers | true |
| useUppercase | Boolean | Use uppercase for TOC entries. true = TOC entries in uppercase (default), false = original case | true |
| cultureName | String | Culture code for document processing (e.g., "en-US", "de-DE", "fr-FR"). Default: null. Affects document language and formatting | en-US |
TOC Generation Behavior
- Existing TOC Detection: Scans document for existing TOC fields (FieldType.FieldTOC) and updates them
- New TOC Creation: If no TOC exists, creates new TOC at document start
- Heading Level Mapping: Maps to Word's built-in heading styles (Heading 1, Heading 2, etc.)
- Field Updates: Updates fields multiple times for accuracy
- Page Layout: Calls UpdateFields() and UpdatePageLayout() in sequence
Output
The PDF4me Update Table of Contents 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 Word document with updated table of contents, 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
- No headings found in document
- Invalid heading levels specified (invalid format)
- Invalid culture name provided
- 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
- Table of contents update failure
- Error converting document to bytes
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 Settings):
{
"document": { "Name": "document.docx" },
"docContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC..."
}
Advanced Example (With All Options):
{
"document": { "Name": "document.docx" },
"docContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"headingLevels": "1-4",
"useHyperlinks": true,
"hideTabLeader": false,
"useUppercase": false,
"cultureName": "en-US"
}
Academic Paper Example:
{
"document": { "Name": "research-paper.docx" },
"docContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"headingLevels": "1-3",
"useHyperlinks": true,
"hideTabLeader": true,
"useUppercase": true
}
Code Samples
The PDF4me Update Table of Contents 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
C# (CSharp) Sample
Complete C# implementation for updating table of contents in Word documents:
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 Navigation: Create detailed TOC for complex contracts
- Legal Briefs: Generate navigable structure for court documents
- Compliance Manuals: Index regulatory requirements and procedures
- Case Documentation: Organize evidence and legal arguments
Business & Enterprise Use Cases
- Policy Manuals: Index internal procedures and policies
- Proposal Structure: Create navigable structure for proposals and RFPs
- Training Materials: Organize internal training and onboarding docs
- Project Documentation: Structure project plans and deliverables
Education & Research Use Cases
- Textbook Indexing: Create comprehensive TOC for educational materials
- Research Papers: Standardize academic document structure
- Medical and Clinical Manuals: Index procedures, protocols, and study materials
- Thesis Documentation: Structure graduate and doctoral work
Finance & Banking Use Cases
- Financial Reports: Create navigable structure for complex reports
- Policy Manuals: Index banking procedures and regulations
- Audit Documentation: Organize compliance and audit materials
- Investment Guides: Structure financial education content