happypdf / PDFCheckBox
Class: PDFCheckBox
Defined in: src/api/form/PDFCheckBox.ts:33
Represents a check box field of a [[PDFForm]].
[[PDFCheckBox]] fields are interactive boxes that users can click with their mouse. This type of [[PDFField]] has two states: on and off. The purpose of a check box is to enable users to select from one or more options, where each option is represented by a single check box. Check boxes are typically square in shape and display a check mark when they are in the on state.
Extends
Properties
acroField
readonly acroField: PDFAcroCheckBox;Defined in: src/api/form/PDFCheckBox.ts:49
The low-level PDFAcroCheckBox wrapped by this check box.
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
addToPage()
addToPage(page, options?): void;Defined in: src/api/form/PDFCheckBox.ts:150
Show this check box on the specified page. For example:
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const page = pdfDoc.addPage()
const form = pdfDoc.getForm()
const checkBox = form.createCheckBox('some.checkBox.field')
checkBox.addToPage(page, {
x: 50,
y: 75,
width: 25,
height: 25,
textColor: rgb(1, 0, 0),
backgroundColor: rgb(0, 1, 0),
borderColor: rgb(0, 0, 1),
borderWidth: 2,
rotate: degrees(90),
})This will create a new widget for this check box field.
Parameters
| Parameter | Type | Description |
|---|---|---|
page | PDFPage | The page to which this check box widget should be added. |
options? | FieldAppearanceOptions | The options to be used when adding this check box widget. |
Returns
void
check()
check(): void;Defined in: src/api/form/PDFCheckBox.ts:84
Mark this check box. This operation is analogous to a human user clicking a check box to fill it in a PDF reader. This method will update the underlying state of the check box field to indicate it has been selected. PDF libraries and readers will be able to extract this value from the saved document and determine that it was selected.
For example:
const checkBox = form.getCheckBox('some.checkBox.field')
checkBox.check()This method will mark this check box 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 check mark inside the widgets of this check box field.
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(): void;Defined in: src/api/form/PDFCheckBox.ts:219
Update the appearance streams for each of this check box's widgets using the default appearance provider for check boxes. For example:
const checkBox = form.getCheckBox('some.checkBox.field')
checkBox.defaultUpdateAppearances()Returns
void
Overrides
PDFField.defaultUpdateAppearancesdisableExporting()
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
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
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
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
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
isChecked()
isChecked(): boolean;Defined in: src/api/form/PDFCheckBox.ts:120
Returns true if this check box is selected (either by a human user via a PDF reader, or else programmatically via software). For example:
const checkBox = form.getCheckBox('some.checkBox.field')
if (checkBox.isChecked()) console.log('check box is selected')Returns
boolean
Whether or not this check box is selected.
isDirty()
protected isDirty(): boolean;Defined in: src/api/form/PDFField.ts:329
Returns
boolean
Inherited from
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
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
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/PDFCheckBox.ts:197
Returns true if any of this check box's widgets do not have an appearance stream for its current state. For example:
const checkBox = form.getCheckBox('some.checkBox.field')
if (checkBox.needsAppearancesUpdate()) console.log('Needs update')Returns
boolean
Whether or not this check box needs an appearance update.
Overrides
PDFField.needsAppearancesUpdateof()
static of(
acroCheckBox,
ref,
doc): PDFCheckBox;Defined in: src/api/form/PDFCheckBox.ts:45
> NOTE: You probably don't want to call this method directly. Instead, > consider using the [[PDFForm.getCheckBox]] method, which will create an > instance of [[PDFCheckBox]] for you.
Create an instance of [[PDFCheckBox]] from an existing acroCheckBox and ref
Parameters
| Parameter | Type | Description |
|---|---|---|
acroCheckBox | PDFAcroCheckBox | The underlying PDFAcroCheckBox for this check box. |
ref | PDFRef | The unique reference for this check box. |
doc | PDFDocument | The document to which this check box will belong. |
Returns
PDFCheckBox
uncheck()
uncheck(): void;Defined in: src/api/form/PDFCheckBox.ts:106
Clears this check box. This operation is analogous to a human user clicking a check box to unmark it in a PDF reader. This method will update the underlying state of the check box field to indicate it has been deselected. PDF libraries and readers will be able to extract this value from the saved document and determine that it was not selected.
For example:
const checkBox = form.getCheckBox('some.checkBox.field')
checkBox.uncheck()This method will mark this check box as dirty. See [[PDFCheckBox.check]] for more details about what this means.
Returns
void
updateAppearances()
updateAppearances(provider?): void;Defined in: src/api/form/PDFCheckBox.ts:240
Update the appearance streams for each of this check box's widgets using the given appearance provider. If no provider is passed, the default appearance provider for check boxs will be used. For example:
const checkBox = form.getCheckBox('some.checkBox.field')
checkBox.updateAppearances((field, widget) => {
...
return {
normal: { on: drawCheckBox(...), off: drawCheckBox(...) },
down: { on: drawCheckBox(...), off: drawCheckBox(...) },
}
})Parameters
| Parameter | Type | Description |
|---|---|---|
provider? | CheckBoxAppearanceProvider | 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