Skip to content

Commit

Permalink
Ensure RadioGroups and CheckBoxes have APs (#743)
Browse files Browse the repository at this point in the history
* Ensure appearance streams are generated when missing

* Reset scratchpad
  • Loading branch information
Hopding authored Jan 9, 2021
1 parent 1eae75d commit bace366
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/api/form/PDFCheckBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ export default class PDFCheckBox extends PDFField {
const widgets = this.acroField.getWidgets();
for (let idx = 0, len = widgets.length; idx < len; idx++) {
const widget = widgets[idx];
const value = this.acroField.getValue();
const state = widget.getAppearanceState();
const normal = widget.getAppearances()?.normal;
return !(normal instanceof PDFDict && normal.has(value));

if (!(normal instanceof PDFDict)) return true;
if (state && !normal.has(state)) return true;
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion src/api/form/PDFForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ export default class PDFForm {
}

if (!(refOrDict instanceof PDFRef)) {
throw new Error(`Failed to extract appearance ref`);
const name = field.getName();
throw new Error(`Failed to extract appearance ref for: ${name}`);
}

const xObjectKey = addRandomSuffix('FlatWidget', 10);
Expand Down
9 changes: 4 additions & 5 deletions src/api/form/PDFRadioGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,14 @@ export default class PDFRadioGroup extends PDFField {
const widgets = this.acroField.getWidgets();
for (let idx = 0, len = widgets.length; idx < len; idx++) {
const widget = widgets[idx];
const value = this.acroField.getValue();
const state = widget.getAppearanceState();
const normal = widget.getAppearances()?.normal;

if (normal instanceof PDFDict && normal.has(value)) {
return false;
}
if (!(normal instanceof PDFDict)) return true;
if (state && !normal.has(state)) return true;
}

return true;
return false;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/core/annotation/PDFAnnotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class PDFAnnotation {
this.dict.set(PDFName.of('Rect'), Rect);
}

getAppearanceState(): PDFName | undefined {
const AS = this.dict.lookup(PDFName.of('AS'));
if (AS instanceof PDFName) return AS;
return undefined;
}

setAppearanceState(state: PDFName) {
this.dict.set(PDFName.of('AS'), state);
}
Expand Down

0 comments on commit bace366

Please sign in to comment.