Skip to main content
This document continues from the previous section, explaining how to process parsing results after obtaining them and draw text boxes at corresponding positions on the original file.
This document uses this sample document as an example to explain how to draw text boxes on the original file image.
Please install dependencies before running the example code: pip install PyMuPDF pillow requests

Example Code

In the output directory, you can see the rendering effect of text boxes: visualization

Example Code Logic

  • Get parsing results: Call the result retrieval interface with with_document=true enabled to get result.files[].document.
  • Extract pages and coordinates: Read width/height/angle and lines[] from document.pages[]; each line.position is [x1,y1,x2,y2,x3,y3,x4,y4] clockwise four-point coordinates.
  • Prepare base image: Use the same file as parsing to generate page images. If converting images yourself, remember to record the rendered img_width/img_height.
  • Coordinate scaling: Calculate scale_x = img_width / page.width, scale_y = img_height / page.height, and scale the returned coordinates proportionally to the base image pixel coordinate system.
  • Draw visualization: Draw the scaled four points as closed polylines (or filled polygons) on the corresponding page image line by line, with configurable line width, color, and transparency.
  • Output and display: Save annotated images, or overlay rendering in frontend canvas/SVG/Canvas; ensure page-by-page correspondence.
  • Optional enhancements:
    • Handle angle rotation: If the base image is not rotated correctly, coordinate rotation correction based on page.angle is needed.
    • Multi-type coloring: Different element types can use different colors/legends; if there are table, image, and other types, they can be differentiated as needed.
    • Character-level highlighting: If charPositions is returned, scale each character coordinate proportionally and draw for more refined echo effects.
    • Performance optimization: Batch drawing, resolution trade-offs, on-demand page rendering to avoid processing too many pages at once causing lag.
    • Robustness: Null value checking, coordinate boundary clipping, fault tolerance for network/parsing exceptions.
The above process is language-agnostic. When implementing in other languages, you only need to replace the libraries and APIs for: HTTP requests, JSON parsing, image drawing, and coordinate scaling.