Skip to content

Commit 922d576

Browse files
committed
Rebuild missing AcroForm fields
1 parent dd7e373 commit 922d576

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/core/editor/pdf_editor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ class PDFEditor {
12091209
annotationDict.get("FT"),
12101210
"Sig"
12111211
);
1212-
const parentRef = annotationDict.get("Parent") || null;
1212+
const parentRef = annotationDict.getRaw("Parent") || null;
12131213
// We remove the parent to avoid visiting it when cloning the
12141214
// annotation.
12151215
// It'll be fixed later in #mergeAcroForms when merging the
@@ -2109,14 +2109,14 @@ class PDFEditor {
21092109
* If the document has some fields but no Fields entry in the AcroForm, we
21102110
* need to fix that by creating a Fields entry with the oldest parent field
21112111
* for each field.
2112-
* @param {Map<Ref, Ref>} fieldToParent
2112+
* @param {RefSetCache} fieldToParent
21132113
* @param {XRef} xref
21142114
* @returns {Array<Ref>}
21152115
*/
21162116
#fixFields(fieldToParent, xref) {
21172117
const newFields = [];
21182118
const processed = new RefSet();
2119-
for (const [fieldRef, parentRef] of fieldToParent) {
2119+
for (const [fieldRef, parentRef] of fieldToParent.items()) {
21202120
if (!parentRef) {
21212121
newFields.push(fieldRef);
21222122
continue;

test/unit/api_spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7501,6 +7501,32 @@ small scripts as well as for`);
75017501
return fontIndex < 0 ? null : operatorList.argsArray[fontIndex][0];
75027502
};
75037503

7504+
it("rebuilds a missing AcroForm Fields array", async function () {
7505+
const data = assemblePdf([
7506+
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R /AcroForm 6 0 R >>\nendobj\n",
7507+
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n",
7508+
"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] " +
7509+
"/Annots [4 0 R] >>\nendobj\n",
7510+
"4 0 obj\n<< /Type /Annot /Subtype /Widget /Rect [0 0 20 10] " +
7511+
"/Parent 5 0 R >>\nendobj\n",
7512+
"5 0 obj\n<< /FT /Tx /T (group) /Kids [4 0 R] >>\nendobj\n",
7513+
"6 0 obj\n<< /DA (/Helv 10 Tf) >>\nendobj\n",
7514+
]);
7515+
7516+
let loadingTask = getDocument({ data });
7517+
let pdfDoc = await loadingTask.promise;
7518+
const extracted = await pdfDoc.extractPages([{ document: null }]);
7519+
expect(extracted).not.toBeNull();
7520+
await loadingTask.destroy();
7521+
7522+
loadingTask = getDocument({ data: extracted });
7523+
pdfDoc = await loadingTask.promise;
7524+
expect(Object.keys(await pdfDoc.getFieldObjects())).toEqual(["group"]);
7525+
const annotations = await (await pdfDoc.getPage(1)).getAnnotations();
7526+
expect(annotations[0].fieldName).toEqual("group");
7527+
await loadingTask.destroy();
7528+
});
7529+
75047530
it("extract page 2 and check AcroForm Fields T entries", async function () {
75057531
let loadingTask = getDocument(
75067532
buildGetDocumentParams("form_two_pages.pdf")

0 commit comments

Comments
 (0)