Skip to content

happypdf / PDFTextField

Class: PDFTextField ​

Defined in: src/api/form/PDFTextField.ts:49

Represents a text field of a [[PDFForm]].

[[PDFTextField]] fields are boxes that display text entered by the user. The purpose of a text field is to enable users to enter text or view text values in the document prefilled by software. Users can click on a text field and input text via their keyboard. Some text fields allow multiple lines of text to be entered (see [[PDFTextField.isMultiline]]).

Extends ​

Properties ​

acroField ​

ts
readonly acroField: PDFAcroText;

Defined in: src/api/form/PDFTextField.ts:65

The low-level PDFAcroText wrapped by this text field.

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(page, options?): void;

Defined in: src/api/form/PDFTextField.ts:710

Show this text field on the specified page. For example:

js
const ubuntuFont = await pdfDoc.embedFont(ubuntuFontBytes)
const page = pdfDoc.addPage()

const form = pdfDoc.getForm()
const textField = form.createTextField('best.gundam')
textField.setText('Exia')

textField.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 text field.

Parameters ​

ParameterTypeDescription
pagePDFPageThe page to which this text field widget should be added.
options?FieldAppearanceOptionsThe options to be used when adding this text field 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/PDFTextField.ts:781

Update the appearance streams for each of this text field's widgets using the default appearance provider for text fields. For example:

js
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const textField = form.getTextField('some.text.field')
textField.defaultUpdateAppearances(helvetica)

Parameters ​

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

Returns ​

void

Overrides ​

ts
PDFField.defaultUpdateAppearances

disableCombing() ​

ts
disableCombing(): void;

Defined in: src/api/form/PDFTextField.ts:634

Turn off combing for this text field. For example:

js
const textField = form.getTextField('some.text.field')
textField.disableCombing()

See [[PDFTextField.isCombed]] and [[PDFTextField.enableCombing]] for more information about what combing is.

This method will mark this text field as dirty. See [[PDFTextField.setText]] for more details about what this means.

Returns ​

void


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


disableFileSelection() ​

ts
disableFileSelection(): void;

Defined in: src/api/form/PDFTextField.ts:474

Indicate that this text field is not intended to store a file path. For example:

js
const textField = form.getTextField('some.text.field')
textField.disableFileSelection()

Returns ​

void


disableMultiline() ​

ts
disableMultiline(): void;

Defined in: src/api/form/PDFTextField.ts:386

Display each line of text on the same line when this field is displayed in a PDF reader. For example:

js
const textField = form.getTextField('some.text.field')
textField.disableMultiline()

This method will mark this text field as dirty. See [[PDFTextField.setText]] for more details about what this means.

Returns ​

void


disablePassword() ​

ts
disablePassword(): void;

Defined in: src/api/form/PDFTextField.ts:435

Indicate that this text field is not intended for storing a secure password. For example:

js
const textField = form.getTextField('some.text.field')
textField.disablePassword()

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


disableRichFormatting() ​

ts
disableRichFormatting(): void;

Defined in: src/api/form/PDFTextField.ts:679

Indicate that this is a standard text field that does not XFA data (rich text). For example:

js
const textField = form.getTextField('some.text.field')
textField.disableRichFormatting()

Returns ​

void


disableScrolling() ​

ts
disableScrolling(): void;

Defined in: src/api/form/PDFTextField.ts:553

Do not allow PDF readers to present a scroll bar to the user when the contents of this text field do not fit within its view bounds. For example:

js
const textField = form.getTextField('some.text.field')
textField.disableScrolling()

Returns ​

void


disableSpellChecking() ​

ts
disableSpellChecking(): void;

Defined in: src/api/form/PDFTextField.ts:512

Do not allow PDF readers to spell check the text entered in this field. For example:

js
const textField = form.getTextField('some.text.field')
textField.disableSpellChecking()

Returns ​

void


enableCombing() ​

ts
enableCombing(): void;

Defined in: src/api/form/PDFTextField.ts:607

Split this field into n equal size cells with one character in each (where n is equal to the max length of the text field). This will cause all characters in the field to be displayed an equal distance apart from one another. For example:

js
const textField = form.getTextField('some.text.field')
textField.enableCombing()

In addition to calling this method, text fields must have a max length defined in order to be combed (see [[PDFTextField.setMaxLength]]).

This method will also call the following three methods internally:

  • [[PDFTextField.disableMultiline]]
  • [[PDFTextField.disablePassword]]
  • [[PDFTextField.disableFileSelection]]

This method will mark this text field as dirty. See [[PDFTextField.setText]] for more details about what this means.

Returns ​

void


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


enableFileSelection() ​

ts
enableFileSelection(): void;

Defined in: src/api/form/PDFTextField.ts:462

Indicate that this text field is intended to store a file path. The contents of the file stored at that path should be submitted as the value of the field. For example:

js
const textField = form.getTextField('some.text.field')
textField.enableFileSelection()

Returns ​

void


enableMultiline() ​

ts
enableMultiline(): void;

Defined in: src/api/form/PDFTextField.ts:371

Display each line of text on a new line when this field is displayed in a PDF reader. For example:

js
const textField = form.getTextField('some.text.field')
textField.enableMultiline()

This method will mark this text field as dirty. See [[PDFTextField.setText]] for more details about what this means.

Returns ​

void


enablePassword() ​

ts
enablePassword(): void;

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

Indicate that this text field is intended for storing a secure password. For example:

js
const textField = form.getTextField('some.text.field')
textField.enablePassword()

Values entered into password text fields should not be displayed on the screen by PDF readers. Most PDF readers will display the value as asterisks or bullets. PDF readers should never store values entered by the user into password text fields. Similarly, applications should not write data to a password text field.

Please note that this method does not cause entered values to be encrypted or secured in any way! It simply sets a flag that PDF software and readers can access to determine the purpose of this field.

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


enableRichFormatting() ​

ts
enableRichFormatting(): void;

Defined in: src/api/form/PDFTextField.ts:667

Indicate that this field contains XFA data - or rich text. For example:

js
const textField = form.getTextField('some.text.field')
textField.enableRichFormatting()

Note that pdf-lib does not support reading or writing rich text fields. Nor do most PDF readers and writers. Rich text fields are based on XFA (XML Forms Architecture). Relatively few PDFs use rich text fields or XFA. Unlike PDF itself, XFA is not an ISO standard. XFA has been deprecated in PDF 2.0:

Returns ​

void


enableScrolling() ​

ts
enableScrolling(): void;

Defined in: src/api/form/PDFTextField.ts:541

Allow PDF readers to present a scroll bar to the user when the contents of this text field do not fit within its view bounds. For example:

js
const textField = form.getTextField('some.text.field')
textField.enableScrolling()

A horizontal scroll bar should be shown for singleline fields. A vertical scroll bar should be shown for multiline fields.

Returns ​

void


enableSpellChecking() ​

ts
enableSpellChecking(): void;

Defined in: src/api/form/PDFTextField.ts:500

Allow PDF readers to spell check the text entered in this field. For example:

js
const textField = form.getTextField('some.text.field')
textField.enableSpellChecking()

Returns ​

void


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


getAlignment() ​

ts
getAlignment(): TextAlignment;

Defined in: src/api/form/PDFTextField.ts:180

Get the alignment for this text field. This value represents the justification of the text when it is displayed to the user in PDF readers. There are three possible alignments: left, center, and right. For example:

js
const textField = form.getTextField('some.text.field')
const alignment = textField.getAlignment()
if (alignment === TextAlignment.Left) console.log('Text is left justified')
if (alignment === TextAlignment.Center) console.log('Text is centered')
if (alignment === TextAlignment.Right) console.log('Text is right justified')

Returns ​

TextAlignment

The alignment of this text field.


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


getMaxLength() ​

ts
getMaxLength(): number | undefined;

Defined in: src/api/form/PDFTextField.ts:232

Get the maximum length of this field. This value represents the maximum number of characters that can be typed into this field by the user. If this field does not have a maximum length, undefined is returned. For example:

js
const textField = form.getTextField('some.text.field')
const maxLength = textField.getMaxLength()
if (maxLength === undefined) console.log('No max length')
else console.log(`Max length is ${maxLength}`)

Returns ​

number | undefined

The maximum number of characters allowed in this field, or undefined if no limit exists.


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


getText() ​

ts
getText(): string | undefined;

Defined in: src/api/form/PDFTextField.ts:103

Get the text that this field contains. This text is visible to users who view this field in a PDF reader.

For example:

js
const textField = form.getTextField('some.text.field')
const text = textField.getText()
console.log('Text field contents:', text)

Note that if this text field contains no underlying value, undefined will be returned. Text fields may also contain an underlying value that is simply an empty string (''). This detail is largely irrelevant for most applications. In general, you'll want to treat both cases the same way and simply consider the text field to be empty. In either case, the text field will appear empty to users when viewed in a PDF reader.

An error will be thrown if this is a rich text field. pdf-lib does not support reading rich text fields. Nor do most PDF readers and writers. Rich text fields are based on XFA (XML Forms Architecture). Relatively few PDFs use rich text fields or XFA. Unlike PDF itself, XFA is not an ISO standard. XFA has been deprecated in PDF 2.0:

Returns ​

string | undefined

The text contained in this text field.


isCombed() ​

ts
isCombed(): boolean;

Defined in: src/api/form/PDFTextField.ts:576

Returns true if this is a combed text field. This means that the field is split into n equal size cells with one character in each (where n is equal to the max length of the text field). The result is that all characters in this field are displayed an equal distance apart from one another. See [[PDFTextField.enableCombing]] and [[PDFTextField.disableCombing]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isCombed()) console.log('Combing is enabled')

Note that in order for a text field to be combed, the following must be true (in addition to enabling combing):

  • It must not be a multiline field (see [[PDFTextField.isMultiline]])
  • It must not be a password field (see [[PDFTextField.isPassword]])
  • It must not be a file selector field (see [[PDFTextField.isFileSelector]])
  • It must have a max length defined (see [[PDFTextField.setMaxLength]])

Returns ​

boolean

Whether or not this field is combed.


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


isFileSelector() ​

ts
isFileSelector(): boolean;

Defined in: src/api/form/PDFTextField.ts:449

Returns true if the contents of this text field represent a file path. See [[PDFTextField.enableFileSelection]] and [[PDFTextField.disableFileSelection]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isFileSelector()) console.log('Is a file selector')

Returns ​

boolean

Whether or not this field should contain file paths.


isMultiline() ​

ts
isMultiline(): boolean;

Defined in: src/api/form/PDFTextField.ts:357

Returns true if each line of text is shown on a new line when this field is displayed in a PDF reader. The alternative is that all lines of text are merged onto a single line when displayed. See [[PDFTextField.enableMultiline]] and [[PDFTextField.disableMultiline]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isMultiline()) console.log('Multiline is enabled')

Returns ​

boolean

Whether or not this is a multiline text field.


isPassword() ​

ts
isPassword(): boolean;

Defined in: src/api/form/PDFTextField.ts:402

Returns true if this is a password text field. This means that the field is intended for storing a secure password. See [[PDFTextField.enablePassword]] and [[PDFTextField.disablePassword]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isPassword()) console.log('Password is enabled')

Returns ​

boolean

Whether or not this is a password text field.


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


isRichFormatted() ​

ts
isRichFormatted(): boolean;

Defined in: src/api/form/PDFTextField.ts:649

Returns true if this text field contains rich text. See [[PDFTextField.enableRichFormatting]] and [[PDFTextField.disableRichFormatting]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isRichFormatted()) console.log('Rich formatting enabled')

Returns ​

boolean

Whether or not this field contains rich text.


isScrollable() ​

ts
isScrollable(): boolean;

Defined in: src/api/form/PDFTextField.ts:527

Returns true if PDF readers should allow the user to scroll the text field when its contents do not fit within the field's view bounds. See [[PDFTextField.enableScrolling]] and [[PDFTextField.disableScrolling]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isScrollable()) console.log('Scrolling is enabled')

Returns ​

boolean

Whether or not the field is scrollable in PDF readers.


isSpellChecked() ​

ts
isSpellChecked(): boolean;

Defined in: src/api/form/PDFTextField.ts:488

Returns true if the text entered in this field should be spell checked by PDF readers. See [[PDFTextField.enableSpellChecking]] and [[PDFTextField.disableSpellChecking]]. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.isSpellChecked()) console.log('Spell checking is enabled')

Returns ​

boolean

Whether or not this field should be spell checked.


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/PDFTextField.ts:757

Returns true if this text field has been marked as dirty, or if any of this text field's widgets do not have an appearance stream. For example:

js
const textField = form.getTextField('some.text.field')
if (textField.needsAppearancesUpdate()) console.log('Needs update')

Returns ​

boolean

Whether or not this text field needs an appearance update.

Overrides ​

ts
PDFField.needsAppearancesUpdate

of() ​

ts
static of(
   acroText, 
   ref, 
   doc): PDFTextField;

Defined in: src/api/form/PDFTextField.ts:61

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

Create an instance of [[PDFTextField]] from an existing acroText and ref

Parameters ​

ParameterTypeDescription
acroTextPDFAcroTextThe underlying PDFAcroText for this text field.
refPDFRefThe unique reference for this text field.
docPDFDocumentThe document to which this text field will belong.

Returns ​

PDFTextField


removeMaxLength() ​

ts
removeMaxLength(): void;

Defined in: src/api/form/PDFTextField.ts:281

Remove the maximum length for this text field. This allows any number of characters to be typed into this field by the user. For example:

js
const textField = form.getTextField('some.text.field')
textField.removeMaxLength()

Calling this method is equivalent to passing undefined to [[PDFTextField.setMaxLength]].

Returns ​

void


setAlignment() ​

ts
setAlignment(alignment): void;

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

Set the alignment for this text field. This will determine the justification of the text when it is displayed to the user in PDF readers. There are three possible alignments: left, center, and right. For example:

js
const textField = form.getTextField('some.text.field')

// Text will be left justified when displayed
textField.setAlignment(TextAlignment.Left)

// Text will be centered when displayed
textField.setAlignment(TextAlignment.Center)

// Text will be right justified when displayed
textField.setAlignment(TextAlignment.Right)

This method will mark this text field as dirty. See [[PDFTextField.setText]] for more details about what this means.

Parameters ​

ParameterTypeDescription
alignmentTextAlignmentThe alignment for this text field.

Returns ​

void


setFontSize() ​

ts
setFontSize(fontSize): void;

Defined in: src/api/form/PDFTextField.ts:339

Set the font size for this field. Larger font sizes will result in larger text being displayed when PDF readers render this text field. 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 textField = form.getTextField('some.text.field')
textField.setFontSize(4)
textField.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): void;

Defined in: src/api/form/PDFTextField.ts:296

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

js
const pngImage = await pdfDoc.embedPng(...)
const textField = form.getTextField('some.text.field')
textField.setImage(pngImage)

This will update the appearances streams for each of this text field's widgets.

Parameters ​

ParameterTypeDescription
imagePDFImageThe image that should be displayed.

Returns ​

void


setMaxLength() ​

ts
setMaxLength(maxLength?): void;

Defined in: src/api/form/PDFTextField.ts:255

Set the maximum length of this field. This limits the number of characters that can be typed into this field by the user. This also limits the length of the string that can be passed to [[PDFTextField.setText]]. This limit can be removed by passing undefined as maxLength. For example:

js
const textField = form.getTextField('some.text.field')

// Allow between 0 and 5 characters to be entered
textField.setMaxLength(5)

// Allow any number of characters to be entered
textField.setMaxLength(undefined)

This method will mark this text field as dirty. See [[PDFTextField.setText]] for more details about what this means.

Parameters ​

ParameterTypeDescription
maxLength?numberThe maximum number of characters allowed in this field, or undefined to remove the limit.

Returns ​

void


setText() ​

ts
setText(text): void;

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

Set the text for this field. This operation is analogous to a human user clicking on the text field in a PDF reader and typing in text via their keyboard. This method will update the underlying state of the text field to indicate what text has been set. PDF libraries and readers will be able to extract these values from the saved document and determine what text was set.

For example:

js
const textField = form.getTextField('best.superhero.text.field')
textField.setText('One Punch Man')

This method will mark this text field as dirty, causing its appearance streams to be updated when either [[PDFDocument.save]] or [[PDFForm.updateFieldAppearances]] is called. The updated streams will display the text this field contains inside the widgets of this text field.

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 this field contains text outside the WinAnsi character set (the latin alphabet).

Embedding a custom font and passing it to [[PDFForm.updateFieldAppearances]] or [[PDFTextField.updateAppearances]] allows you to generate appearance streams with characters outside the latin alphabet (assuming the custom font supports them).

If this is a rich text field, it will be converted to a standard text field in order to set the text. pdf-lib does not support writing rich text strings. Nor do most PDF readers and writers. See [[PDFTextField.getText]] for more information about rich text fields and their deprecation in PDF 2.0.

Parameters ​

ParameterTypeDescription
textstring | undefinedThe text this field should contain.

Returns ​

void


updateAppearances() ​

ts
updateAppearances(font, provider?): void;

Defined in: src/api/form/PDFTextField.ts:802

Update the appearance streams for each of this text field's widgets using the given appearance provider. If no provider is passed, the default appearance provider for text fields will be used. For example:

js
const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
const textField = form.getTextField('some.text.field')
textField.updateAppearances(helvetica, (field, widget, font) => {
  ...
  return drawTextField(...)
})

Parameters ​

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