Skip to main content
Glama

pdf_add_text

Add custom text to specific positions on PDF pages. Insert annotations, labels, or comments at precise coordinates to modify PDF documents with targeted text placement.

Instructions

Add text to a PDF at a specified position.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathYes
page_numberYes
textYes
xYes
yYes
font_sizeNo
colorNo

Implementation Reference

  • The core handler function for the 'pdf_add_text' tool. It validates input, opens the PDF with PyMuPDF, inserts text at (x,y) with specified font size and color on the given page, generates timestamped output file, saves, and returns success message with path.
    @mcp.tool() async def pdf_add_text( pdf_path: str, page_number: int, text: str, x: float, y: float, font_size: int = 12, color: List[float] = None ) -> str: """Add text to a PDF at a specified position.""" if color is None: color = [0, 0, 0] if not os.path.exists(pdf_path): return f"Error: PDF file not found: {pdf_path}" if not validate_pdf_file(pdf_path): return f"Error: Invalid PDF file: {pdf_path}" try: # Open PDF document doc = fitz.open(pdf_path) # Validate page number if not validate_page_number(doc, page_number): doc.close() return f"Error: Invalid page number {page_number}. Document has {len(doc)} pages." # Get the page page = doc[page_number] # Add text to the page page.insert_text( (x, y), text, fontsize=font_size, color=color ) # Generate output filename output_path = generate_output_filename(pdf_path) # Save the modified PDF doc.save(output_path) doc.close() return f"Successfully added text to PDF. Output saved to: {output_path}" except Exception as e: return f"Error adding text to PDF: {str(e)}"
  • Helper utility to create a timestamped output filename based on input path to prevent overwriting originals.
    def generate_output_filename(input_path: str, suffix: str = "modified") -> str: """Generate a new filename with timestamp to avoid overwriting originals.""" path = Path(input_path) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") return str(path.parent / f"{path.stem}_{suffix}_{timestamp}{path.suffix}")
  • Helper to validate if a file is a valid PDF by attempting to open it with PyMuPDF.
    def validate_pdf_file(pdf_path: str) -> bool: """Validate that the file is a valid PDF.""" try: doc = fitz.open(pdf_path) doc.close() return True except Exception: return False
  • Helper to check if a 0-based page number is valid for the given PDF document.
    def validate_page_number(doc: fitz.Document, page_num: int) -> bool: """Validate that the page number exists in the document.""" return 0 <= page_num < len(doc)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/andr3medeiros/pdf-manipulation-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server