Skip to content

modern-pdf-lib


modern-pdf-lib / PdfTextField

Class: PdfTextField

Defined in: src/form/fields/textField.ts:43

A PDF text form field (/FT /Tx).

Stores a string value and supports properties like alignment, multiline mode, password masking, and maximum length.

Extends

Constructors

Constructor

new PdfTextField(name, dict, widgetDict, parentNames?): PdfTextField

Defined in: src/form/pdfField.ts:175

Parameters

name

string

dict

PdfDict

widgetDict

PdfDict

parentNames?

string[] = []

Returns

PdfTextField

Inherited from

PdfField.constructor

Properties

dict

protected readonly dict: PdfDict

Defined in: src/form/pdfField.ts:164

The underlying field dictionary (may contain both field and widget entries for simple one-widget fields).

Inherited from

PdfField.dict


fieldType

readonly fieldType: FieldType = 'text'

Defined in: src/form/fields/textField.ts:44

Discriminator for the concrete field type.

Overrides

PdfField.fieldType


name

readonly name: string

Defined in: src/form/pdfField.ts:158

The fully-qualified field name.

Inherited from

PdfField.name


parentNames

protected readonly parentNames: string[]

Defined in: src/form/pdfField.ts:173

Parent field dictionary chain for building full names.

Inherited from

PdfField.parentNames


widgetDict

protected readonly widgetDict: PdfDict

Defined in: src/form/pdfField.ts:170

The widget annotation dictionary. For merged field+widget dicts, this is the same object as dict.

Inherited from

PdfField.widgetDict

Methods

addToPage()

addToPage(page): void

Defined in: src/form/pdfField.ts:309

Add this field's widget annotation to a page.

Ensures the widget dict has /Type /Annot and /Subtype /Widget, then adds it to the page's annotation list so it appears in the rendered PDF.

Parameters

page

WidgetAnnotationHost

A page that implements WidgetAnnotationHost.

Returns

void

Inherited from

PdfField.addToPage


disableExporting()

disableExporting(): void

Defined in: src/form/pdfField.ts:271

Disable exporting this field (set the NoExport flag).

Returns

void

Inherited from

PdfField.disableExporting


enableExporting()

enableExporting(): void

Defined in: src/form/pdfField.ts:266

Enable exporting this field (clear the NoExport flag).

Returns

void

Inherited from

PdfField.enableExporting


generateAppearance()

generateAppearance(): PdfStream

Defined in: src/form/fields/textField.ts:229

Generate the appearance stream for this text field.

Returns

PdfStream

Overrides

PdfField.generateAppearance


getAlignment()

getAlignment(): "center" | "left" | "right"

Defined in: src/form/fields/textField.ts:123

Get the text alignment. /Q: 0 = left, 1 = center, 2 = right.

Returns

"center" | "left" | "right"


getFieldFlags()

protected getFieldFlags(): number

Defined in: src/form/pdfField.ts:211

Get the raw /Ff (field flags) integer value.

Returns

number

Inherited from

PdfField.getFieldFlags


getFontName()

getFontName(): string

Defined in: src/form/fields/textField.ts:108

Get the font name from the /DA string. Returns "Helv" (Helvetica) as default.

Returns

string


getFontSize()

getFontSize(): number

Defined in: src/form/fields/textField.ts:83

Get the font size from the /DA (default appearance) string. Returns 0 if the font size is not specified or is auto.

Returns

number


getFullName()

getFullName(): string

Defined in: src/form/pdfField.ts:201

Get the fully qualified field name (Parent.Child.Name format). Per PDF spec SS12.7.3.2, the full name is formed by concatenating ancestor /T values with periods.

Returns

string

Inherited from

PdfField.getFullName


getMaxLength()

getMaxLength(): number | undefined

Defined in: src/form/fields/textField.ts:166

Get the maximum length, or undefined if no limit.

Returns

number | undefined


getName()

getName(): string

Defined in: src/form/pdfField.ts:192

Get the partial field name (/T entry).

Returns

string

Inherited from

PdfField.getName


getRect()

getRect(): [number, number, number, number]

Defined in: src/form/pdfField.ts:283

Get the field's widget rectangle as [x1, y1, x2, y2]. The /Rect entry comes from the widget annotation dictionary.

Returns

[number, number, number, number]

Inherited from

PdfField.getRect


getText()

getText(): string

Defined in: src/form/fields/textField.ts:51

Get the text value of this field.

Returns

string


getValue()

getValue(): string

Defined in: src/form/fields/textField.ts:66

Alias for getText().

Returns

string

Overrides

PdfField.getValue


hasFlag()

protected hasFlag(flag): boolean

Defined in: src/form/pdfField.ts:221

Check if a specific flag bit is set.

Parameters

flag

number

Returns

boolean

Inherited from

PdfField.hasFlag


isExported()

isExported(): boolean

Defined in: src/form/pdfField.ts:261

Whether the field is exported (inverse of NoExport flag).

Returns

boolean

Inherited from

PdfField.isExported


isMultiline()

isMultiline(): boolean

Defined in: src/form/fields/textField.ts:147

Whether this is a multiline text field.

Returns

boolean


isNoExport()

isNoExport(): boolean

Defined in: src/form/pdfField.ts:256

Whether the field should not be exported.

Returns

boolean

Inherited from

PdfField.isNoExport


isPassword()

isPassword(): boolean

Defined in: src/form/fields/textField.ts:157

Whether this is a password field.

Returns

boolean


isReadOnly()

isReadOnly(): boolean

Defined in: src/form/pdfField.ts:236

Whether the field is read-only.

Returns

boolean

Inherited from

PdfField.isReadOnly


isRequired()

isRequired(): boolean

Defined in: src/form/pdfField.ts:246

Whether the field is required.

Returns

boolean

Inherited from

PdfField.isRequired


setAlignment()

setAlignment(align): void

Defined in: src/form/fields/textField.ts:136

Set the text alignment.

Parameters

align

"center" | "left" | "right"

Returns

void


setFieldFlags()

protected setFieldFlags(flags): void

Defined in: src/form/pdfField.ts:216

Set the raw /Ff (field flags) integer value.

Parameters

flags

number

Returns

void

Inherited from

PdfField.setFieldFlags


setFlag()

protected setFlag(flag, on): void

Defined in: src/form/pdfField.ts:226

Set or clear a specific flag bit.

Parameters

flag

number

on

boolean

Returns

void

Inherited from

PdfField.setFlag


setFontSize()

setFontSize(size): void

Defined in: src/form/fields/textField.ts:98

Set the font size in the /DA string. Creates or updates the /DA entry.

Parameters

size

number

Returns

void


setImage()

setImage(imageRef): void

Defined in: src/form/fields/textField.ts:188

Set an image on this text field.

Creates an appearance stream that paints the image XObject scaled to fit the widget rectangle. This replaces the text appearance.

Parameters

imageRef

An object with name (resource name) and ref (PdfRef) pointing to the image XObject, plus width and height.

height

number

name

string

ref

PdfRef

width

number

Returns

void


setMaxLength()

setMaxLength(maxLength): void

Defined in: src/form/fields/textField.ts:171

Set the maximum length.

Parameters

maxLength

number

Returns

void


setMultiline()

setMultiline(multiline): void

Defined in: src/form/fields/textField.ts:152

Set the multiline flag.

Parameters

multiline

boolean

Returns

void


setReadOnly()

setReadOnly(readOnly): void

Defined in: src/form/pdfField.ts:241

Set the read-only flag.

Parameters

readOnly

boolean

Returns

void

Inherited from

PdfField.setReadOnly


setRequired()

setRequired(required): void

Defined in: src/form/pdfField.ts:251

Set the required flag.

Parameters

required

boolean

Returns

void

Inherited from

PdfField.setRequired


setText()

setText(value): void

Defined in: src/form/fields/textField.ts:59

Set the text value of this field. Also updates /V and removes /AP to force regeneration.

Parameters

value

string

Returns

void


setValue()

setValue(value): void

Defined in: src/form/fields/textField.ts:71

Alias for setText().

Parameters

value

string | boolean | string[]

Returns

void

Overrides

PdfField.setValue

Released under the MIT License.