@panda-agent/panda-cli 0.1.29 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/pandacli.mjs +6 -1
- package/bundled-preset-skills/.gitkeep +0 -0
- package/bundled-preset-skills/README.md +17 -0
- package/bundled-preset-skills/docx/.skill-metadata.yaml +173 -0
- package/bundled-preset-skills/docx/LICENSE.txt +30 -0
- package/bundled-preset-skills/docx/SKILL.md +589 -0
- package/bundled-preset-skills/docx/scripts/__init__.py +1 -0
- package/bundled-preset-skills/docx/scripts/accept_changes.py +206 -0
- package/bundled-preset-skills/docx/scripts/comment.py +442 -0
- package/bundled-preset-skills/docx/scripts/office/helpers/__init__.py +1 -0
- package/bundled-preset-skills/docx/scripts/office/helpers/merge_runs.py +190 -0
- package/bundled-preset-skills/docx/scripts/office/helpers/simplify_redlines.py +185 -0
- package/bundled-preset-skills/docx/scripts/office/pack.py +167 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/mce/mc.xsd +75 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/bundled-preset-skills/docx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/bundled-preset-skills/docx/scripts/office/soffice.py +194 -0
- package/bundled-preset-skills/docx/scripts/office/unpack.py +145 -0
- package/bundled-preset-skills/docx/scripts/office/validate.py +114 -0
- package/bundled-preset-skills/docx/scripts/office/validators/__init__.py +16 -0
- package/bundled-preset-skills/docx/scripts/office/validators/base.py +733 -0
- package/bundled-preset-skills/docx/scripts/office/validators/docx.py +354 -0
- package/bundled-preset-skills/docx/scripts/office/validators/pptx.py +230 -0
- package/bundled-preset-skills/docx/scripts/office/validators/redlining.py +212 -0
- package/bundled-preset-skills/docx/scripts/templates/comments.xml +3 -0
- package/bundled-preset-skills/docx/scripts/templates/commentsExtended.xml +3 -0
- package/bundled-preset-skills/docx/scripts/templates/commentsExtensible.xml +3 -0
- package/bundled-preset-skills/docx/scripts/templates/commentsIds.xml +3 -0
- package/bundled-preset-skills/docx/scripts/templates/people.xml +3 -0
- package/bundled-preset-skills/frontend-design/LICENSE.txt +177 -0
- package/bundled-preset-skills/frontend-design/SKILL.md +42 -0
- package/bundled-preset-skills/pdf/.skill-metadata.yaml +273 -0
- package/bundled-preset-skills/pdf/LICENSE.txt +30 -0
- package/bundled-preset-skills/pdf/SKILL.md +324 -0
- package/bundled-preset-skills/pdf/advanced-reference.md +609 -0
- package/bundled-preset-skills/pdf/form-filling-guide.md +318 -0
- package/bundled-preset-skills/pdf/forms.md +294 -0
- package/bundled-preset-skills/pdf/reference.md +612 -0
- package/bundled-preset-skills/pdf/scripts/check_bounding_boxes.py +198 -0
- package/bundled-preset-skills/pdf/scripts/check_fillable_fields.py +64 -0
- package/bundled-preset-skills/pdf/scripts/convert_pdf_to_images.py +102 -0
- package/bundled-preset-skills/pdf/scripts/create_validation_image.py +125 -0
- package/bundled-preset-skills/pdf/scripts/extract_form_field_info.py +220 -0
- package/bundled-preset-skills/pdf/scripts/extract_form_structure.py +202 -0
- package/bundled-preset-skills/pdf/scripts/fill_fillable_fields.py +205 -0
- package/bundled-preset-skills/pdf/scripts/fill_pdf_form_with_annotations.py +193 -0
- package/bundled-preset-skills/pptx-generator/SKILL.md +204 -0
- package/bundled-preset-skills/pptx-generator/assets/styles/business.json +8 -0
- package/bundled-preset-skills/pptx-generator/assets/styles/minimal.json +8 -0
- package/bundled-preset-skills/pptx-generator/assets/styles/modern.json +8 -0
- package/bundled-preset-skills/pptx-generator/assets/templates/ppt_data_template.json +40 -0
- package/bundled-preset-skills/pptx-generator/references/collaboration_guide.md +381 -0
- package/bundled-preset-skills/pptx-generator/references/json_format_spec.md +215 -0
- package/bundled-preset-skills/pptx-generator/references/layout_guide.md +290 -0
- package/bundled-preset-skills/pptx-generator/scripts/json_validator.py +194 -0
- package/bundled-preset-skills/pptx-generator/scripts/pptx_builder.py +340 -0
- package/bundled-preset-skills/pptx-generator/scripts/pptx_validator.py +162 -0
- package/bundled-preset-skills/skill-creator/LICENSE.txt +202 -0
- package/bundled-preset-skills/skill-creator/SKILL.md +479 -0
- package/bundled-preset-skills/skill-creator/agents/analyzer.md +274 -0
- package/bundled-preset-skills/skill-creator/agents/comparator.md +202 -0
- package/bundled-preset-skills/skill-creator/agents/grader.md +223 -0
- package/bundled-preset-skills/skill-creator/assets/eval_review.html +146 -0
- package/bundled-preset-skills/skill-creator/eval-viewer/generate_review.py +471 -0
- package/bundled-preset-skills/skill-creator/eval-viewer/viewer.html +1325 -0
- package/bundled-preset-skills/skill-creator/references/schemas.md +430 -0
- package/bundled-preset-skills/skill-creator/scripts/__init__.py +0 -0
- package/bundled-preset-skills/skill-creator/scripts/aggregate_benchmark.py +401 -0
- package/bundled-preset-skills/skill-creator/scripts/generate_report.py +326 -0
- package/bundled-preset-skills/skill-creator/scripts/improve_description.py +248 -0
- package/bundled-preset-skills/skill-creator/scripts/package_skill.py +136 -0
- package/bundled-preset-skills/skill-creator/scripts/quick_validate.py +103 -0
- package/bundled-preset-skills/skill-creator/scripts/run_eval.py +310 -0
- package/bundled-preset-skills/skill-creator/scripts/run_loop.py +332 -0
- package/bundled-preset-skills/skill-creator/scripts/utils.py +47 -0
- package/bundled-preset-skills/xlsx/.skill-metadata.yaml +185 -0
- package/bundled-preset-skills/xlsx/LICENSE.txt +30 -0
- package/bundled-preset-skills/xlsx/SKILL.md +233 -0
- package/bundled-preset-skills/xlsx/scripts/office/helpers/__init__.py +1 -0
- package/bundled-preset-skills/xlsx/scripts/office/helpers/merge_runs.py +226 -0
- package/bundled-preset-skills/xlsx/scripts/office/helpers/simplify_redlines.py +198 -0
- package/bundled-preset-skills/xlsx/scripts/office/pack.py +162 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/mce/mc.xsd +75 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/bundled-preset-skills/xlsx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/bundled-preset-skills/xlsx/scripts/office/soffice.py +185 -0
- package/bundled-preset-skills/xlsx/scripts/office/unpack.py +146 -0
- package/bundled-preset-skills/xlsx/scripts/office/validate.py +108 -0
- package/bundled-preset-skills/xlsx/scripts/office/validators/__init__.py +13 -0
- package/bundled-preset-skills/xlsx/scripts/office/validators/base.py +800 -0
- package/bundled-preset-skills/xlsx/scripts/office/validators/docx.py +383 -0
- package/bundled-preset-skills/xlsx/scripts/office/validators/pptx.py +250 -0
- package/bundled-preset-skills/xlsx/scripts/office/validators/redlining.py +229 -0
- package/bundled-preset-skills/xlsx/scripts/recalc.py +296 -0
- package/dist/panda-cli-ink.bundle.mjs +276 -342
- package/package.json +6 -4
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# PDF Form Filling Guide
|
|
2
|
+
|
|
3
|
+
**CRITICAL: You MUST complete these steps in order. Do not skip ahead to writing code.**
|
|
4
|
+
|
|
5
|
+
Before attempting to fill a PDF form, determine whether it contains interactive fields. Execute the following from this file's directory:
|
|
6
|
+
`python scripts/check_fillable_fields.py <file.pdf>`, then proceed to either the "Interactive Form Fields" or "Static Form Layout" section based on the output.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Interactive Form Fields
|
|
11
|
+
|
|
12
|
+
When the PDF contains native fillable widgets:
|
|
13
|
+
|
|
14
|
+
### Step 1: Extract Field Metadata
|
|
15
|
+
|
|
16
|
+
Run (from this file's directory):
|
|
17
|
+
`python scripts/extract_form_field_info.py <input.pdf> <field_info.json>`
|
|
18
|
+
|
|
19
|
+
The resulting JSON uses this schema:
|
|
20
|
+
```json
|
|
21
|
+
[
|
|
22
|
+
{
|
|
23
|
+
"field_id": "(canonical identifier for the widget)",
|
|
24
|
+
"page": "(1-indexed page number)",
|
|
25
|
+
"rect": "([left, bottom, right, top] in PDF coordinate space where y=0 is page bottom)",
|
|
26
|
+
"type": "text"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"field_id": "(canonical identifier)",
|
|
30
|
+
"page": "(1-indexed page number)",
|
|
31
|
+
"type": "checkbox",
|
|
32
|
+
"checked_value": "(assign this to activate the checkbox)",
|
|
33
|
+
"unchecked_value": "(assign this to deactivate the checkbox)"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"field_id": "(canonical identifier)",
|
|
37
|
+
"page": "(1-indexed page number)",
|
|
38
|
+
"type": "radio_group",
|
|
39
|
+
"radio_options": [
|
|
40
|
+
{
|
|
41
|
+
"value": "(assign this to select the option)",
|
|
42
|
+
"rect": "(bounding rectangle for this specific button)"
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"field_id": "(canonical identifier)",
|
|
48
|
+
"page": "(1-indexed page number)",
|
|
49
|
+
"type": "choice",
|
|
50
|
+
"choice_options": [
|
|
51
|
+
{
|
|
52
|
+
"value": "(assign this to pick the item)",
|
|
53
|
+
"text": "(human-readable label)"
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Step 2: Visual Inspection
|
|
61
|
+
|
|
62
|
+
Render the document as page images (run from this file's directory):
|
|
63
|
+
`python scripts/convert_pdf_to_images.py <file.pdf> <output_directory>`
|
|
64
|
+
|
|
65
|
+
Inspect each image to understand what data each widget expects (remember to map PDF bounding box coordinates to image pixel space).
|
|
66
|
+
|
|
67
|
+
### Step 3: Assemble Field Values
|
|
68
|
+
|
|
69
|
+
Build a `field_values.json` specifying data for each widget:
|
|
70
|
+
```json
|
|
71
|
+
[
|
|
72
|
+
{
|
|
73
|
+
"field_id": "last_name",
|
|
74
|
+
"description": "The user's last name",
|
|
75
|
+
"page": 1,
|
|
76
|
+
"value": "Simpson"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"field_id": "Checkbox12",
|
|
80
|
+
"description": "Checkbox to be checked if the user is 18 or over",
|
|
81
|
+
"page": 1,
|
|
82
|
+
"value": "/On"
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Notes:
|
|
88
|
+
- `field_id` must correspond to the extraction step output
|
|
89
|
+
- `page` must align with the page number from field_info.json
|
|
90
|
+
- For checkboxes, use `checked_value` to activate; for radio groups use one of the `value` entries from `radio_options`
|
|
91
|
+
|
|
92
|
+
### Step 4: Execute Fill Operation
|
|
93
|
+
|
|
94
|
+
Run (from this file's directory):
|
|
95
|
+
`python scripts/fill_fillable_fields.py <input.pdf> <field_values.json> <output.pdf>`
|
|
96
|
+
|
|
97
|
+
The script validates all identifiers and values; correct any reported errors and re-run.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Static Form Layout
|
|
102
|
+
|
|
103
|
+
When the document lacks interactive widgets, text annotations will be positioned manually. First attempt structural analysis (higher precision), falling back to visual methods when necessary.
|
|
104
|
+
|
|
105
|
+
### Phase 1: Structural Analysis
|
|
106
|
+
|
|
107
|
+
Execute the layout analyzer to locate text elements, ruling lines, and checkbox shapes with precise coordinates:
|
|
108
|
+
`python scripts/extract_form_structure.py <input.pdf> form_structure.json`
|
|
109
|
+
|
|
110
|
+
The output contains:
|
|
111
|
+
- **labels**: All text tokens with precise positioning (x0, top, x1, bottom in PDF points)
|
|
112
|
+
- **lines**: Horizontal rules defining row separators
|
|
113
|
+
- **checkboxes**: Small square shapes detected as tick-boxes (includes center coordinates)
|
|
114
|
+
- **row_boundaries**: Computed row extents derived from horizontal rules
|
|
115
|
+
|
|
116
|
+
**Decision point**: If `form_structure.json` yields meaningful text labels matching visible form fields, proceed with **Method A: Coordinate-Driven Placement**. For scanned/rasterized PDFs with sparse or garbled labels, use **Method B: Visual Positioning**.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
### Method A: Coordinate-Driven Placement (Preferred)
|
|
121
|
+
|
|
122
|
+
Applicable when `extract_form_structure.py` successfully identifies text labels.
|
|
123
|
+
|
|
124
|
+
#### A.1: Interpret the Layout Data
|
|
125
|
+
|
|
126
|
+
Examine form_structure.json to determine:
|
|
127
|
+
|
|
128
|
+
1. **Composite labels**: Adjacent tokens forming a single heading (e.g., "Last" + "Name")
|
|
129
|
+
2. **Row alignment**: Tokens sharing similar `top` values occupy the same horizontal band
|
|
130
|
+
3. **Entry zones**: Input areas begin after label endpoints (x0 = label.x1 + gap)
|
|
131
|
+
4. **Tick-boxes**: Use checkbox coordinates verbatim from the structural data
|
|
132
|
+
|
|
133
|
+
**Coordinate convention**: Origin at top-left corner; y increases downward.
|
|
134
|
+
|
|
135
|
+
#### A.2: Identify Structural Gaps
|
|
136
|
+
|
|
137
|
+
The analyzer may miss certain visual elements:
|
|
138
|
+
- **Round tick-boxes**: Only rectangular shapes are detected
|
|
139
|
+
- **Decorative graphics**: Non-standard form controls or ornamental elements
|
|
140
|
+
- **Low-contrast elements**: Faint or light-colored items may not register
|
|
141
|
+
|
|
142
|
+
For any form elements visible in rendered images but absent from form_structure.json, apply **visual analysis** for those specific items (see "Combined Approach" below).
|
|
143
|
+
|
|
144
|
+
#### A.3: Construct fields.json Using PDF Coordinates
|
|
145
|
+
|
|
146
|
+
Derive entry positions from the structural data:
|
|
147
|
+
|
|
148
|
+
**Text input areas:**
|
|
149
|
+
- entry x0 = label x1 + 5 (small gap after label)
|
|
150
|
+
- entry x1 = next label's x0, or row boundary
|
|
151
|
+
- entry top = same as label top
|
|
152
|
+
- entry bottom = row boundary line below, or label bottom + row_height
|
|
153
|
+
|
|
154
|
+
**Tick-boxes:**
|
|
155
|
+
- Use rectangle coordinates directly from form_structure.json
|
|
156
|
+
- entry_bounding_box = [checkbox.x0, checkbox.top, checkbox.x1, checkbox.bottom]
|
|
157
|
+
|
|
158
|
+
Build fields.json with `pdf_width` and `pdf_height` (indicating native coordinates):
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"pages": [
|
|
162
|
+
{"page_number": 1, "pdf_width": 612, "pdf_height": 792}
|
|
163
|
+
],
|
|
164
|
+
"form_fields": [
|
|
165
|
+
{
|
|
166
|
+
"page_number": 1,
|
|
167
|
+
"description": "Last name entry field",
|
|
168
|
+
"field_label": "Last Name",
|
|
169
|
+
"label_bounding_box": [43, 63, 87, 73],
|
|
170
|
+
"entry_bounding_box": [92, 63, 260, 79],
|
|
171
|
+
"entry_text": {"text": "Smith", "font_size": 10}
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
"page_number": 1,
|
|
175
|
+
"description": "US Citizen Yes checkbox",
|
|
176
|
+
"field_label": "Yes",
|
|
177
|
+
"label_bounding_box": [260, 200, 280, 210],
|
|
178
|
+
"entry_bounding_box": [285, 197, 292, 205],
|
|
179
|
+
"entry_text": {"text": "X"}
|
|
180
|
+
}
|
|
181
|
+
]
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**Key**: Specify `pdf_width`/`pdf_height` and use coordinates taken directly from form_structure.json.
|
|
186
|
+
|
|
187
|
+
#### A.4: Validate Geometry
|
|
188
|
+
|
|
189
|
+
Prior to filling, verify bounding box integrity:
|
|
190
|
+
`python scripts/check_bounding_boxes.py fields.json`
|
|
191
|
+
|
|
192
|
+
This detects overlapping rectangles and entry boxes too small for their font size. Resolve all reported issues before proceeding.
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
### Method B: Visual Positioning (Fallback)
|
|
197
|
+
|
|
198
|
+
Applicable when the PDF is rasterized/scanned and structural analysis yields no usable text (e.g., all content appears as "(cid:X)" patterns).
|
|
199
|
+
|
|
200
|
+
#### B.1: Render Document Pages
|
|
201
|
+
|
|
202
|
+
`python scripts/convert_pdf_to_images.py <input.pdf> <images_dir/>`
|
|
203
|
+
|
|
204
|
+
#### B.2: Rough Field Location
|
|
205
|
+
|
|
206
|
+
Scan each rendered page to catalogue form regions and approximate positions:
|
|
207
|
+
- Label text and estimated pixel locations
|
|
208
|
+
- Input zones (underlines, boxes, blank areas)
|
|
209
|
+
- Tick-boxes and their rough locations
|
|
210
|
+
|
|
211
|
+
Record approximate pixel coordinates for each element (precision not critical at this stage).
|
|
212
|
+
|
|
213
|
+
#### B.3: Precision Refinement via Cropping (ESSENTIAL)
|
|
214
|
+
|
|
215
|
+
For each field, isolate a region around the initial estimate to pin down exact coordinates.
|
|
216
|
+
|
|
217
|
+
**Produce a magnified crop with ImageMagick:**
|
|
218
|
+
```bash
|
|
219
|
+
magick <page_image> -crop <width>x<height>+<x>+<y> +repage <crop_output.png>
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Parameters:
|
|
223
|
+
- `<x>, <y>` = top-left of crop region (rough estimate minus padding)
|
|
224
|
+
- `<width>, <height>` = crop dimensions (field area plus ~50px padding per side)
|
|
225
|
+
|
|
226
|
+
**Example:** Refining a "Name" field estimated near (100, 150):
|
|
227
|
+
```bash
|
|
228
|
+
magick images_dir/page_1.png -crop 300x80+50+120 +repage crops/name_field.png
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
(Note: if the `magick` command isn't available, try `convert` with the same arguments).
|
|
232
|
+
|
|
233
|
+
**Analyze the crop** to determine exact boundaries:
|
|
234
|
+
1. Pinpoint where the input zone starts (after label)
|
|
235
|
+
2. Pinpoint where it ends (before next element or page edge)
|
|
236
|
+
3. Identify vertical extent of the input area
|
|
237
|
+
|
|
238
|
+
**Map crop-local coordinates back to full-page space:**
|
|
239
|
+
- full_x = crop_x + crop_offset_x
|
|
240
|
+
- full_y = crop_y + crop_offset_y
|
|
241
|
+
|
|
242
|
+
Example: Crop origin at (50, 120), entry box at (52, 18) within crop:
|
|
243
|
+
- entry_x0 = 52 + 50 = 102
|
|
244
|
+
- entry_top = 18 + 120 = 138
|
|
245
|
+
|
|
246
|
+
**Process each field**, batching adjacent elements into shared crops where feasible.
|
|
247
|
+
|
|
248
|
+
#### B.4: Build fields.json with Pixel Coordinates
|
|
249
|
+
|
|
250
|
+
Construct fields.json using `image_width` and `image_height` (indicating pixel-space coordinates):
|
|
251
|
+
```json
|
|
252
|
+
{
|
|
253
|
+
"pages": [
|
|
254
|
+
{"page_number": 1, "image_width": 1700, "image_height": 2200}
|
|
255
|
+
],
|
|
256
|
+
"form_fields": [
|
|
257
|
+
{
|
|
258
|
+
"page_number": 1,
|
|
259
|
+
"description": "Last name entry field",
|
|
260
|
+
"field_label": "Last Name",
|
|
261
|
+
"label_bounding_box": [120, 175, 242, 198],
|
|
262
|
+
"entry_bounding_box": [255, 175, 720, 218],
|
|
263
|
+
"entry_text": {"text": "Smith", "font_size": 10}
|
|
264
|
+
}
|
|
265
|
+
]
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Key**: Specify `image_width`/`image_height` and use refined pixel values from the cropping analysis.
|
|
270
|
+
|
|
271
|
+
#### B.5: Validate Geometry
|
|
272
|
+
|
|
273
|
+
Prior to filling, verify bounding box integrity:
|
|
274
|
+
`python scripts/check_bounding_boxes.py fields.json`
|
|
275
|
+
|
|
276
|
+
This detects overlapping rectangles and entry boxes too small for their font size. Resolve all reported issues before proceeding.
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
### Combined Approach: Structure + Visual
|
|
281
|
+
|
|
282
|
+
Applicable when structural analysis succeeds for most fields but misses certain elements (e.g., circular tick-boxes, non-standard controls).
|
|
283
|
+
|
|
284
|
+
1. **Apply Method A** for all elements present in form_structure.json
|
|
285
|
+
2. **Render pages** for visual inspection of missing elements
|
|
286
|
+
3. **Apply crop-based refinement** (from Method B) for undetected items
|
|
287
|
+
4. **Unify coordinate systems**: Structure-derived values are already in PDF space. For visually-estimated values, convert:
|
|
288
|
+
- pdf_x = image_x * (pdf_width / image_width)
|
|
289
|
+
- pdf_y = image_y * (pdf_height / image_height)
|
|
290
|
+
5. **Express everything in PDF coordinates** using `pdf_width`/`pdf_height` in the final fields.json
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## Pre-Fill Validation
|
|
295
|
+
|
|
296
|
+
**Always run geometry checks before filling:**
|
|
297
|
+
`python scripts/check_bounding_boxes.py fields.json`
|
|
298
|
+
|
|
299
|
+
Detects:
|
|
300
|
+
- Overlapping bounding boxes (would produce garbled text)
|
|
301
|
+
- Entry regions too small for the specified text size
|
|
302
|
+
|
|
303
|
+
Correct all flagged issues in fields.json before continuing.
|
|
304
|
+
|
|
305
|
+
## Apply Annotations
|
|
306
|
+
|
|
307
|
+
The annotation engine automatically handles coordinate system detection and conversion:
|
|
308
|
+
`python scripts/fill_pdf_form_with_annotations.py <input.pdf> fields.json <output.pdf>`
|
|
309
|
+
|
|
310
|
+
## Visual Confirmation
|
|
311
|
+
|
|
312
|
+
Render the completed PDF and inspect text placement:
|
|
313
|
+
`python scripts/convert_pdf_to_images.py <output.pdf> <verify_images/>`
|
|
314
|
+
|
|
315
|
+
Troubleshooting misaligned text:
|
|
316
|
+
- **Method A**: Confirm you used PDF coordinates from form_structure.json with `pdf_width`/`pdf_height`
|
|
317
|
+
- **Method B**: Verify image dimensions match and pixel coordinates are accurately measured
|
|
318
|
+
- **Combined**: Double-check conversion math for visually-estimated fields
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
**CRITICAL: You MUST complete these steps in order. Do not skip ahead to writing code.**
|
|
2
|
+
|
|
3
|
+
If you need to fill out a PDF form, first check to see if the PDF has fillable form fields. Run this script from this file's directory:
|
|
4
|
+
`python scripts/check_fillable_fields <file.pdf>`, and depending on the result go to either the "Fillable fields" or "Non-fillable fields" and follow those instructions.
|
|
5
|
+
|
|
6
|
+
# Fillable fields
|
|
7
|
+
If the PDF has fillable form fields:
|
|
8
|
+
- Run this script from this file's directory: `python scripts/extract_form_field_info.py <input.pdf> <field_info.json>`. It will create a JSON file with a list of fields in this format:
|
|
9
|
+
```
|
|
10
|
+
[
|
|
11
|
+
{
|
|
12
|
+
"field_id": (unique ID for the field),
|
|
13
|
+
"page": (page number, 1-based),
|
|
14
|
+
"rect": ([left, bottom, right, top] bounding box in PDF coordinates, y=0 is the bottom of the page),
|
|
15
|
+
"type": ("text", "checkbox", "radio_group", or "choice"),
|
|
16
|
+
},
|
|
17
|
+
// Checkboxes have "checked_value" and "unchecked_value" properties:
|
|
18
|
+
{
|
|
19
|
+
"field_id": (unique ID for the field),
|
|
20
|
+
"page": (page number, 1-based),
|
|
21
|
+
"type": "checkbox",
|
|
22
|
+
"checked_value": (Set the field to this value to check the checkbox),
|
|
23
|
+
"unchecked_value": (Set the field to this value to uncheck the checkbox),
|
|
24
|
+
},
|
|
25
|
+
// Radio groups have a "radio_options" list with the possible choices.
|
|
26
|
+
{
|
|
27
|
+
"field_id": (unique ID for the field),
|
|
28
|
+
"page": (page number, 1-based),
|
|
29
|
+
"type": "radio_group",
|
|
30
|
+
"radio_options": [
|
|
31
|
+
{
|
|
32
|
+
"value": (set the field to this value to select this radio option),
|
|
33
|
+
"rect": (bounding box for the radio button for this option)
|
|
34
|
+
},
|
|
35
|
+
// Other radio options
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
// Multiple choice fields have a "choice_options" list with the possible choices:
|
|
39
|
+
{
|
|
40
|
+
"field_id": (unique ID for the field),
|
|
41
|
+
"page": (page number, 1-based),
|
|
42
|
+
"type": "choice",
|
|
43
|
+
"choice_options": [
|
|
44
|
+
{
|
|
45
|
+
"value": (set the field to this value to select this option),
|
|
46
|
+
"text": (display text of the option)
|
|
47
|
+
},
|
|
48
|
+
// Other choice options
|
|
49
|
+
],
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
```
|
|
53
|
+
- Convert the PDF to PNGs (one image for each page) with this script (run from this file's directory):
|
|
54
|
+
`python scripts/convert_pdf_to_images.py <file.pdf> <output_directory>`
|
|
55
|
+
Then analyze the images to determine the purpose of each form field (make sure to convert the bounding box PDF coordinates to image coordinates).
|
|
56
|
+
- Create a `field_values.json` file in this format with the values to be entered for each field:
|
|
57
|
+
```
|
|
58
|
+
[
|
|
59
|
+
{
|
|
60
|
+
"field_id": "last_name", // Must match the field_id from `extract_form_field_info.py`
|
|
61
|
+
"description": "The user's last name",
|
|
62
|
+
"page": 1, // Must match the "page" value in field_info.json
|
|
63
|
+
"value": "Simpson"
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"field_id": "Checkbox12",
|
|
67
|
+
"description": "Checkbox to be checked if the user is 18 or over",
|
|
68
|
+
"page": 1,
|
|
69
|
+
"value": "/On" // If this is a checkbox, use its "checked_value" value to check it. If it's a radio button group, use one of the "value" values in "radio_options".
|
|
70
|
+
},
|
|
71
|
+
// more fields
|
|
72
|
+
]
|
|
73
|
+
```
|
|
74
|
+
- Run the `fill_fillable_fields.py` script from this file's directory to create a filled-in PDF:
|
|
75
|
+
`python scripts/fill_fillable_fields.py <input pdf> <field_values.json> <output pdf>`
|
|
76
|
+
This script will verify that the field IDs and values you provide are valid; if it prints error messages, correct the appropriate fields and try again.
|
|
77
|
+
|
|
78
|
+
# Non-fillable fields
|
|
79
|
+
If the PDF doesn't have fillable form fields, you'll add text annotations. First try to extract coordinates from the PDF structure (more accurate), then fall back to visual estimation if needed.
|
|
80
|
+
|
|
81
|
+
## Step 1: Try Structure Extraction First
|
|
82
|
+
|
|
83
|
+
Run this script to extract text labels, lines, and checkboxes with their exact PDF coordinates:
|
|
84
|
+
`python scripts/extract_form_structure.py <input.pdf> form_structure.json`
|
|
85
|
+
|
|
86
|
+
This creates a JSON file containing:
|
|
87
|
+
- **labels**: Every text element with exact coordinates (x0, top, x1, bottom in PDF points)
|
|
88
|
+
- **lines**: Horizontal lines that define row boundaries
|
|
89
|
+
- **checkboxes**: Small square rectangles that are checkboxes (with center coordinates)
|
|
90
|
+
- **row_boundaries**: Row top/bottom positions calculated from horizontal lines
|
|
91
|
+
|
|
92
|
+
**Check the results**: If `form_structure.json` has meaningful labels (text elements that correspond to form fields), use **Approach A: Structure-Based Coordinates**. If the PDF is scanned/image-based and has few or no labels, use **Approach B: Visual Estimation**.
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Approach A: Structure-Based Coordinates (Preferred)
|
|
97
|
+
|
|
98
|
+
Use this when `extract_form_structure.py` found text labels in the PDF.
|
|
99
|
+
|
|
100
|
+
### A.1: Analyze the Structure
|
|
101
|
+
|
|
102
|
+
Read form_structure.json and identify:
|
|
103
|
+
|
|
104
|
+
1. **Label groups**: Adjacent text elements that form a single label (e.g., "Last" + "Name")
|
|
105
|
+
2. **Row structure**: Labels with similar `top` values are in the same row
|
|
106
|
+
3. **Field columns**: Entry areas start after label ends (x0 = label.x1 + gap)
|
|
107
|
+
4. **Checkboxes**: Use the checkbox coordinates directly from the structure
|
|
108
|
+
|
|
109
|
+
**Coordinate system**: PDF coordinates where y=0 is at TOP of page, y increases downward.
|
|
110
|
+
|
|
111
|
+
### A.2: Check for Missing Elements
|
|
112
|
+
|
|
113
|
+
The structure extraction may not detect all form elements. Common cases:
|
|
114
|
+
- **Circular checkboxes**: Only square rectangles are detected as checkboxes
|
|
115
|
+
- **Complex graphics**: Decorative elements or non-standard form controls
|
|
116
|
+
- **Faded or light-colored elements**: May not be extracted
|
|
117
|
+
|
|
118
|
+
If you see form fields in the PDF images that aren't in form_structure.json, you'll need to use **visual analysis** for those specific fields (see "Hybrid Approach" below).
|
|
119
|
+
|
|
120
|
+
### A.3: Create fields.json with PDF Coordinates
|
|
121
|
+
|
|
122
|
+
For each field, calculate entry coordinates from the extracted structure:
|
|
123
|
+
|
|
124
|
+
**Text fields:**
|
|
125
|
+
- entry x0 = label x1 + 5 (small gap after label)
|
|
126
|
+
- entry x1 = next label's x0, or row boundary
|
|
127
|
+
- entry top = same as label top
|
|
128
|
+
- entry bottom = row boundary line below, or label bottom + row_height
|
|
129
|
+
|
|
130
|
+
**Checkboxes:**
|
|
131
|
+
- Use the checkbox rectangle coordinates directly from form_structure.json
|
|
132
|
+
- entry_bounding_box = [checkbox.x0, checkbox.top, checkbox.x1, checkbox.bottom]
|
|
133
|
+
|
|
134
|
+
Create fields.json using `pdf_width` and `pdf_height` (signals PDF coordinates):
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"pages": [
|
|
138
|
+
{"page_number": 1, "pdf_width": 612, "pdf_height": 792}
|
|
139
|
+
],
|
|
140
|
+
"form_fields": [
|
|
141
|
+
{
|
|
142
|
+
"page_number": 1,
|
|
143
|
+
"description": "Last name entry field",
|
|
144
|
+
"field_label": "Last Name",
|
|
145
|
+
"label_bounding_box": [43, 63, 87, 73],
|
|
146
|
+
"entry_bounding_box": [92, 63, 260, 79],
|
|
147
|
+
"entry_text": {"text": "Smith", "font_size": 10}
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
"page_number": 1,
|
|
151
|
+
"description": "US Citizen Yes checkbox",
|
|
152
|
+
"field_label": "Yes",
|
|
153
|
+
"label_bounding_box": [260, 200, 280, 210],
|
|
154
|
+
"entry_bounding_box": [285, 197, 292, 205],
|
|
155
|
+
"entry_text": {"text": "X"}
|
|
156
|
+
}
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Important**: Use `pdf_width`/`pdf_height` and coordinates directly from form_structure.json.
|
|
162
|
+
|
|
163
|
+
### A.4: Validate Bounding Boxes
|
|
164
|
+
|
|
165
|
+
Before filling, check your bounding boxes for errors:
|
|
166
|
+
`python scripts/check_bounding_boxes.py fields.json`
|
|
167
|
+
|
|
168
|
+
This checks for intersecting bounding boxes and entry boxes that are too small for the font size. Fix any reported errors before filling.
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Approach B: Visual Estimation (Fallback)
|
|
173
|
+
|
|
174
|
+
Use this when the PDF is scanned/image-based and structure extraction found no usable text labels (e.g., all text shows as "(cid:X)" patterns).
|
|
175
|
+
|
|
176
|
+
### B.1: Convert PDF to Images
|
|
177
|
+
|
|
178
|
+
`python scripts/convert_pdf_to_images.py <input.pdf> <images_dir/>`
|
|
179
|
+
|
|
180
|
+
### B.2: Initial Field Identification
|
|
181
|
+
|
|
182
|
+
Examine each page image to identify form sections and get **rough estimates** of field locations:
|
|
183
|
+
- Form field labels and their approximate positions
|
|
184
|
+
- Entry areas (lines, boxes, or blank spaces for text input)
|
|
185
|
+
- Checkboxes and their approximate locations
|
|
186
|
+
|
|
187
|
+
For each field, note approximate pixel coordinates (they don't need to be precise yet).
|
|
188
|
+
|
|
189
|
+
### B.3: Zoom Refinement (CRITICAL for accuracy)
|
|
190
|
+
|
|
191
|
+
For each field, crop a region around the estimated position to refine coordinates precisely.
|
|
192
|
+
|
|
193
|
+
**Create a zoomed crop using ImageMagick:**
|
|
194
|
+
```bash
|
|
195
|
+
magick <page_image> -crop <width>x<height>+<x>+<y> +repage <crop_output.png>
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Where:
|
|
199
|
+
- `<x>, <y>` = top-left corner of crop region (use your rough estimate minus padding)
|
|
200
|
+
- `<width>, <height>` = size of crop region (field area plus ~50px padding on each side)
|
|
201
|
+
|
|
202
|
+
**Example:** To refine a "Name" field estimated around (100, 150):
|
|
203
|
+
```bash
|
|
204
|
+
magick images_dir/page_1.png -crop 300x80+50+120 +repage crops/name_field.png
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
(Note: if the `magick` command isn't available, try `convert` with the same arguments).
|
|
208
|
+
|
|
209
|
+
**Examine the cropped image** to determine precise coordinates:
|
|
210
|
+
1. Identify the exact pixel where the entry area begins (after the label)
|
|
211
|
+
2. Identify where the entry area ends (before next field or edge)
|
|
212
|
+
3. Identify the top and bottom of the entry line/box
|
|
213
|
+
|
|
214
|
+
**Convert crop coordinates back to full image coordinates:**
|
|
215
|
+
- full_x = crop_x + crop_offset_x
|
|
216
|
+
- full_y = crop_y + crop_offset_y
|
|
217
|
+
|
|
218
|
+
Example: If the crop started at (50, 120) and the entry box starts at (52, 18) within the crop:
|
|
219
|
+
- entry_x0 = 52 + 50 = 102
|
|
220
|
+
- entry_top = 18 + 120 = 138
|
|
221
|
+
|
|
222
|
+
**Repeat for each field**, grouping nearby fields into single crops when possible.
|
|
223
|
+
|
|
224
|
+
### B.4: Create fields.json with Refined Coordinates
|
|
225
|
+
|
|
226
|
+
Create fields.json using `image_width` and `image_height` (signals image coordinates):
|
|
227
|
+
```json
|
|
228
|
+
{
|
|
229
|
+
"pages": [
|
|
230
|
+
{"page_number": 1, "image_width": 1700, "image_height": 2200}
|
|
231
|
+
],
|
|
232
|
+
"form_fields": [
|
|
233
|
+
{
|
|
234
|
+
"page_number": 1,
|
|
235
|
+
"description": "Last name entry field",
|
|
236
|
+
"field_label": "Last Name",
|
|
237
|
+
"label_bounding_box": [120, 175, 242, 198],
|
|
238
|
+
"entry_bounding_box": [255, 175, 720, 218],
|
|
239
|
+
"entry_text": {"text": "Smith", "font_size": 10}
|
|
240
|
+
}
|
|
241
|
+
]
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**Important**: Use `image_width`/`image_height` and the refined pixel coordinates from the zoom analysis.
|
|
246
|
+
|
|
247
|
+
### B.5: Validate Bounding Boxes
|
|
248
|
+
|
|
249
|
+
Before filling, check your bounding boxes for errors:
|
|
250
|
+
`python scripts/check_bounding_boxes.py fields.json`
|
|
251
|
+
|
|
252
|
+
This checks for intersecting bounding boxes and entry boxes that are too small for the font size. Fix any reported errors before filling.
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Hybrid Approach: Structure + Visual
|
|
257
|
+
|
|
258
|
+
Use this when structure extraction works for most fields but misses some elements (e.g., circular checkboxes, unusual form controls).
|
|
259
|
+
|
|
260
|
+
1. **Use Approach A** for fields that were detected in form_structure.json
|
|
261
|
+
2. **Convert PDF to images** for visual analysis of missing fields
|
|
262
|
+
3. **Use zoom refinement** (from Approach B) for the missing fields
|
|
263
|
+
4. **Combine coordinates**: For fields from structure extraction, use `pdf_width`/`pdf_height`. For visually-estimated fields, you must convert image coordinates to PDF coordinates:
|
|
264
|
+
- pdf_x = image_x * (pdf_width / image_width)
|
|
265
|
+
- pdf_y = image_y * (pdf_height / image_height)
|
|
266
|
+
5. **Use a single coordinate system** in fields.json - convert all to PDF coordinates with `pdf_width`/`pdf_height`
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Step 2: Validate Before Filling
|
|
271
|
+
|
|
272
|
+
**Always validate bounding boxes before filling:**
|
|
273
|
+
`python scripts/check_bounding_boxes.py fields.json`
|
|
274
|
+
|
|
275
|
+
This checks for:
|
|
276
|
+
- Intersecting bounding boxes (which would cause overlapping text)
|
|
277
|
+
- Entry boxes that are too small for the specified font size
|
|
278
|
+
|
|
279
|
+
Fix any reported errors in fields.json before proceeding.
|
|
280
|
+
|
|
281
|
+
## Step 3: Fill the Form
|
|
282
|
+
|
|
283
|
+
The fill script auto-detects the coordinate system and handles conversion:
|
|
284
|
+
`python scripts/fill_pdf_form_with_annotations.py <input.pdf> fields.json <output.pdf>`
|
|
285
|
+
|
|
286
|
+
## Step 4: Verify Output
|
|
287
|
+
|
|
288
|
+
Convert the filled PDF to images and verify text placement:
|
|
289
|
+
`python scripts/convert_pdf_to_images.py <output.pdf> <verify_images/>`
|
|
290
|
+
|
|
291
|
+
If text is mispositioned:
|
|
292
|
+
- **Approach A**: Check that you're using PDF coordinates from form_structure.json with `pdf_width`/`pdf_height`
|
|
293
|
+
- **Approach B**: Check that image dimensions match and coordinates are accurate pixels
|
|
294
|
+
- **Hybrid**: Ensure coordinate conversions are correct for visually-estimated fields
|