Skip to main content
Glama

pdf_add_form_field

Add interactive form fields to PDF documents by specifying field type, position, and dimensions to create fillable forms.

Instructions

Add a form field to a PDF.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathYes
page_numberYes
field_typeYes
field_nameYes
xYes
yYes
widthYes
heightYes
optionsNo

Implementation Reference

  • The primary handler function for the 'pdf_add_form_field' tool, decorated with @mcp.tool() for automatic registration and schema inference from type hints. Implements adding form fields (text, checkbox, radio, combobox) to a PDF page using PyMuPDF.
    async def pdf_add_form_field( pdf_path: str, page_number: int, field_type: str, field_name: str, x: float, y: float, width: float, height: float, options: List[str] = None ) -> str: """Add a form field to a PDF.""" if options is None: options = [] 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] # Create rectangle for form field rect = fitz.Rect(x, y, x + width, y + height) # Add form field based on type if field_type == "text": widget = page.add_textfield(rect, field_name) elif field_type == "checkbox": widget = page.add_checkbox(rect, field_name) elif field_type == "radio": widget = page.add_radio_button(rect, field_name) elif field_type == "combobox": widget = page.add_combobox(rect, field_name, options) else: doc.close() return f"Error: Invalid field type: {field_type}" # Update widget appearance widget.update() # Generate output filename output_path = generate_output_filename(pdf_path) # Save the modified PDF doc.save(output_path) doc.close() return f"Successfully added {field_type} form field '{field_name}' to PDF. Output saved to: {output_path}" except Exception as e: return f"Error adding form field to PDF: {str(e)}"
  • Helper utility to validate if the input PDF file is valid 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 utility to check if the specified page number is valid within the 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)
  • Helper utility to generate a unique output filename with timestamp to prevent overwriting the original PDF.
    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}")

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