PDFButton
Represents a button field of a PDFForm.
PDFButton fields are interactive controls that users can click with their mouse. This type of PDFField is not stateful. The purpose of a button is to perform an action when the user clicks on it, such as opening a print modal or resetting the form. Buttons are typically rectangular in shape and have a text label describing the action that they perform when clicked.
Hierarchy
-
↳ PDFButton
Index
Properties
Methods
- addToPage
- defaultUpdateAppearances
- disableExporting
- disableReadOnly
- disableRequired
- enableExporting
- enableReadOnly
- enableRequired
- getName
- isExported
- isReadOnly
- isRequired
- needsAppearancesUpdate
- setFontSize
- setImage
- updateAppearances
- of
Properties
acroField
• acroField: PDFAcroPushButton
Defined in api/form/PDFButton.ts:54
The low-level PDFAcroPushButton wrapped by this button.
doc
• doc: PDFDocument
Defined in api/form/PDFField.ts:92
The document to which this field belongs.
ref
• ref: PDFRef
Defined in api/form/PDFField.ts:89
The unique reference assigned to this field within the document.
Methods
addToPage
▸ addToPage(text
: string, page
: PDFPage, options?
: FieldAppearanceOptions): void
Defined in api/form/PDFButton.ts:149
Show this button on the specified page with the given text. For example:
const ubuntuFont = await pdfDoc.embedFont(ubuntuFontBytes)
const page = pdfDoc.addPage()
const form = pdfDoc.getForm()
const button = form.createButton('some.button.field')
button.addToPage('Do Stuff', 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 button field.
Parameters:
Name | Type | Description |
---|---|---|
text | string | The text to be displayed for this button widget. |
page | PDFPage | The page to which this button widget should be added. |
options? | FieldAppearanceOptions | The options to be used when adding this button widget. |
Returns: void
defaultUpdateAppearances
▸ defaultUpdateAppearances(font
: PDFFont): void
Overrides void
Defined in api/form/PDFButton.ts:220
Update the appearance streams for each of this button's widgets using the default appearance provider for buttons. For example:
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const button = form.getButton('some.button.field')
button.defaultUpdateAppearances(helvetica)
Parameters:
Name | Type | Description |
---|---|---|
font | PDFFont | The font to be used for creating the appearance streams. |
Returns: void
disableExporting
▸ disableExporting(): void
Inherited from PDFField.disableExporting
Defined in api/form/PDFField.ts:247
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
disableReadOnly
▸ disableReadOnly(): void
Inherited from PDFField.disableReadOnly
Defined in api/form/PDFField.ts:170
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
disableRequired
▸ disableRequired(): void
Inherited from PDFField.disableRequired
Defined in api/form/PDFField.ts:208
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
enableExporting
▸ enableExporting(): void
Inherited from PDFField.enableExporting
Defined in api/form/PDFField.ts:235
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
enableReadOnly
▸ enableReadOnly(): void
Inherited from PDFField.enableReadOnly
Defined in api/form/PDFField.ts:158
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
enableRequired
▸ enableRequired(): void
Inherited from PDFField.enableRequired
Defined in api/form/PDFField.ts:196
Require this field to have a value when the form is submitted. For example:
const field = form.getField('some.field')
field.enableRequired()
Returns: void
getName
▸ getName(): string
Inherited from PDFField.getName
Defined in api/form/PDFField.ts:128
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.
isExported
▸ isExported(): boolean
Inherited from PDFField.isExported
Defined in api/form/PDFField.ts:223
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.
isReadOnly
▸ isReadOnly(): boolean
Inherited from PDFField.isReadOnly
Defined in api/form/PDFField.ts:143
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.
isRequired
▸ isRequired(): boolean
Inherited from PDFField.isRequired
Defined in api/form/PDFField.ts:184
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.
needsAppearancesUpdate
▸ needsAppearancesUpdate(): boolean
Overrides void
Defined in api/form/PDFButton.ts:196
Returns true
if this button has been marked as dirty, or if any of this
button's widgets do not have an appearance stream. For example:
const button = form.getButton('some.button.field')
if (button.needsAppearancesUpdate()) console.log('Needs update')
Returns: boolean
Whether or not this button needs an appearance update.
setFontSize
▸ setFontSize(fontSize
: number): void
Defined in api/form/PDFButton.ts:116
Set the font size for this field. Larger font sizes will result in larger text being displayed when PDF readers render this button. 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 button = form.getButton('some.button.field')
button.setFontSize(4)
button.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 theTf
operator), then this method will throw an error.
Parameters:
Name | Type | Description |
---|---|---|
fontSize | number | The font size to be used when rendering text in this field. |
Returns: void
setImage
▸ setImage(image
: PDFImage, alignment
: ImageAlignment): void
Defined in api/form/PDFButton.ts:81
Display an image inside the bounds of this button's widgets. For example:
const pngImage = await pdfDoc.embedPng(...)
const button = form.getButton('some.button.field')
button.setImage(pngImage, ImageAlignment.Center)
This will update the appearances streams for each of this button's widgets.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
image | PDFImage | - | The image that should be displayed. |
alignment | ImageAlignment | ImageAlignment.Center | The alignment of the image. |
Returns: void
updateAppearances
▸ updateAppearances(font
: PDFFont, provider?
: AppearanceProviderFor‹PDFButton›): void
Defined in api/form/PDFButton.ts:244
Update the appearance streams for each of this button's widgets using
the given appearance provider. If no provider
is passed, the default
appearance provider for buttons will be used. For example:
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const button = form.getButton('some.button.field')
button.updateAppearances(helvetica, (field, widget, font) => {
...
return {
normal: drawButton(...),
down: drawButton(...),
}
})
Parameters:
Name | Type | Description |
---|---|---|
font | PDFFont | The font to be used for creating the appearance streams. |
provider? | AppearanceProviderFor‹PDFButton› | Optionally, the appearance provider to be used for generating the contents of the appearance streams. |
Returns: void
Static
of
▸ of(acroPushButton
: PDFAcroPushButton, ref
: PDFRef, doc
: PDFDocument): PDFButton‹›
Defined in api/form/PDFButton.ts:47
NOTE: You probably don't want to call this method directly. Instead, consider using the PDFForm.getButton method, which will create an instance of PDFButton for you.
Create an instance of PDFButton from an existing acroPushButton and ref
Parameters:
Name | Type | Description |
---|---|---|
acroPushButton | PDFAcroPushButton | The underlying PDFAcroPushButton for this button. |
ref | PDFRef | The unique reference for this button. |
doc | PDFDocument | The document to which this button will belong. |
Returns: PDFButton‹›