Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find and remove child widgets from the document context #1002

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions apps/deno/tests/test1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,29 @@ export default async (assets: Assets) => {
font: ubuntuFont,
});

page5.drawText('There should be no remnant of a field\nbelow this text!!', {
y: size - fMax * 5 - fPadding * 0 - fHeight * 3,
x: fPadding,
size: 18,
font: indieFlowerFont,
lineHeight: 18,
});
const textField = form.createTextField('a.new.text.field');
textField.setText('This Should Not Be Visible');
textField.addToPage(page5, {
x: fPadding,
y: size - fMax * 5 - fPadding * 3.5 - fHeight * 3,
width: fWidth * 2.5,
height: fHeight * 1,
borderWidth: 4,
backgroundColor: pastels.pinkish,
borderColor: pastels.blue,
textColor: pastels.darkBlue,
font: ubuntuFont,
});

form.removeField(textField);

/********************** Print Metadata **********************/

console.log('Title:', pdfDoc.getTitle());
Expand Down
23 changes: 23 additions & 0 deletions apps/node/tests/test1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,29 @@ export default async (assets: Assets) => {
font: ubuntuFont,
});

page5.drawText('There should be no remnant of a field\nbelow this text!!', {
y: size - fMax * 5 - fPadding * 0 - fHeight * 3,
x: fPadding,
size: 18,
font: indieFlowerFont,
lineHeight: 18,
});
const textField = form.createTextField('a.new.text.field');
textField.setText('This Should Not Be Visible');
textField.addToPage(page5, {
x: fPadding,
y: size - fMax * 5 - fPadding * 3.5 - fHeight * 3,
width: fWidth * 2.5,
height: fHeight * 1,
borderWidth: 4,
backgroundColor: pastels.pinkish,
borderColor: pastels.blue,
textColor: pastels.darkBlue,
font: ubuntuFont,
});

form.removeField(textField);

/********************** Print Metadata **********************/

console.log('Title:', pdfDoc.getTitle());
Expand Down
23 changes: 23 additions & 0 deletions apps/rn/src/tests/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,29 @@ export default async () => {
font: ubuntuFont,
});

page5.drawText('There should be no remnant of a field\nbelow this text!!', {
y: size - fMax * 5 - fPadding * 0 - fHeight * 3,
x: fPadding,
size: 18,
font: indieFlowerFont,
lineHeight: 18,
});
const textField = form.createTextField('a.new.text.field');
textField.setText('This Should Not Be Visible');
textField.addToPage(page5, {
x: fPadding,
y: size - fMax * 5 - fPadding * 3.5 - fHeight * 3,
width: fWidth * 2.5,
height: fHeight * 1,
borderWidth: 4,
backgroundColor: pastels.pinkish,
borderColor: pastels.blue,
textColor: pastels.darkBlue,
font: ubuntuFont,
});

form.removeField(textField);

/********************** Print Metadata **********************/

console.log('Title:', pdfDoc.getTitle());
Expand Down
26 changes: 26 additions & 0 deletions apps/web/test1.html
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,32 @@
font: ubuntuFont,
});

page5.drawText(
'There should be no remnant of a field\nbelow this text!!',
{
y: size - fMax * 5 - fPadding * 0 - fHeight * 3,
x: fPadding,
size: 18,
font: indieFlowerFont,
lineHeight: 18,
},
);
const textField = form.createTextField('a.new.text.field');
textField.setText('This Should Not Be Visible');
textField.addToPage(page5, {
x: fPadding,
y: size - fMax * 5 - fPadding * 3.5 - fHeight * 3,
width: fWidth * 2.5,
height: fHeight * 1,
borderWidth: 4,
backgroundColor: pastels.pinkish,
borderColor: pastels.blue,
textColor: pastels.darkBlue,
font: ubuntuFont,
});

form.removeField(textField);

/********************** Print Metadata **********************/

console.log('Title:', pdfDoc.getTitle());
Expand Down
8 changes: 8 additions & 0 deletions src/api/form/PDFForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@ export default class PDFForm {

pages.forEach((page) => page.node.removeAnnot(field.ref));
this.acroForm.removeField(field.acroField);
const fieldKids = field.acroField.normalizedEntries().Kids;
const kidsCount = fieldKids.size();
for (let childIndex = 0; childIndex < kidsCount; childIndex++) {
const child = fieldKids.get(childIndex);
if (child instanceof PDFRef) {
this.doc.context.delete(child);
}
}
this.doc.context.delete(field.ref);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/api/form/PDFForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,43 @@ describe(`PDFForm`, () => {
rgWidgetRefs.forEach((ref) => expect(refs2).not.toContain(ref));
});

it(`it cleans references of removed fields and their widgets when created with pdf-lib`, async () => {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();
const form = pdfDoc.getForm();

const cb = form.createCheckBox('a.new.check.box');
const tf = form.createTextField('a.new.text.field');

cb.addToPage(page);
tf.addToPage(page);

const refs1 = getRefs(pdfDoc);

const cbWidgetRefs = cb.acroField.normalizedEntries().Kids.asArray();
const tfWidgetRefs = cb.acroField.normalizedEntries().Kids.asArray();

expect(cbWidgetRefs.length).toBeGreaterThan(0);
expect(tfWidgetRefs.length).toBeGreaterThan(0);

// Assert that refs are present before their fields have been removed
expect(refs1.includes(cb.ref)).toBe(true);
expect(refs1.includes(tf.ref)).toBe(true);
cbWidgetRefs.forEach((ref) => expect(refs1).toContain(ref));
tfWidgetRefs.forEach((ref) => expect(refs1).toContain(ref));

form.removeField(cb);
form.removeField(tf);

const refs2 = getRefs(pdfDoc);

// Assert that refs are not present after their fields have been removed
expect(refs2.includes(cb.ref)).toBe(false);
expect(refs2.includes(tf.ref)).toBe(false);
cbWidgetRefs.forEach((ref) => expect(refs2).not.toContain(ref));
tfWidgetRefs.forEach((ref) => expect(refs2).not.toContain(ref));
});

// TODO: Add method to remove APs and use `NeedsAppearances`? How would this
// work with RadioGroups? Just set the APs to `null`but keep the keys?
});