* Get or create the document's interactive form. * * If the document has no AcroForm, one is created and added to the catalog * with proper default resources (Helvetica and ZapfDingbats fonts) and * default appearance settings. * * @returns The form (never null) * * @example
()
| 2675 | * ``` |
| 2676 | */ |
| 2677 | getOrCreateForm(): PDFForm { |
| 2678 | const existing = this.getForm(); |
| 2679 | |
| 2680 | if (existing) { |
| 2681 | return existing; |
| 2682 | } |
| 2683 | |
| 2684 | // Create standard Helvetica font dictionary |
| 2685 | const helveticaDict = PdfDict.of({ |
| 2686 | Type: PdfName.of("Font"), |
| 2687 | Subtype: PdfName.of("Type1"), |
| 2688 | BaseFont: PdfName.of("Helvetica"), |
| 2689 | }); |
| 2690 | |
| 2691 | // Create ZapfDingbats font dictionary (for checkboxes/radios) |
| 2692 | const zapfDingbatsDict = PdfDict.of({ |
| 2693 | Type: PdfName.of("Font"), |
| 2694 | Subtype: PdfName.of("Type1"), |
| 2695 | BaseFont: PdfName.of("ZapfDingbats"), |
| 2696 | }); |
| 2697 | |
| 2698 | // Create Font dictionary with standard fonts |
| 2699 | const fontsDict = PdfDict.of({ |
| 2700 | Helv: helveticaDict, |
| 2701 | ZaDb: zapfDingbatsDict, |
| 2702 | }); |
| 2703 | |
| 2704 | // Create Default Resources dictionary |
| 2705 | const drDict = PdfDict.of({ |
| 2706 | Font: fontsDict, |
| 2707 | }); |
| 2708 | |
| 2709 | // Create AcroForm dictionary with proper defaults |
| 2710 | const acroFormDict = PdfDict.of({ |
| 2711 | Fields: new PdfArray([]), |
| 2712 | DR: drDict, |
| 2713 | DA: PdfString.fromString("/Helv 0 Tf 0 g"), |
| 2714 | NeedAppearances: PdfBool.of(false), |
| 2715 | }); |
| 2716 | |
| 2717 | const acroFormRef = this.ctx.registry.register(acroFormDict); |
| 2718 | |
| 2719 | // Add to catalog |
| 2720 | const catalog = this.getCatalog(); |
| 2721 | catalog.set("AcroForm", acroFormRef); |
| 2722 | |
| 2723 | // Reload form cache |
| 2724 | this._form = PDFForm.load(this.ctx); |
| 2725 | |
| 2726 | if (!this._form) { |
| 2727 | throw new Error("Failed to create form"); |
| 2728 | } |
| 2729 | |
| 2730 | return this._form; |
| 2731 | } |
| 2732 | |
| 2733 | // ───────────────────────────────────────────────────────────────────────────── |
| 2734 | // Digital Signatures |
no test coverage detected