Skip to main content
Glama

highlight_form_field

Visually identify form fields in PDF documents by generating images with specified fields highlighted in red boxes for easy location and verification.

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
NameRequiredDescriptionDefault
pdf_pathYes
field_nameYes

Implementation Reference

  • The main handler function for the 'highlight_form_field' tool. It opens the PDF using PyMuPDF, locates the specified form field on its page, renders the page at 2x zoom, draws a thick red rectangle around the field using PIL, and returns the resulting PNG image via MCP's Image type. The @mcp.tool() decorator registers it as a tool.
    @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)}")

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/Wildebeest/mcp_pdf_forms'

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