change: degridifies listFormField

This commit is contained in:
2025-05-21 19:05:54 -06:00
parent 123bf51001
commit 569c67583d
2 changed files with 4 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@bearmetal/pdf-tools", "name": "@bearmetal/pdf-tools",
"version": "1.0.7", "version": "1.0.8-a",
"license": "GPL 3.0", "license": "GPL 3.0",
"tasks": { "tasks": {
"dev": "deno run -A --env-file=.env main.ts", "dev": "deno run -A --env-file=.env main.ts",

View File

@@ -20,31 +20,10 @@ export class ListFormFields implements ITool {
const form = await loadPdfForm(pdfPath); const form = await loadPdfForm(pdfPath);
const fields = form.getFields(); const fields = form.getFields();
const height = this.block.getRenderHeight() - 1; const fieldNames = fields.map((f) => f.getName());
const fieldNames = fields.sort((a, b) => {
const aRect = a.acroField.getWidgets().find((e) => e.Rect())?.Rect()
?.asRectangle();
const bRect = b.acroField.getWidgets().find((e) => e.Rect())?.Rect()
?.asRectangle();
if (aRect && bRect) {
if (aRect.x !== bRect.x) {
return aRect.x - bRect.x; // Sort left to right
} else {
return bRect.y - aRect.y; // If x is equal, sort top to bottom
}
}
return a.getName().localeCompare(b.getName());
}).map((f) => f.getName());
const maxLength = Math.max(...fieldNames.map((f) => f.length)) + 4;
const lines = []; const lines = [];
for (let i = 0; i < height; i++) { for (const fieldName of fieldNames) {
let line = ""; lines.push(fieldName);
for (let j = 0; j < fieldNames.length; j += height) {
const fieldName = fieldNames[i + j] ?? "";
line += fieldName.padEnd(maxLength, " ");
}
lines.push(line);
} }
this.block.setLines(lines, [0, 1]); this.block.setLines(lines, [0, 1]);
await cliAlert("", this.block); await cliAlert("", this.block);