Fixes to get field heirarchy working

This commit is contained in:
Jim Pravetz 2019-07-19 09:04:40 -07:00
parent e135aad920
commit 1553ca8498
5 changed files with 23 additions and 22 deletions

4
.gitignore vendored
View File

@ -13,3 +13,7 @@ demo/bundle.js
coverage
tests/integration/__pdfs__
tests/integration/pdfmake/__pdfs__
tests/integration/**/__snapshots__/*
tests/integration/__snapshots__/*
*.snap
/*.pdf

View File

View File

@ -17,10 +17,10 @@ doc.font('Helvetica') // establishes the default font
doc.initAcroForm();
let rootField = doc.field('rootField');
let child1Field = doc.field('child1Field', { parent: rootField });
let child2Field = doc.field('child2Field', { parent: rootField });
doc.formText('leaf1', 10, 10, 200, 40, { parent: child1Field })
doc.formText('leaf2', 10, 60, 200, 40, { parent: child1Field })
doc.formText('leaf3', 10, 110, 200, 40, { parent: child2Field })
let child1Field = doc.field('child1Field', { Parent: rootField });
let child2Field = doc.field('child2Field', { Parent: rootField });
doc.formText('leaf1', 10, 10, 200, 40, { Parent: child1Field })
doc.formText('leaf2', 10, 60, 200, 40, { Parent: child1Field })
doc.formText('leaf3', 10, 110, 200, 40, { Parent: child2Field })
doc.end();

View File

@ -74,32 +74,29 @@ export default {
field (name, options = {}) {
let fieldDict = this._fieldDict(name, options);
let fieldRef = this.ref(fieldDict);
this._addToParent(fieldRef, options.parent);
delete options.parent;
this._addToParent(fieldRef);
return fieldRef;
},
widgetAnnot (name, x, y, w, h, options = {}) {
let fieldDict = this._fieldDict(name, null, options);
let fieldDict = this._fieldDict(name, options);
fieldDict.Subtype = 'Widget';
fieldDict.F = 4;
let parent = options.parent;
delete options.parent;
// Add Field annot to page, and get it's ref
this.annotate(x, y, w, h, fieldDict);
let annotRef = this.page.annotations[this.page.annotations.length - 1];
return this._addToParent(annotRef, parent);
return this._addToParent(annotRef);
},
_addToParent (fieldRef, parentRef) {
if (parentRef) {
if (!parentRef.data.Kids) {
parentRef.data.Kids = [];
_addToParent (fieldRef) {
let parent = fieldRef.data.Parent;
if (parent) {
if (!parent.data.Kids) {
parent.data.Kids = [];
}
parentRef.data.Kids.push(fieldRef);
fieldRef.data.Parent = parentRef;
parent.data.Kids.push(fieldRef);
} else {
this._root.data.AcroForm.data.Fields.push(fieldRef);
}

View File

@ -66,11 +66,11 @@ describe('AcroForm', () => {
doc.initAcroForm()
let rootField = doc.field('rootField');
let child1Field = doc.field('child1Field', { parent: rootField });
let child2Field = doc.field('child2Field', { parent: rootField });
doc.formText('leaf1', 10, 10, 200, 40, { parent: child1Field })
doc.formText('leaf2', 10, 60, 200, 40, { parent: child1Field })
doc.formText('leaf3', 10, 110, 200, 40, { parent: child2Field })
let child1Field = doc.field('child1Field', { Parent: rootField });
let child2Field = doc.field('child2Field', { Parent: rootField });
doc.formText('leaf1', 10, 10, 200, 40, { Parent: child1Field })
doc.formText('leaf2', 10, 60, 200, 40, { Parent: child1Field })
doc.formText('leaf3', 10, 110, 200, 40, { Parent: child2Field })
doc.end();