modify_pdf_content
Replace text in PDF documents with optional regex matching and hyperlink support, preserving original font styling.
Instructions
Find and replace text in a PDF while preserving font styles.
This tool performs text replacement by:
Locating all occurrences of the search text
Redacting the original text (white fill)
Inserting the replacement text with matched styling
IMPORTANT BEHAVIORS:
Text is matched within individual text spans
Font style is approximated using Base 14 fonts (Helvetica, Times, Courier)
Replacement text should be similar length to avoid overlap
Multiple replacements can be specified in a single call
HYPERLINK SUPPORT:
Append "|URL" to create a clickable link: "Click Here|https://example.com"
Use "|void(0)" to neutralize existing links: "Product|void(0)"
REGEX SUPPORT:
Set use_regex=true to treat keys as regex patterns
Useful for matching dates, IDs, or variable content
Example: {"Order #\d+": "Order #REDACTED"}
Args: input_path: Absolute path to the source PDF file. output_path: Absolute path where the modified PDF will be saved. Parent directory must exist. replacements: Dictionary mapping old text to new text. Keys are search strings (or regex if use_regex=true). Values are replacement strings (optionally with |URL). use_regex: If true, treat replacement keys as regex patterns. Default is false for literal string matching. password: Optional password if the source PDF is encrypted.
Returns: JSON string with modification results including: - success: boolean indicating if operation completed - replacements_made: count of text spans modified - pages_modified: count of pages with changes - warnings: any non-fatal issues encountered
Examples: # Simple text replacement modify_pdf_content( "/path/input.pdf", "/path/output.pdf", {"$99.99": "$149.99", "Draft": "Final"} )
# Regex replacement for dates
modify_pdf_content(
"/path/input.pdf",
"/path/output.pdf",
{"\d{2}/\d{2}/\d{4}": "01/01/2025"},
use_regex=True
)
# Create hyperlink
modify_pdf_content(
"/path/input.pdf",
"/path/output.pdf",
{"Learn More": "Visit Website|https://example.com"}
)Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input_path | Yes | ||
| output_path | Yes | ||
| replacements | Yes | ||
| use_regex | No | ||
| password | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |