Skip to content

Commit

Permalink
Merge pull request #1003 from Trapfether/setPageReferenceForNewFields
Browse files Browse the repository at this point in the history
Set page reference for new field Widgets
  • Loading branch information
Hopding authored Oct 7, 2021
2 parents 5cb111a + 7bb4856 commit 0dfe3fa
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/api/form/PDFButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export default class PDFButton extends PDFField {
rotate: options?.rotate ?? degrees(0),
caption: text,
hidden: options?.hidden,
page: page.ref,
});
const widgetRef = this.doc.context.register(widget.dict);

Expand Down
1 change: 1 addition & 0 deletions src/api/form/PDFCheckBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export default class PDFCheckBox extends PDFField {
borderWidth: options.borderWidth ?? 0,
rotate: options.rotate ?? degrees(0),
hidden: options.hidden,
page: page.ref,
});
const widgetRef = this.doc.context.register(widget.dict);

Expand Down
1 change: 1 addition & 0 deletions src/api/form/PDFDropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export default class PDFDropdown extends PDFField {
borderWidth: options.borderWidth ?? 0,
rotate: options.rotate ?? degrees(0),
hidden: options.hidden,
page: page.ref,
});
const widgetRef = this.doc.context.register(widget.dict);

Expand Down
6 changes: 6 additions & 0 deletions src/api/form/PDFField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export default class PDFField {
rotate: Rotation;
caption?: string;
hidden?: boolean;
page?: PDFRef;
}): PDFWidgetAnnotation {
const textColor = options.textColor;
const backgroundColor = options.backgroundColor;
Expand All @@ -305,6 +306,7 @@ export default class PDFField {
const width = options.width + borderWidth;
const height = options.height + borderWidth;
const hidden = Boolean(options.hidden);
const pageRef = options.page;

assertMultiple(degreesAngle, 'degreesAngle', 90);

Expand All @@ -319,6 +321,10 @@ export default class PDFField {
);
widget.setRectangle(rect);

if(typeof pageRef !== 'undefined'){
widget.setP(pageRef);
}

const ac = widget.getOrCreateAppearanceCharacteristics();
if (backgroundColor) {
ac.setBackgroundColor(colorToComponents(backgroundColor));
Expand Down
1 change: 1 addition & 0 deletions src/api/form/PDFOptionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ export default class PDFOptionList extends PDFField {
borderWidth: options.borderWidth ?? 0,
rotate: options.rotate ?? degrees(0),
hidden: options.hidden,
page: page.ref,
});
const widgetRef = this.doc.context.register(widget.dict);

Expand Down
1 change: 1 addition & 0 deletions src/api/form/PDFRadioGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ export default class PDFRadioGroup extends PDFField {
borderWidth: options?.borderWidth ?? 1,
rotate: options?.rotate ?? degrees(0),
hidden: options?.hidden,
page: page.ref,
});
const widgetRef = this.doc.context.register(widget.dict);

Expand Down
1 change: 1 addition & 0 deletions src/api/form/PDFTextField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ export default class PDFTextField extends PDFField {
borderWidth: options.borderWidth ?? 0,
rotate: options.rotate ?? degrees(0),
hidden: options.hidden,
page: page.ref,
});
const widgetRef = this.doc.context.register(widget.dict);

Expand Down
4 changes: 4 additions & 0 deletions src/core/annotation/PDFWidgetAnnotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class PDFWidgetAnnotation extends PDFAnnotation {
return undefined;
}

setP(page: PDFRef){
this.dict.set(PDFName.of('P'),page);
}

setDefaultAppearance(appearance: string) {
this.dict.set(PDFName.of('DA'), PDFString.of(appearance));
}
Expand Down
16 changes: 16 additions & 0 deletions tests/api/form/PDFCheckBox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ describe(`PDFCheckBox`, () => {
expect(widgets().length).toBe(1);
expect(widgets()[0].hasFlag(AnnotationFlags.Print)).toBe(true);
});

it(`sets page reference when added to a page`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();

const form = pdfDoc.getForm();

const checkBox = form.createCheckBox('a.new.check.box');

const widgets = () => checkBox.acroField.getWidgets();
expect(widgets().length).toBe(0);

checkBox.addToPage(page);
expect(widgets().length).toBe(1);
expect(widgets()[0].P()).toBe(page.ref);
});
});
16 changes: 16 additions & 0 deletions tests/api/form/PDFDropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,20 @@ describe(`PDFDropdown`, () => {
expect(widgets().length).toBe(1);
expect(widgets()[0].hasFlag(AnnotationFlags.Print)).toBe(true);
});

it(`sets page reference when added to a page`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();

const form = pdfDoc.getForm();

const dropdown = form.createDropdown('a.new.dropdown');

const widgets = () => dropdown.acroField.getWidgets();
expect(widgets().length).toBe(0);

dropdown.addToPage(page);
expect(widgets().length).toBe(1);
expect(widgets()[0].P()).toBe(page.ref);
});
});
16 changes: 16 additions & 0 deletions tests/api/form/PDFOptionList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,20 @@ describe(`PDFOptionList`, () => {
expect(widgets().length).toBe(1);
expect(widgets()[0].hasFlag(AnnotationFlags.Print)).toBe(true);
});

it(`sets page reference when added to a page`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();

const form = pdfDoc.getForm();

const optionList = form.createOptionList('a.new.option.list');

const widgets = () => optionList.acroField.getWidgets();
expect(widgets().length).toBe(0);

optionList.addToPage(page);
expect(widgets().length).toBe(1);
expect(widgets()[0].P()).toBe(page.ref);
});
});
16 changes: 16 additions & 0 deletions tests/api/form/PDFRadioGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,20 @@ describe(`PDFRadioGroup`, () => {
expect(widgets().length).toBe(1);
expect(widgets()[0].hasFlag(AnnotationFlags.Print)).toBe(true);
});

it(`sets page reference when added to a page`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();

const form = pdfDoc.getForm();

const radioGroup = form.createRadioGroup('a.new.radio.group');

const widgets = () => radioGroup.acroField.getWidgets();
expect(widgets().length).toBe(0);

radioGroup.addOptionToPage('foo', page);
expect(widgets().length).toBe(1);
expect(widgets()[0].P()).toBe(page.ref);
});
});
16 changes: 16 additions & 0 deletions tests/api/form/PDFTextField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ describe(`PDFTextField`, () => {
expect(widgets()[0].hasFlag(AnnotationFlags.Print)).toBe(true);
});

it(`sets page reference when added to a page`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();

const form = pdfDoc.getForm();

const textField = form.createTextField('a.new.text.field');

const widgets = () => textField.acroField.getWidgets();
expect(widgets().length).toBe(0);

textField.addToPage(page);
expect(widgets().length).toBe(1);
expect(widgets()[0].P()).toBe(page.ref);
});

it(`sets the 'hidden' flag when passed options.hidden`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();
Expand Down

0 comments on commit 0dfe3fa

Please sign in to comment.