()
| 11 | import { loadFixture } from "../utils"; |
| 12 | |
| 13 | async function main() { |
| 14 | console.log("Reading form fields from a PDF...\n"); |
| 15 | |
| 16 | // Load a PDF with form fields |
| 17 | const bytes = await loadFixture("forms", "fancy_fields.pdf"); |
| 18 | const pdf = await PDF.load(bytes); |
| 19 | |
| 20 | // Check if the document has a form |
| 21 | if (!pdf.hasForm()) { |
| 22 | console.log("This PDF does not contain a form."); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // Get the form |
| 27 | const form = pdf.getForm(); |
| 28 | if (!form) { |
| 29 | console.log("Could not load form."); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | console.log("=== Form Overview ==="); |
| 34 | console.log(`Total fields: ${form.fieldCount}`); |
| 35 | console.log(`Is empty: ${form.isEmpty}`); |
| 36 | console.log(""); |
| 37 | |
| 38 | // Get all field names |
| 39 | const fieldNames = form.getFieldNames(); |
| 40 | console.log("=== Field Names ==="); |
| 41 | for (const name of fieldNames) { |
| 42 | console.log(` - ${name}`); |
| 43 | } |
| 44 | console.log(""); |
| 45 | |
| 46 | // Iterate through all fields and show their details |
| 47 | console.log("=== Field Details ===\n"); |
| 48 | const fields = form.getFields(); |
| 49 | |
| 50 | for (const field of fields) { |
| 51 | console.log(`Field: "${field.name}"`); |
| 52 | console.log(` Type: ${field.type}`); |
| 53 | console.log(` Read-only: ${field.isReadOnly()}`); |
| 54 | console.log(` Required: ${field.isRequired()}`); |
| 55 | |
| 56 | // Show type-specific information |
| 57 | switch (field.type) { |
| 58 | case "text": { |
| 59 | const textField = form.getTextField(field.name); |
| 60 | if (textField) { |
| 61 | const value = textField.getValue(); |
| 62 | console.log(` Value: "${value ?? "(empty)"}"`); |
| 63 | console.log(` Max length: ${textField.maxLength ?? "unlimited"}`); |
| 64 | console.log(` Multiline: ${textField.isMultiline}`); |
| 65 | } |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | case "checkbox": { |
| 70 | const checkbox = form.getCheckbox(field.name); |
no test coverage detected