highlight_form_field
Highlight a specific form field in a PDF by generating an image with the field outlined in a red box. This tool helps users easily identify and locate form fields within PDF documents for better visualization and processing.
Instructions
Generate an image with the specified form field highlighted with a red box
Args:
pdf_path: Path to the PDF file
field_name: Name of the form field to highlight
Returns:
Image of the page with the field highlighted
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| field_name | Yes | ||
| pdf_path | Yes |
Implementation Reference
- mcp_pdf_forms/server.py:133-202 (handler)The handler function for the 'highlight_form_field' tool. It locates the specified form field in the PDF, renders the page at 2x zoom, draws a thick red rectangle around the field using PIL, and returns the image as a PNG in MCP Image format.@mcp.tool() def highlight_form_field(pdf_path: str, field_name: str) -> Image: """ Generate an image with the specified form field highlighted with a red box Args: pdf_path: Path to the PDF file field_name: Name of the form field to highlight Returns: Image of the page with the field highlighted """ try: doc = fitz.open(pdf_path) # Find the field and its page field_found = False field_page = None field_rect = None for page_num, page in enumerate(doc): for widget in page.widgets(): if widget.field_name == field_name: field_found = True field_page = page field_rect = widget.rect break if field_found: break if not field_found: raise ValueError(f"Field '{field_name}' not found in the document") # Render the page as an image zoom = 2 # higher zoom for better quality mat = fitz.Matrix(zoom, zoom) pix = field_page.get_pixmap(matrix=mat) # Convert to PIL Image img = PILImage.frombytes("RGB", [pix.width, pix.height], pix.samples) # Draw red rectangle around the form field draw = ImageDraw.Draw(img) # Scale rectangle coordinates according to zoom factor rect = ( field_rect.x0 * zoom, field_rect.y0 * zoom, field_rect.x1 * zoom, field_rect.y1 * zoom, ) # Draw rectangle with 3-pixel width for i in range(3): draw.rectangle( (rect[0] - i, rect[1] - i, rect[2] + i, rect[3] + i), outline="red" ) # Convert to bytes buffer = io.BytesIO() img.save(buffer, format="PNG") img_bytes = buffer.getvalue() doc.close() # Return MCP Image object return Image(data=img_bytes, format="png") except Exception as e: raise Exception(f"Error highlighting form field: {str(e)}")