happypdf / PDFDropdown
Class: PDFDropdown
Defined in: src/api/form/PDFDropdown.ts:39
Represents a dropdown field of a [[PDFForm]].
[[PDFDropdown]] fields are interactive text boxes that display a single element (the currently selected value). The purpose of a dropdown is to enable users to select a single option from a set of possible options. Users can click on a dropdown to view the full list of options it provides. Clicking on an option in the list will cause it to be selected and displayed in the dropdown's text box. Some dropdowns allow users to enter text directly into the box from their keyboard, rather than only being allowed to choose an option from the list (see [[PDFDropdown.isEditable]]).
Extends
Properties
acroField
readonly acroField: PDFAcroComboBox;Defined in: src/api/form/PDFDropdown.ts:55
The low-level PDFAcroComboBox wrapped by this dropdown.
Overrides
doc
readonly doc: PDFDocument;Defined in: src/api/form/PDFField.ts:96
The document to which this field belongs.
Inherited from
ref
readonly ref: PDFRef;Defined in: src/api/form/PDFField.ts:93
The unique reference assigned to this field within the document.
Inherited from
Methods
addOptions()
addOptions(options): void;Defined in: src/api/form/PDFDropdown.ts:157
Add to the list of options that are available for this dropdown. Users will be able to select these values in a PDF reader. In addition to the values passed as options, any preexisting options for this dropdown will still be available for users to select. For example:
const dropdown = form.getDropdown('rockets.dropdown')
dropdown.addOptions(['Saturn IV', 'Falcon Heavy'])Parameters
| Parameter | Type | Description |
|---|---|---|
options | string | string[] | New options that should be available in this dropdown. |
Returns
void
addToPage()
addToPage(page, options?): void;Defined in: src/api/form/PDFDropdown.ts:529
Show this dropdown on the specified page. For example:
const ubuntuFont = await pdfDoc.embedFont(ubuntuFontBytes)
const page = pdfDoc.addPage()
const form = pdfDoc.getForm()
const dropdown = form.createDropdown('best.gundam')
dropdown.setOptions(['Exia', 'Dynames'])
dropdown.select('Exia')
dropdown.addToPage(page, {
x: 50,
y: 75,
width: 200,
height: 100,
textColor: rgb(1, 0, 0),
backgroundColor: rgb(0, 1, 0),
borderColor: rgb(0, 0, 1),
borderWidth: 2,
rotate: degrees(90),
font: ubuntuFont,
})This will create a new widget for this dropdown field.
Parameters
| Parameter | Type | Description |
|---|---|---|
page | PDFPage | The page to which this dropdown widget should be added. |
options? | FieldAppearanceOptions | The options to be used when adding this dropdown widget. |
Returns
void
clear()
clear(): void;Defined in: src/api/form/PDFDropdown.ts:261
Clear all selected values for this dropdown. This operation is equivalent to selecting an empty list. This method will update the underlying state of the dropdown to indicate that no values have been selected. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.clear()This method will mark this text field as dirty. See [[PDFDropdown.select]] for more details about what this means.
Returns
void
createImageAppearanceStream()
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
| Parameter | Type | Description |
|---|---|---|
widget | PDFWidgetAnnotation | The widget that should display the image. |
image | PDFImage | The image that should be displayed. |
alignment | ImageAlignment | The alignment of the image. |
Returns
The ref for the FormXObject that was added to the context.
Inherited from
PDFField.createImageAppearanceStream
createWidget()
protected createWidget(options): PDFWidgetAnnotation;Defined in: src/api/form/PDFField.ts:333
Parameters
| Parameter | Type |
|---|---|
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.borderWidth | number |
options.caption? | string |
options.height | number |
options.hidden? | boolean |
options.page? | PDFRef |
options.rotate | Rotation |
options.textColor? | Color |
options.width | number |
options.x | number |
options.y | number |
Returns
Inherited from
defaultUpdateAppearances()
defaultUpdateAppearances(font): void;Defined in: src/api/form/PDFDropdown.ts:600
Update the appearance streams for each of this dropdown's widgets using the default appearance provider for dropdowns. For example:
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.defaultUpdateAppearances(helvetica)Parameters
| Parameter | Type | Description |
|---|---|---|
font | PDFFont | The font to be used for creating the appearance streams. |
Returns
void
Overrides
PDFField.defaultUpdateAppearancesdisableEditing()
disableEditing(): void;Defined in: src/api/form/PDFDropdown.ts:334
Do not allow users to edit the selected value of this dropdown in PDF readers with their keyboard. This will constrain the selected value of this dropdown to the list of available options. Users will only be able to select an option from that list. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.disableEditing()Returns
void
disableExporting()
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:
const field = form.getField('some.field')
field.disableExporting()Returns
void
Inherited from
disableMultiselect()
disableMultiselect(): void;Defined in: src/api/form/PDFDropdown.ts:413
Do not allow users to select more than one option from this dropdown's option list. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.disableMultiselect()Returns
void
disableReadOnly()
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:
const field = form.getField('some.field')
field.disableReadOnly()Returns
void
Inherited from
disableRequired()
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:
const field = form.getField('some.field')
field.disableRequired()Returns
void
Inherited from
disableSelectOnClick()
disableSelectOnClick(): void;Defined in: src/api/form/PDFDropdown.ts:497
Wait to store the option selected by a user until they leave this dropdown field (by clicking outside of it - on another field, for example). For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.disableSelectOnClick()Returns
void
disableSorting()
disableSorting(): void;Defined in: src/api/form/PDFDropdown.ts:375
Do not always display the option list of this dropdown in alphabetical order. Instead, display the options in whichever order they were added to the list. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.disableSorting()Returns
void
disableSpellChecking()
disableSpellChecking(): void;Defined in: src/api/form/PDFDropdown.ts:453
Do not allow PDF readers to spell check the selected option of this dropdown. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.disableSpellChecking()Returns
void
enableEditing()
enableEditing(): void;Defined in: src/api/form/PDFDropdown.ts:319
Allow users to edit the selected value of this dropdown in PDF readers with their keyboard. This means that the selected value of this dropdown will not be constrained by the list of available options. However, if this dropdown has any available options, users will still be allowed to select from that list. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.enableEditing()Returns
void
enableExporting()
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:
const field = form.getField('some.field')
field.enableExporting()Returns
void
Inherited from
enableMultiselect()
enableMultiselect(): void;Defined in: src/api/form/PDFDropdown.ts:401
Allow users to select more than one option from this dropdown's option list. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.enableMultiselect()Returns
void
enableReadOnly()
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:
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
enableRequired()
enableRequired(): void;Defined in: src/api/form/PDFField.ts:200
Require this field to have a value when the form is submitted. For example:
const field = form.getField('some.field')
field.enableRequired()Returns
void
Inherited from
enableSelectOnClick()
enableSelectOnClick(): void;Defined in: src/api/form/PDFDropdown.ts:484
Store the option selected by a user immediately after the user clicks the option. Do not wait for the user to leave this dropdown field (by clicking outside of it - on another field, for example). For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.enableSelectOnClick()Returns
void
enableSorting()
enableSorting(): void;Defined in: src/api/form/PDFDropdown.ts:362
Always display the option list of this dropdown in alphabetical order, irrespective of the order in which the options were added to this dropdown. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.enableSorting()Returns
void
enableSpellChecking()
enableSpellChecking(): void;Defined in: src/api/form/PDFDropdown.ts:441
Allow PDF readers to spell check the selected option of this dropdown. For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.enableSpellChecking()Returns
void
getAction()
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:
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
getJavaScriptActions()
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:
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
getName()
getName(): string;Defined in: src/api/form/PDFField.ts:132
Get the fully qualified name of this field. For example:
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
getOptions()
getOptions(): string[];Defined in: src/api/form/PDFDropdown.ts:82
Get the list of available options for this dropdown. These options will be displayed to users who click on this dropdown in a PDF reader. For example:
const dropdown = form.getDropdown('some.dropdown.field')
const options = dropdown.getOptions()
console.log('Dropdown options:', options)Returns
string[]
The options for this dropdown.
getSelected()
getSelected(): string[];Defined in: src/api/form/PDFDropdown.ts:111
Get the selected options for this dropdown. These are the values that were selected by a human user via a PDF reader, or programatically via software. For example:
const dropdown = form.getDropdown('some.dropdown.field')
const selections = dropdown.getSelected()
console.log('Dropdown selections:', selections)> NOTE: Note that PDF readers only display one selected option when > rendering dropdowns. However, the PDF specification does allow for > multiple values to be selected in a dropdown. As such, the pdf-lib > API supports this. However, in most cases the array returned by this > method will contain only a single element (or no elements).
Returns
string[]
The selected options in this dropdown.
isDirty()
protected isDirty(): boolean;Defined in: src/api/form/PDFField.ts:329
Returns
boolean
Inherited from
isEditable()
isEditable(): boolean;Defined in: src/api/form/PDFDropdown.ts:303
Returns true if users are allowed to edit the selected value of this dropdown directly and are not constrained by the list of available options. See [[PDFDropdown.enableEditing]] and [[PDFDropdown.disableEditing]]. For example:
const dropdown = form.getDropdown('some.dropdown.field')
if (dropdown.isEditable()) console.log('Editing is enabled')Returns
boolean
Whether or not this dropdown is editable.
isExported()
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:
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
isMultiselect()
isMultiselect(): boolean;Defined in: src/api/form/PDFDropdown.ts:389
Returns true if multiple options can be selected from this dropdown's option list. See [[PDFDropdown.enableMultiselect]] and [[PDFDropdown.disableMultiselect]]. For example:
const dropdown = form.getDropdown('some.dropdown.field')
if (dropdown.isMultiselect()) console.log('Multiselect is enabled')Returns
boolean
Whether or not multiple options can be selected.
isReadOnly()
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:
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
isRequired()
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:
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
isSelectOnClick()
isSelectOnClick(): boolean;Defined in: src/api/form/PDFDropdown.ts:471
Returns true if the option selected by a user is stored, or "committed", when the user clicks the option. The alternative is that the user's selection is stored when the user leaves this dropdown field (by clicking outside of it - on another field, for example). See [[PDFDropdown.enableSelectOnClick]] and [[PDFDropdown.disableSelectOnClick]]. For example:
const dropdown = form.getDropdown('some.dropdown.field')
if (dropdown.isSelectOnClick()) console.log('Select on click is enabled')Returns
boolean
Whether or not options are selected immediately after they are clicked.
isSorted()
isSorted(): boolean;Defined in: src/api/form/PDFDropdown.ts:349
Returns true if the option list of this dropdown is always displayed in alphabetical order, irrespective of the order in which the options were added to the dropdown. See [[PDFDropdown.enableSorting]] and [[PDFDropdown.disableSorting]]. For example:
const dropdown = form.getDropdown('some.dropdown.field')
if (dropdown.isSorted()) console.log('Sorting is enabled')Returns
boolean
Whether or not this dropdown's options are sorted.
isSpellChecked()
isSpellChecked(): boolean;Defined in: src/api/form/PDFDropdown.ts:429
Returns true if the selected option should be spell checked by PDF readers. Spell checking will only be performed if this dropdown allows editing (see [[PDFDropdown.isEditable]]). See [[PDFDropdown.enableSpellChecking]] and [[PDFDropdown.disableSpellChecking]]. For example:
const dropdown = form.getDropdown('some.dropdown.field')
if (dropdown.isSpellChecked()) console.log('Spell checking is enabled')Returns
boolean
Whether or not this dropdown can be spell checked.
markAsClean()
protected markAsClean(): void;Defined in: src/api/form/PDFField.ts:325
Returns
void
Inherited from
markAsDirty()
protected markAsDirty(): void;Defined in: src/api/form/PDFField.ts:321
Returns
void
Inherited from
needsAppearancesUpdate()
needsAppearancesUpdate(): boolean;Defined in: src/api/form/PDFDropdown.ts:576
Returns true if this dropdown has been marked as dirty, or if any of this dropdown's widgets do not have an appearance stream. For example:
const dropdown = form.getDropdown('some.dropdown.field')
if (dropdown.needsAppearancesUpdate()) console.log('Needs update')Returns
boolean
Whether or not this dropdown needs an appearance update.
Overrides
PDFField.needsAppearancesUpdateof()
static of(
acroComboBox,
ref,
doc): PDFDropdown;Defined in: src/api/form/PDFDropdown.ts:51
> NOTE: You probably don't want to call this method directly. Instead, > consider using the [[PDFForm.getDropdown]] method, which will create an > instance of [[PDFDropdown]] for you.
Create an instance of [[PDFDropdown]] from an existing acroComboBox and ref
Parameters
| Parameter | Type | Description |
|---|---|---|
acroComboBox | PDFAcroComboBox | The underlying PDFAcroComboBox for this dropdown. |
ref | PDFRef | The unique reference for this dropdown. |
doc | PDFDocument | The document to which this dropdown will belong. |
Returns
PDFDropdown
select()
select(options, merge?): void;Defined in: src/api/form/PDFDropdown.ts:218
Select one or more values for this dropdown. This operation is analogous to a human user opening the dropdown in a PDF reader and clicking on a value to select it. This method will update the underlying state of the dropdown to indicate which values have been selected. PDF libraries and readers will be able to extract these values from the saved document and determine which values were selected.
For example:
const dropdown = form.getDropdown('best.superhero.dropdown')
dropdown.select('One Punch Man')This method will mark this dropdown as dirty, causing its appearance streams to be updated when either [[PDFDocument.save]] or [[PDFForm.updateFieldAppearances]] is called. The updated streams will display the selected option inside the widgets of this dropdown.
IMPORTANT: The default font used to update appearance streams is [[StandardFonts.Helvetica]]. Note that this is a WinAnsi font. This means that encoding errors will be thrown if the selected option for this field contains characters outside the WinAnsi character set (the latin alphabet).
Embedding a custom font and passing it to [[PDFForm.updateFieldAppearances]] or [[PDFDropdown.updateAppearances]] allows you to generate appearance streams with characters outside the latin alphabet (assuming the custom font supports them).
Selecting an option that does not exist in this dropdown's option list (see [[PDFDropdown.getOptions]]) will enable editing on this dropdown (see [[PDFDropdown.enableEditing]]).
> NOTE: PDF readers only display one selected option when rendering > dropdowns. However, the PDF specification does allow for multiple values > to be selected in a dropdown. As such, the pdf-lib API supports this. > However, it is not recommended to select more than one value with this > method, as only one will be visible. [[PDFOptionList]] fields are better > suited for displaying multiple selected values.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
options | string | string[] | undefined | The options to be selected. |
merge | boolean | false | Whether or not existing selections should be preserved. |
Returns
void
setFontSize()
setFontSize(fontSize): void;Defined in: src/api/form/PDFDropdown.ts:286
Set the font size for this field. Larger font sizes will result in larger text being displayed when PDF readers render this dropdown. Font sizes may be integer or floating point numbers. Supplying a negative font size will cause this method to throw an error.
For example:
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.setFontSize(4)
dropdown.setFontSize(15.7)> This method depends upon the existence of a default appearance > (/DA) string. If this field does not have a default appearance string, > or that string does not contain a font size (via the Tf operator), > then this method will throw an error.
Parameters
| Parameter | Type | Description |
|---|---|---|
fontSize | number | The font size to be used when rendering text in this field. |
Returns
void
setOptions()
setOptions(options): void;Defined in: src/api/form/PDFDropdown.ts:135
Set the list of options that are available for this dropdown. These are the values that will be available for users to select when they view this dropdown in a PDF reader. Note that preexisting options for this dropdown will be removed. Only the values passed as options will be available to select. For example:
const dropdown = form.getDropdown('planets.dropdown')
dropdown.setOptions(['Earth', 'Mars', 'Pluto', 'Venus'])Parameters
| Parameter | Type | Description |
|---|---|---|
options | string[] | The options that should be available in this dropdown. |
Returns
void
updateAppearances()
updateAppearances(font, provider?): void;Defined in: src/api/form/PDFDropdown.ts:621
Update the appearance streams for each of this dropdown's widgets using the given appearance provider. If no provider is passed, the default appearance provider for dropdowns will be used. For example:
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const dropdown = form.getDropdown('some.dropdown.field')
dropdown.updateAppearances(helvetica, (field, widget, font) => {
...
return drawTextField(...)
})Parameters
| Parameter | Type | Description |
|---|---|---|
font | PDFFont | The font to be used for creating the appearance streams. |
provider? | DropdownAppearanceProvider | Optionally, the appearance provider to be used for generating the contents of the appearance streams. |
Returns
void
updateOnOffWidgetAppearance()
protected updateOnOffWidgetAppearance(
widget,
onValue,
__namedParameters): void;Defined in: src/api/form/PDFField.ts:412
Parameters
| Parameter | Type |
|---|---|
widget | PDFWidgetAnnotation |
onValue | PDFName |
__namedParameters | AppearanceMapping<{ off: PDFOperator[]; on: PDFOperator[]; }> |
Returns
void
Inherited from
PDFField.updateOnOffWidgetAppearance
updateWidgetAppearances()
protected updateWidgetAppearances(widget, __namedParameters): void;Defined in: src/api/form/PDFField.ts:429
Parameters
| Parameter | Type |
|---|---|
widget | PDFWidgetAnnotation |
__namedParameters | AppearanceMapping<PDFRef | PDFDict> |
Returns
void
Inherited from
PDFField.updateWidgetAppearances
updateWidgetAppearanceWithFont()
protected updateWidgetAppearanceWithFont(
widget,
font,
__namedParameters): void;Defined in: src/api/form/PDFField.ts:400
Parameters
| Parameter | Type |
|---|---|
widget | PDFWidgetAnnotation |
font | PDFFont |
__namedParameters | AppearanceMapping<PDFOperator[]> |
Returns
void