Skip to content

Fixed radio/checkbox return values in getAllFieldsTypes() #383

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 4 additions & 3 deletions lib/pdfanno.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import nodeUtil from "util";

//BEGIN - MQZ 9/19/2012. Helper functions to parse acroForm elements
function setupRadioButton(annotation, item) {
let asName = '';
//let asName = '';
//PDF Spec p.689: parent item's DV holds the item's value that is selected by default
const po = annotation.get('Parent');
if (po) {
po.forEach((key, val) => {
if (key === 'DV') {
asName = val.name || '';
//asName = val.name || '';
}
else if (key === 'TU') {
//radio buttons use the alternative text from the parent
Expand All @@ -28,7 +28,8 @@ function setupRadioButton(annotation, item) {
if (key.toLowerCase() !== "off") {
//value if selected
item.value = key; //export value
item.checked = (key === asName); //initial selection state

item.checked = (item.fieldValue === item.value); //initial selection state
}
});

Expand Down
14 changes: 13 additions & 1 deletion lib/pdffield.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,30 @@ export default class PDFField {
//static public method to generate fieldsType object based on parser result
static getAllFieldsTypes(data) {
const isFieldReadOnly = field => (field.AM & kFBANotOverridable) ? true : false;

function getFieldValue(field) {
switch(field.T.Name) {
case 'box':
return field.checked === undefined ? false : field.checked;
case 'alpha':
case 'date':
default:
return field.V;
}
}
const getFieldBase = field => ({
id: field.id.Id,
type: field.T.Name,
calc: isFieldReadOnly(field),
value: field.V || ""
value: getFieldValue(field)
});

const retVal = [];
data.Pages.forEach( page => {
page.Boxsets.forEach( boxsets => {
if (boxsets.boxes.length > 1) { //radio button
boxsets.boxes.forEach( box => {
if(box.checked)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchecked box field is needed to render the form too.

retVal.push({ id: boxsets.id.Id, type: "radio", calc: isFieldReadOnly(box), value: box.id.Id });
});
}
Expand Down
8 changes: 8 additions & 0 deletions pdfparser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export declare class PDFParser extends EventEmitter{
createParserStream(): ParserStream
getRawTextContent(): string
on<K extends keyof EventMap>(eventName: K, listener: EventMap[K]): this
getAllFieldsTypes(): FieldType[]
}

export type EventMap = {
Expand Down Expand Up @@ -118,6 +119,13 @@ export declare interface Field {
}
}

export declare interface FieldType {
id: string,
type: 'alpha' | 'box' | 'radio' | 'date' | 'link' | 'signature',
calc: boolean,
value: string | boolean
}

export declare interface Box {
x: number,
y: number,
Expand Down