* Remove all widgets of a field from their respective pages.
(field: FormField)
| 1041 | * Remove all widgets of a field from their respective pages. |
| 1042 | */ |
| 1043 | private removeWidgetsFromPages(field: FormField): void { |
| 1044 | const widgets = field.getWidgets(); |
| 1045 | const pageRefs = this._ctx.pages.getPages(); |
| 1046 | |
| 1047 | // Build a set of widget refs for fast lookup |
| 1048 | const widgetRefKeys = new Set<string>(); |
| 1049 | |
| 1050 | for (const widget of widgets) { |
| 1051 | if (widget.ref) { |
| 1052 | widgetRefKeys.add(`${widget.ref.objectNumber} ${widget.ref.generation}`); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | if (widgetRefKeys.size === 0) { |
| 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | // Scan pages and remove widget annotations |
| 1061 | for (const pageRef of pageRefs) { |
| 1062 | const pageDict = this._ctx.registry.resolve(pageRef); |
| 1063 | |
| 1064 | if (!(pageDict instanceof PdfDict)) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | const annots = pageDict.getArray( |
| 1069 | "Annots", |
| 1070 | this._ctx.registry.resolve.bind(this._ctx.registry), |
| 1071 | ); |
| 1072 | |
| 1073 | if (!annots) { |
| 1074 | continue; |
| 1075 | } |
| 1076 | |
| 1077 | // Remove matching widget refs from this page's annotations |
| 1078 | // Iterate backwards to safely remove items |
| 1079 | for (let i = annots.length - 1; i >= 0; i--) { |
| 1080 | const item = annots.at(i); |
| 1081 | |
| 1082 | if (item instanceof PdfRef) { |
| 1083 | const key = `${item.objectNumber} ${item.generation}`; |
| 1084 | |
| 1085 | if (widgetRefKeys.has(key)) { |
| 1086 | annots.remove(i); |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | // If annots is now empty, remove the /Annots entry |
| 1092 | if (annots.length === 0) { |
| 1093 | pageDict.delete("Annots"); |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // ───────────────────────────────────────────────────────────────────────────── |
| 1099 | // Field Creation Helpers |
no test coverage detected