Skip to content

happypdf / PDFButton

Class: PDFButton

Defined in: src/api/form/PDFButton.ts:35

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.

Extends

Properties

acroField

ts
readonly acroField: PDFAcroPushButton;

Defined in: src/api/form/PDFButton.ts:54

The low-level PDFAcroPushButton wrapped by this button.

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

addToPage()

ts
addToPage(
   text, 
   page, 
   options?): void;

Defined in: src/api/form/PDFButton.ts:149

Show this button on the specified page with the given text. For example:

js
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

ParameterTypeDescription
textstringThe text to be displayed for this button widget.
pagePDFPageThe page to which this button widget should be added.
options?FieldAppearanceOptionsThe options to be used when adding this button widget.

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(font): void;

Defined in: src/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:

js
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const button = form.getButton('some.button.field')
button.defaultUpdateAppearances(helvetica)

Parameters

ParameterTypeDescription
fontPDFFontThe font to be used for creating the appearance streams.

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


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


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


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


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/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:

js
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.

Overrides

ts
PDFField.needsAppearancesUpdate

of()

ts
static of(
   acroPushButton, 
   ref, 
   doc): PDFButton;

Defined in: src/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

ParameterTypeDescription
acroPushButtonPDFAcroPushButtonThe underlying PDFAcroPushButton for this button.
refPDFRefThe unique reference for this button.
docPDFDocumentThe document to which this button will belong.

Returns

PDFButton


setFontSize()

ts
setFontSize(fontSize): void;

Defined in: src/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:

js
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 the Tf operator), > then this method will throw an error.

Parameters

ParameterTypeDescription
fontSizenumberThe font size to be used when rendering text in this field.

Returns

void


setImage()

ts
setImage(image, alignment?): void;

Defined in: src/api/form/PDFButton.ts:81

Display an image inside the bounds of this button's widgets. For example:

js
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

ParameterTypeDefault valueDescription
imagePDFImageundefinedThe image that should be displayed.
alignmentImageAlignmentImageAlignment.CenterThe alignment of the image.

Returns

void


updateAppearances()

ts
updateAppearances(font, provider?): void;

Defined in: src/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:

js
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

ParameterTypeDescription
fontPDFFontThe font to be used for creating the appearance streams.
provider?ButtonAppearanceProviderOptionally, 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.