Skip to content

happypdf / PDFRadioGroup

Class: PDFRadioGroup

Defined in: src/api/form/PDFRadioGroup.ts:43

Represents a radio group field of a [[PDFForm]].

[[PDFRadioGroup]] fields are collections of radio buttons. The purpose of a radio group is to enable users to select one option from a set of mutually exclusive choices. Each choice in a radio group is represented by a radio button. Radio buttons each have two states: on and off. At most one radio button in a group may be in the on state at any time. Users can click on a radio button to select it (and thereby automatically deselect any other radio button that might have already been selected). Some radio groups allow users to toggle a selected radio button off by clicking on it (see [[PDFRadioGroup.isOffToggleable]]).

Note that some radio groups allow multiple radio buttons to be in the on state at the same type if they represent the same underlying value (see [[PDFRadioGroup.isMutuallyExclusive]]).

Extends

Properties

acroField

ts
readonly acroField: PDFAcroRadioButton;

Defined in: src/api/form/PDFRadioGroup.ts:64

The low-level PDFAcroRadioButton wrapped by this radio group.

Overrides

PDFField.acroField


doc

ts
readonly doc: PDFDocument;

Defined in: src/api/form/PDFField.ts:96

The document to which this field belongs.

Inherited from

PDFField.doc


ref

ts
readonly ref: PDFRef;

Defined in: src/api/form/PDFField.ts:93

The unique reference assigned to this field within the document.

Inherited from

PDFField.ref

Methods

addOptionToPage()

ts
addOptionToPage(
   option, 
   page, 
   options?): void;

Defined in: src/api/form/PDFRadioGroup.ts:352

Add a new radio button to this group on the specified page. For example:

js
const page = pdfDoc.addPage()

const form = pdfDoc.getForm()
const radioGroup = form.createRadioGroup('best.gundam')

const options = {
  x: 50,
  width: 25,
  height: 25,
  textColor: rgb(1, 0, 0),
  backgroundColor: rgb(0, 1, 0),
  borderColor: rgb(0, 0, 1),
  borderWidth: 2,
  rotate: degrees(90),
}

radioGroup.addOptionToPage('Exia', page, { ...options, y: 50 })
radioGroup.addOptionToPage('Dynames', page, { ...options, y: 110 })

This will create a new radio button widget for this radio group field.

Parameters

ParameterTypeDescription
optionstringThe option that the radio button widget represents.
pagePDFPageThe page to which the radio button widget should be added.
options?FieldAppearanceOptionsThe options to be used when adding the radio button widget.

Returns

void


clear()

ts
clear(): void;

Defined in: src/api/form/PDFRadioGroup.ts:222

Clear any selected option for this dropdown. This will result in all radio buttons in this group being toggled off. This method will update the underlying state of the dropdown to indicate that no radio buttons have been selected. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.clear()

This method will mark this radio group as dirty. See [[PDFRadioGroup.select]] for more details about what this means.

Returns

void


createImageAppearanceStream()

ts
protected createImageAppearanceStream(
   widget, 
   image, 
   alignment): PDFRef;

Defined in: src/api/form/PDFField.ts:498

Create a FormXObject of the supplied image and add it to context. The FormXObject size is calculated based on the widget (including the alignment).

Parameters

ParameterTypeDescription
widgetPDFWidgetAnnotationThe widget that should display the image.
imagePDFImageThe image that should be displayed.
alignmentImageAlignmentThe alignment of the image.

Returns

PDFRef

The ref for the FormXObject that was added to the context.

Inherited from

PDFField.createImageAppearanceStream


createWidget()

ts
protected createWidget(options): PDFWidgetAnnotation;

Defined in: src/api/form/PDFField.ts:333

Parameters

ParameterType
options{ backgroundColor?: Color; borderColor?: Color; borderWidth: number; caption?: string; height: number; hidden?: boolean; page?: PDFRef; rotate: Rotation; textColor?: Color; width: number; x: number; y: number; }
options.backgroundColor?Color
options.borderColor?Color
options.borderWidthnumber
options.caption?string
options.heightnumber
options.hidden?boolean
options.page?PDFRef
options.rotateRotation
options.textColor?Color
options.widthnumber
options.xnumber
options.ynumber

Returns

PDFWidgetAnnotation

Inherited from

PDFField.createWidget


defaultUpdateAppearances()

ts
defaultUpdateAppearances(): void;

Defined in: src/api/form/PDFRadioGroup.ts:423

Update the appearance streams for each of this group's radio button widgets using the default appearance provider for radio groups. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.defaultUpdateAppearances()

Returns

void

Overrides

ts
PDFField.defaultUpdateAppearances

disableExporting()

ts
disableExporting(): void;

Defined in: src/api/form/PDFField.ts:251

Indicate that this field's value should not be exported when the form is submitted in a PDF reader. For example:

js
const field = form.getField('some.field')
field.disableExporting()

Returns

void

Inherited from

PDFField.disableExporting


disableMutualExclusion()

ts
disableMutualExclusion(): void;

Defined in: src/api/form/PDFRadioGroup.ts:321

When the user clicks a radio button in this group only it will be selected. No other radio buttons in the group will be selected, even if they share the same underlying value. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.disableMutualExclusion()

Note that this option must be disabled prior to adding options to the radio group. It does not currently apply retroactively to existing radio buttons in the group.

Returns

void


disableOffToggling()

ts
disableOffToggling(): void;

Defined in: src/api/form/PDFRadioGroup.ts:270

Prevent users from clicking on selected radio buttons in this group to toggle them off. Clicking on a selected radio button will have no effect. The only way to deselect a selected radio button is to click on a different radio button in the group. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.disableOffToggling()

Returns

void


disableReadOnly()

ts
disableReadOnly(): void;

Defined in: src/api/form/PDFField.ts:174

Allow users to interact with this field and change its value in PDF readers via mouse and keyboard input. For example:

js
const field = form.getField('some.field')
field.disableReadOnly()

Returns

void

Inherited from

PDFField.disableReadOnly


disableRequired()

ts
disableRequired(): void;

Defined in: src/api/form/PDFField.ts:212

Do not require this field to have a value when the form is submitted. For example:

js
const field = form.getField('some.field')
field.disableRequired()

Returns

void

Inherited from

PDFField.disableRequired


enableExporting()

ts
enableExporting(): void;

Defined in: src/api/form/PDFField.ts:239

Indicate that this field's value should be exported when the form is submitted in a PDF reader. For example:

js
const field = form.getField('some.field')
field.enableExporting()

Returns

void

Inherited from

PDFField.enableExporting


enableMutualExclusion()

ts
enableMutualExclusion(): void;

Defined in: src/api/form/PDFRadioGroup.ts:305

When the user clicks a radio button in this group it will be selected. In addition, any other radio buttons in this group that share the same underlying value will also be selected. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.enableMutualExclusion()

Note that this option must be enabled prior to adding options to the radio group. It does not currently apply retroactively to existing radio buttons in the group.

Returns

void


enableOffToggling()

ts
enableOffToggling(): void;

Defined in: src/api/form/PDFRadioGroup.ts:256

Allow users to click on selected radio buttons in this group to toggle them off. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.enableOffToggling()

> NOTE: This feature is documented in the PDF specification > (Table 226). However, most PDF readers do not respect this option and > prevent users from toggling radio buttons off even when it is enabled. > At the time of this writing (9/6/2020) Mac's Preview software did > respect the option. Adobe Acrobat, Foxit Reader, and Google Chrome did > not.

Returns

void


enableReadOnly()

ts
enableReadOnly(): void;

Defined in: src/api/form/PDFField.ts:162

Prevent PDF readers from allowing users to interact with this field or change its value. The field will not respond to mouse or keyboard input. For example:

js
const field = form.getField('some.field')
field.enableReadOnly()

Useful for fields whose values are computed, imported from a database, or prefilled by software before being displayed to the user.

Returns

void

Inherited from

PDFField.enableReadOnly


enableRequired()

ts
enableRequired(): void;

Defined in: src/api/form/PDFField.ts:200

Require this field to have a value when the form is submitted. For example:

js
const field = form.getField('some.field')
field.enableRequired()

Returns

void

Inherited from

PDFField.enableRequired


getAction()

ts
getAction(): PDFJavaScriptAction | undefined;

Defined in: src/api/form/PDFField.ts:290

Get the default action (A) for this field. This is typically a submit or reset action, or can be a JavaScript action. For example:

js
const field = form.getField('some.field')
const action = field.getAction()
if (action) {
  console.log('Action script:', action.getScript())
}

Returns

PDFJavaScriptAction | undefined

The JavaScript action, or undefined if not a JavaScript action.

Inherited from

PDFField.getAction


getJavaScriptActions()

ts
getJavaScriptActions(): 
  | JavaScriptActionMap
  | undefined;

Defined in: src/api/form/PDFField.ts:271

Get the JavaScript actions associated with this field. Returns a map of action types to JavaScript actions. For example:

js
const field = form.getField('some.field')
const actions = field.getJavaScriptActions()
if (actions.keystroke) {
  console.log('Keystroke script:', actions.keystroke.getScript())
}
if (actions.calculate) {
  console.log('Calculate script:', actions.calculate.getScript())
}

Returns

| JavaScriptActionMap | undefined

A map of JavaScript actions for this field, or undefined if none exist.

Inherited from

PDFField.getJavaScriptActions


getName()

ts
getName(): string;

Defined in: src/api/form/PDFField.ts:132

Get the fully qualified name of this field. For example:

js
const fields = form.getFields()
fields.forEach(field => {
  const name = field.getName()
  console.log('Field name:', name)
})

Note that PDF fields are structured as a tree. Each field is the descendent of a series of ancestor nodes all the way up to the form node, which is always the root of the tree. Each node in the tree (except for the form node) has a partial name. Partial names can be composed of any unicode characters except a period (.). The fully qualified name of a field is composed of the partial names of all its ancestors joined with periods. This means that splitting the fully qualified name on periods and taking the last element of the resulting array will give you the partial name of a specific field.

Returns

string

The fully qualified name of this field.

Inherited from

PDFField.getName


getOptions()

ts
getOptions(): string[];

Defined in: src/api/form/PDFRadioGroup.ts:93

Get the list of available options for this radio group. Each option is represented by a radio button. These radio buttons are displayed at various locations in the document, potentially on different pages (though typically they are stacked horizontally or vertically on the same page). For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
const options = radioGroup.getOptions()
console.log('Radio Group options:', options)

Returns

string[]

The options for this radio group.


getSelected()

ts
getSelected(): string | undefined;

Defined in: src/api/form/PDFRadioGroup.ts:124

Get the selected option for this radio group. The selected option is represented by the radio button in this group that is turned on. At most one radio button in a group can be selected. If no buttons in this group are selected, undefined is returned. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
const selected = radioGroup.getSelected()
console.log('Selected radio button:', selected)

Returns

string | undefined

The selected option for this radio group.


isDirty()

ts
protected isDirty(): boolean;

Defined in: src/api/form/PDFField.ts:329

Returns

boolean

Inherited from

PDFField.isDirty


isExported()

ts
isExported(): boolean;

Defined in: src/api/form/PDFField.ts:227

Returns true if this field's value should be exported when the form is submitted. See [[PDFField.enableExporting]] and [[PDFField.disableExporting]]. For example:

js
const field = form.getField('some.field')
if (field.isExported()) console.log('Exporting is enabled')

Returns

boolean

Whether or not this field's value should be exported.

Inherited from

PDFField.isExported


isMutuallyExclusive()

ts
isMutuallyExclusive(): boolean;

Defined in: src/api/form/PDFRadioGroup.ts:289

Returns true if the radio buttons in this group are mutually exclusive. This means that when the user selects a radio button, only that specific button will be turned on. Even if other radio buttons in the group represent the same value, they will not be enabled. The alternative to this is that clicking a radio button will select that button along with any other radio buttons in the group that share the same value. See [[PDFRadioGroup.enableMutualExclusion]] and [[PDFRadioGroup.disableMutualExclusion]]. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
if (radioGroup.isMutuallyExclusive()) console.log('Mutual exclusion is enabled')

Returns

boolean


isOffToggleable()

ts
isOffToggleable(): boolean;

Defined in: src/api/form/PDFRadioGroup.ts:238

Returns true if users can click on radio buttons in this group to toggle them off. The alternative is that once a user clicks on a radio button to select it, the only way to deselect it is by selecting on another radio button in the group. See [[PDFRadioGroup.enableOffToggling]] and [[PDFRadioGroup.disableOffToggling]]. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
if (radioGroup.isOffToggleable()) console.log('Off toggling is enabled')

Returns

boolean


isReadOnly()

ts
isReadOnly(): boolean;

Defined in: src/api/form/PDFField.ts:147

Returns true if this field is read only. This means that PDF readers will not allow users to interact with the field or change its value. See [[PDFField.enableReadOnly]] and [[PDFField.disableReadOnly]]. For example:

js
const field = form.getField('some.field')
if (field.isReadOnly()) console.log('Read only is enabled')

Returns

boolean

Whether or not this is a read only field.

Inherited from

PDFField.isReadOnly


isRequired()

ts
isRequired(): boolean;

Defined in: src/api/form/PDFField.ts:188

Returns true if this field must have a value when the form is submitted. See [[PDFField.enableRequired]] and [[PDFField.disableRequired]]. For example:

js
const field = form.getField('some.field')
if (field.isRequired()) console.log('Field is required')

Returns

boolean

Whether or not this field is required.

Inherited from

PDFField.isRequired


markAsClean()

ts
protected markAsClean(): void;

Defined in: src/api/form/PDFField.ts:325

Returns

void

Inherited from

PDFField.markAsClean


markAsDirty()

ts
protected markAsDirty(): void;

Defined in: src/api/form/PDFField.ts:321

Returns

void

Inherited from

PDFField.markAsDirty


needsAppearancesUpdate()

ts
needsAppearancesUpdate(): boolean;

Defined in: src/api/form/PDFRadioGroup.ts:401

Returns true if any of this group's radio button widgets do not have an appearance stream for their current state. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
if (radioGroup.needsAppearancesUpdate()) console.log('Needs update')

Returns

boolean

Whether or not this radio group needs an appearance update.

Overrides

ts
PDFField.needsAppearancesUpdate

of()

ts
static of(
   acroRadioButton, 
   ref, 
   doc): PDFRadioGroup;

Defined in: src/api/form/PDFRadioGroup.ts:57

> NOTE: You probably don't want to call this method directly. Instead, > consider using the [[PDFForm.getOptionList]] method, which will create an > instance of [[PDFOptionList]] for you.

Create an instance of [[PDFOptionList]] from an existing acroRadioButton and ref

Parameters

ParameterTypeDescription
acroRadioButtonPDFAcroRadioButtonThe underlying PDFAcroRadioButton for this radio group.
refPDFRefThe unique reference for this radio group.
docPDFDocumentThe document to which this radio group will belong.

Returns

PDFRadioGroup


select()

ts
select(option): void;

Defined in: src/api/form/PDFRadioGroup.ts:185

Select an option for this radio group. This operation is analogous to a human user clicking one of the radio buttons in this group via a PDF reader to toggle it on. This method will update the underlying state of the radio group to indicate which option has been selected. PDF libraries and readers will be able to extract this value from the saved document and determine which option was selected.

For example:

js
const radioGroup = form.getRadioGroup('best.superhero.radioGroup')
radioGroup.select('One Punch Man')

This method will mark this radio group as dirty, causing its appearance streams to be updated when either [[PDFDocument.save]] or [[PDFForm.updateFieldAppearances]] is called. The updated appearance streams will display a dot inside the widget of this check box field that represents the selected option.

Parameters

ParameterTypeDescription
optionstringThe option to be selected.

Returns

void


updateAppearances()

ts
updateAppearances(provider?): void;

Defined in: src/api/form/PDFRadioGroup.ts:450

Update the appearance streams for each of this group's radio button widgets using the given appearance provider. If no provider is passed, the default appearance provider for radio groups will be used. For example:

js
const radioGroup = form.getRadioGroup('some.radioGroup.field')
radioGroup.updateAppearances((field, widget) => {
  ...
  return {
    normal: { on: drawRadioButton(...), off: drawRadioButton(...) },
    down: { on: drawRadioButton(...), off: drawRadioButton(...) },
  }
})

Parameters

ParameterTypeDescription
provider?RadioGroupAppearanceProviderOptionally, the appearance provider to be used for generating the contents of the appearance streams.

Returns

void


updateOnOffWidgetAppearance()

ts
protected updateOnOffWidgetAppearance(
   widget, 
   onValue, 
   __namedParameters): void;

Defined in: src/api/form/PDFField.ts:412

Parameters

ParameterType
widgetPDFWidgetAnnotation
onValuePDFName
__namedParametersAppearanceMapping<{ off: PDFOperator[]; on: PDFOperator[]; }>

Returns

void

Inherited from

PDFField.updateOnOffWidgetAppearance


updateWidgetAppearances()

ts
protected updateWidgetAppearances(widget, __namedParameters): void;

Defined in: src/api/form/PDFField.ts:429

Parameters

ParameterType
widgetPDFWidgetAnnotation
__namedParametersAppearanceMapping<PDFRef | PDFDict>

Returns

void

Inherited from

PDFField.updateWidgetAppearances


updateWidgetAppearanceWithFont()

ts
protected updateWidgetAppearanceWithFont(
   widget, 
   font, 
   __namedParameters): void;

Defined in: src/api/form/PDFField.ts:400

Parameters

ParameterType
widgetPDFWidgetAnnotation
fontPDFFont
__namedParametersAppearanceMapping<PDFOperator[]>

Returns

void

Inherited from

PDFField.updateWidgetAppearanceWithFont

MIT Licensed. A fork of pdf-lib.