@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
9,271 lines • 398 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { _PdfDictionary, _PdfReference, _PdfName } from './../pdf-primitives';
import { PdfRadioButtonListItem, PdfStateItem, PdfWidgetAnnotation, PdfListFieldItem, _PaintParameter, PdfInteractiveBorder } from './../annotations/annotation';
import { _getItemValue, _checkField, _removeReferences, _removeDuplicateReference, _updateVisibility, _styleToString, _getStateTemplate, _findPage, _getInheritableProperty, _getNewGuidString, _calculateBounds, _parseColor, _mapHighlightMode, _reverseMapHighlightMode, _mapBorderStyle, _getUpdatedBounds, _setMatrix, _obtainFontDetails, _isNullOrUndefined, _stringToPdfString, _mapFont, _isRightToLeftCharacters, _getFontStyle, _createFontStream, _encode, _getFontFromDescriptor, _decodeFontFamily, _updateDashedBorderStyle } from './../utils';
import { _PdfCheckFieldState, PdfFormFieldVisibility, _FieldFlag, PdfAnnotationFlag, PdfTextAlignment, PdfHighlightMode, PdfBorderStyle, PdfRotationAngle, PdfCheckBoxStyle, PdfFormFieldsTabOrder, PdfFillMode, PdfTextDirection, _PdfWordWrapType, _SignatureFlag } from './../enumerator';
import { _PdfBaseStream } from './../base-stream';
import { PdfTemplate } from './../graphics/pdf-template';
import { PdfStringFormat, PdfVerticalAlignment } from './../fonts/pdf-string-format';
import { PdfGraphics, _TextRenderingMode, PdfBrush, PdfPen } from './../graphics/pdf-graphics';
import { PdfFontFamily, PdfStandardFont, PdfFont, PdfFontStyle, PdfTrueTypeFont } from './../fonts/pdf-standard-font';
import { PdfAppearance } from './../annotations/pdf-appearance';
import { PdfPath } from './../graphics/pdf-path';
import { PdfAnnotationCollection } from '../annotations/annotation-collection';
import { PdfFieldActions } from '../pdf-action';
/**
* `PdfField` class represents the base class for form field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the count of the loaded field items
* let count: number = field.itemsCount;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfField = /** @class */ (function () {
function PdfField() {
/**
* Enables grouping behavior across related widgets.
*
* @private
*/
this._enableGrouping = false;
/**
* Indicates whether the field resides on a duplicated page.
*
* @private
*/
this._isDuplicatePage = false;
/**
* Indicates whether the field is visible.
*
* @private
*/
this._visible = true;
/**
* Indicates transparent background for the field.
*
* @private
*/
this._isTransparentBackColor = false;
/**
* Indicates transparent border for the field.
*
* @private
*/
this._isTransparentBorderColor = false;
/**
* Default font used for field content.
*
* @private
*/
this._defaultFont = new PdfStandardFont(PdfFontFamily.helvetica, 8);
/**
* Font used for field appearance generation.
*
* @private
*/
this._appearanceFont = new PdfStandardFont(PdfFontFamily.helvetica, 10, PdfFontStyle.regular);
/**
* Default font used for list item text.
*
* @private
*/
this._defaultItemFont = new PdfStandardFont(PdfFontFamily.timesRoman, 12);
/**
* Indicates whether the field should be flattened.
*
* @private
*/
this._flatten = false;
/**
* Font used for circle caption rendering.
*
* @private
*/
this._circleCaptionFont = new PdfStandardFont(PdfFontFamily.helvetica, 8, PdfFontStyle.regular);
/**
* Indicates whether field updates are in progress.
*
* @private
*/
this._isUpdating = false;
/**
* Indicates whether field data is being imported.
*
* @private
*/
this._isImport = false;
/**
* Export value used for checkable fields.
*
* @private
*/
this._exportValue = 'Yes';
}
Object.defineProperty(PdfField.prototype, "itemsCount", {
/**
* Gets the count of the loaded field items (Read only).
*
* @returns {number} Items count.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the count of the loaded field items
* let count: number = field.itemsCount;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._kids ? this._kids.length : 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "form", {
/**
* Gets the form object of the field (Read only).
*
* @returns {PdfForm} Form.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the form object of the field
* let form: PdfForm = field.form;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._form;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "name", {
/**
* Gets the name of the field (Read only).
*
* @returns {string} Field name.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the name of the field
* let name: string = field.name;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._name === 'undefined') {
var names = _getInheritableProperty(this._dictionary, 'T', false, false, 'Parent');
if (names && names.length > 0) {
if (names.length === 1) {
this._name = names[0];
}
else {
this._name = names.slice().reverse().join('.');
}
}
}
return this._name;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "actualName", {
/**
* Gets the actual name of the field (Read only).
*
* @private
* @returns {string} Actual name.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the actual name of the field
* let name: string = field.actualName;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._actualName === 'undefined' && this._dictionary && this._dictionary.has('T')) {
var name_1 = this._dictionary.get('T');
if (name_1 && typeof name_1 === 'string') {
this._actualName = name_1;
}
}
return this._actualName;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "mappingName", {
/**
* Gets the mapping name to be used when exporting interactive form field data from the document.
*
* @returns {string} Mapping name.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the mapping name of the field
* let name: string = field.mappingName;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._mappingName === 'undefined' && this._dictionary.has('TM')) {
var name_2 = this._dictionary.get('TM');
if (name_2 && typeof name_2 === 'string') {
this._mappingName = name_2;
}
}
return this._mappingName;
},
/**
* Sets the mapping name to be used when exporting interactive form field data from the document.
*
* @param {string} value Mapping name.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the mapping name of the field
* field.mappingName = 'Author';
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (typeof this.mappingName === 'undefined' || this._mappingName !== value) {
this._mappingName = value;
this._dictionary.update('TM', value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "toolTip", {
/**
* Gets the tool tip of the form field.
*
* @returns {string} Tooltip.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the tool tip value of the field
* let toolTip: string = field.toolTip;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._alternateName === 'undefined' && this._dictionary && this._dictionary.has('TU')) {
var name_3 = this._dictionary.get('TU');
if (name_3 && typeof name_3 === 'string') {
this._alternateName = name_3;
}
}
return this._alternateName;
},
/**
* Sets the tool tip of the form field.
*
* @param {string} value Tooltip.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the tool tip value of the field
* field.toolTip = 'Author of the document';
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (typeof this.toolTip === 'undefined' || this._alternateName !== value) {
this._alternateName = value;
this._dictionary.update('TU', value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "visibility", {
/**
* Gets the form field visibility.
*
* @returns {PdfFormFieldVisibility} Field visibility option.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the form field visibility.
* let visibility: PdfFormFieldVisibility = field.visibility;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var value;
if (this._isLoaded) {
value = PdfFormFieldVisibility.visible;
var widget = this.itemAt(this._defaultIndex);
var flag = PdfAnnotationFlag.default;
if (widget && widget._hasFlags) {
flag = widget.flags;
}
else if (this._dictionary.has('F')) {
flag = this._dictionary.get('F');
}
else {
return PdfFormFieldVisibility.visibleNotPrintable;
}
var flagValue = 3;
if ((flag & PdfAnnotationFlag.hidden) === PdfAnnotationFlag.hidden) {
flagValue = 0;
}
if ((flag & PdfAnnotationFlag.noView) === PdfAnnotationFlag.noView) {
flagValue = 1;
}
if ((flag & PdfAnnotationFlag.print) !== PdfAnnotationFlag.print) {
flagValue &= 2;
}
switch (flagValue) {
case 0:
value = PdfFormFieldVisibility.hidden;
break;
case 1:
value = PdfFormFieldVisibility.hiddenPrintable;
break;
case 2:
value = PdfFormFieldVisibility.visibleNotPrintable;
break;
case 3:
value = PdfFormFieldVisibility.visible;
break;
}
}
else {
if (typeof this._visibility === 'undefined') {
this._visibility = PdfFormFieldVisibility.visible;
}
value = this._visibility;
}
return value;
},
/**
* Sets the form field visibility.
*
* @param {PdfFormFieldVisibility} value visibility.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the form field visibility.
* field.visibility = PdfFormFieldVisibility.visible;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var widget = this.itemAt(this._defaultIndex);
if (this._isLoaded) {
if (widget && (!widget._hasFlags || this.visibility !== value)) {
_updateVisibility(widget._dictionary, value);
this._dictionary._updated = true;
}
else if (!this._dictionary.has('F') || this.visibility !== value) {
_updateVisibility(this._dictionary, value);
this._dictionary._updated = true;
}
}
else {
if (this.visibility !== value) {
this._visibility = value;
switch (value) {
case PdfFormFieldVisibility.hidden:
widget.flags = PdfAnnotationFlag.hidden;
break;
case PdfFormFieldVisibility.hiddenPrintable:
widget.flags = (PdfAnnotationFlag.noView | PdfAnnotationFlag.print);
break;
case PdfFormFieldVisibility.visible:
widget.flags = PdfAnnotationFlag.print;
break;
case PdfFormFieldVisibility.visibleNotPrintable:
widget.flags = PdfAnnotationFlag.default;
break;
}
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "bounds", {
/**
* Gets the bounds.
*
* @returns {Rectangle} Bounds.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the bounds of list box field.
* let bounds: Rectangle = field.bounds;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var value;
var widget = this.itemAt(this._defaultIndex);
if (widget) {
widget._page = this.page;
}
if (widget && widget.bounds) {
value = widget.bounds;
}
else if (this._dictionary && this._dictionary.has('Rect')) {
value = _calculateBounds(this._dictionary, this.page);
}
if (typeof value === 'undefined' || value === null) {
value = { x: 0, y: 0, width: 0, height: 0 };
}
return value;
},
/**
* Sets the bounds.
*
* @param {Rectangle} value bounds.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the bounds.
* field.bounds = {x: 10, y: 10, width: 100, height: 20};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value.x === 0 && value.y === 0 && value.width === 0 && value.height === 0) {
throw new Error('Cannot set empty bounds');
}
var widget = this.itemAt(this._defaultIndex);
if (this._isLoaded) {
if (typeof widget === 'undefined' || this._dictionary.has('Rect')) {
this._dictionary.update('Rect', _getUpdatedBounds([value.x, value.y, value.width, value.height], this.page));
}
else {
widget._page = this.page;
widget.bounds = value;
}
}
else {
widget._page = this.page;
widget.bounds = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "rotate", {
/**
* Gets the rotation angle of the field.
*
* @returns {number} angle.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the rotation angle of the form field.
* let rotate: number = field.rotate;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var widget = this.itemAt(this._defaultIndex);
var angle;
if (widget && typeof widget.rotate !== 'undefined') {
angle = widget.rotate;
}
else if (this._mkDictionary && this._mkDictionary.has('R')) {
angle = this._mkDictionary.get('R');
}
else if (this._dictionary.has('R')) {
angle = this._dictionary.get('R');
}
else {
for (var i = 0; i < this._kidsCount && typeof angle === 'undefined'; i++) {
if (i !== this._defaultIndex) {
widget = this.itemAt(i);
if (widget && typeof widget.rotate !== 'undefined') {
angle = widget.rotate;
}
}
}
}
if (typeof angle === 'undefined') {
angle = 0;
}
return angle;
},
/**
* Sets the rotation angle of the field.
*
* @param {number} value rotation angle.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the rotation angle.
* field.rotate = 90;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var widget = this.itemAt(this._defaultIndex);
if (widget) {
widget.rotate = value;
}
else if (!this._dictionary.has('R') || this._dictionary.get('R') !== value) {
this._dictionary.update('R', value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "color", {
/**
* Gets the fore color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the fore color of the field.
* let color: PdfColor = field.color;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var value = { r: 0, g: 0, b: 0 };
var widget = this.itemAt(this._defaultIndex);
if (widget && widget.color) {
value = widget.color;
}
else if (this._defaultAppearance) {
value = this._da.color;
}
return value;
},
/**
* Sets the fore color of the field.
*
* @param {PdfColor} value R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the fore color of the field.
* field.color = {r: 255, g: 0, b: 0};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var widget = this.itemAt(this._defaultIndex);
if (widget && widget.color && _isNullOrUndefined(value)) {
widget.color = value;
}
else {
var isNew = false;
if (!this._defaultAppearance) {
this._da = new _PdfDefaultAppearance('');
isNew = true;
}
if (isNew || this._da.color !== value) {
this._da.color = value;
this._dictionary.update('DA', this._da.toString());
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "backColor", {
/**
* Gets the background color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the background color of the field.
* let backColor: PdfColor = field.backColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBackColor(false);
},
/**
* Sets the background color of the field.
*
* @param {PdfColor} value R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the background color of the field.
* field.backColor = {r: 255, g: 0, b: 0};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBackColor(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "borderColor", {
/**
* Gets the border color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the border color of the field.
* let borderColor: PdfColor = field.borderColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBorderColor(true);
},
/**
* Sets the border color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the border color of the field.
* field.borderColor = {r: 255, g: 0, b: 0};
* // Sets the transparent border color of the field.
* field.borderColor = {r: 255, g: 255, b: 255, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBorderColor(value, true);
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "readOnly", {
/**
* Gets a value indicating whether read only.
*
* @returns {boolean} read only or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets a value indicating whether read only.
* let readOnly: boolean = field.readOnly;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return (this._fieldFlags & _FieldFlag.readOnly) !== 0;
},
/**
* Sets a value indicating whether read only.
*
* @param {boolean} value read only or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets a value indicating whether read only.
* field.readOnly = true;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags |= _FieldFlag.readOnly;
}
else {
if (this._fieldFlags === _FieldFlag.readOnly) {
this._fieldFlags |= _FieldFlag.default;
}
this._fieldFlags &= ~_FieldFlag.readOnly;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "required", {
/**
* Gets a value indicating whether the field is required.
*
* @returns {boolean} required or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets a value indicating whether the field is required.
* let required: boolean = field.required;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return (this._fieldFlags & _FieldFlag.required) !== 0;
},
/**
* Sets a value indicating whether the field is required.
*
* @param {boolean} value required or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets a value indicating whether the field is required.
* field.required = true;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags |= _FieldFlag.required;
}
else {
this._fieldFlags &= ~_FieldFlag.required;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "visible", {
/**
* Gets a value indicating the visibility of the field (Read only).
*
* @returns {boolean} visible or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets a value indicating the visibility of the field.
* let visible: boolean = field.visible;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded) {
var widget = this.itemAt(this._defaultIndex);
var flag = PdfAnnotationFlag.default;
if (widget && widget._hasFlags) {
flag = widget.flags;
}
else if (this._dictionary.has('F')) {
flag = this._dictionary.get('F');
}
return flag !== PdfAnnotationFlag.hidden;
}
else {
return this._visible;
}
},
/**
* Sets a value indicating the visibility of the field.
* Only applicable for newly created PDF form fields.
*
* @param {boolean} value or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets a value indicating the visibility of the field
* field.visible = true;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (!this._isLoaded && this._visible !== value && !value) {
this._visible = value;
this.itemAt(this._defaultIndex).flags = PdfAnnotationFlag.hidden;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "border", {
/**
* Gets the width, style and dash of the border of the field.
*
* @returns {PdfInteractiveBorder} Border properties.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the width, style and dash of the border of the field.
* let border: PdfInteractiveBorder = field.border;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var widget = this.itemAt(this._defaultIndex);
var value;
if (widget && widget._dictionary.has('BS')) {
value = widget.border;
}
else {
value = new PdfInteractiveBorder({ style: PdfBorderStyle.solid });
if (!(this instanceof PdfButtonField)) {
value._width = 0;
}
value._dictionary = this._dictionary;
if (this._dictionary !== null && typeof this._dictionary !== 'undefined' && this._dictionary.has('BS')) {
var border = this._dictionary.get('BS');
if (border) {
if (border.has('W')) {
value._width = border.get('W');
}
if (border.has('S')) {
var borderStyle = border.get('S');
if (borderStyle) {
switch (borderStyle.name) {
case 'D':
value._style = PdfBorderStyle.dashed;
break;
case 'B':
value._style = PdfBorderStyle.beveled;
break;
case 'I':
value._style = PdfBorderStyle.inset;
break;
case 'U':
value._style = PdfBorderStyle.underline;
break;
default:
value._style = PdfBorderStyle.solid;
break;
}
}
}
if (border.has('D')) {
value._dash = border.getArray('D');
}
}
}
}
return value;
},
/**
* Sets the width, style and dash of the border of the field.
*
* @param {PdfInteractiveBorder} value Border properties.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the width, style and dash of the border of the field.
* field.border = new PdfInteractiveBorder({width: 2, style: PdfBorderStyle.solid});
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var widget = this.itemAt(this._defaultIndex);
if (widget) {
widget.border.width = value.width;
widget.border.style = value.style;
this._updateBorder(widget._dictionary, value);
}
else {
this._updateBorder(this._dictionary, value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "rotationAngle", {
/**
* Gets the rotation of the field (Read only).
*
* @returns {PdfRotationAngle} Rotation angle.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the rotation of the field.
* let rotate: PdfRotationAngle = field.rotationAngle;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var widget = this.itemAt(this._defaultIndex);
var mkDictionary;
if (widget) {
mkDictionary = widget._dictionary.get('MK');
}
else {
mkDictionary = this._mkDictionary;
}
if (mkDictionary && mkDictionary.has('R')) {
var rotationValue = mkDictionary.get('R');
if (rotationValue !== undefined) {
switch (rotationValue) {
case 90:
return PdfRotationAngle.angle90;
case 180:
return PdfRotationAngle.angle180;
case 270:
return PdfRotationAngle.angle270;
default:
return PdfRotationAngle.angle0;
}
}
}
if (!widget) {
return PdfRotationAngle.angle0;
}
return widget.rotationAngle;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "export", {
/**
* Gets a value indicating whether the field is allow to export data or not.
*
* @returns {boolean} Allow to export data or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets a value indicating whether the field is allow to export data or not.
* let export: boolean = field.export;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return !((this._fieldFlags & _FieldFlag.noExport) !== 0);
},
/**
* Sets a value indicating whether the field is allow to export data or not.
*
* @param {boolean} value Allow to export data or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets a value indicating whether the field is allow to export data or not.
* field.export = true;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags &= ~_FieldFlag.noExport;
}
else {
this._fieldFlags |= _FieldFlag.noExport;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "tabIndex", {
/**
* Gets the tab index of annotation in current page.
*
* @returns {number} tab index.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the tab index of annotation in current page.
* let tabIndex: number = field.tabIndex;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded) {
var annots = void 0;
if (this.page._pageDictionary.has('Annots')) {
annots = this.page._pageDictionary.get('Annots');
}
if (this._kids && this._kids.length > 0) {
for (var _i = 0, _a = this._kids; _i < _a.length; _i++) {
var reference = _a[_i];
if (reference) {
if (this.page._pageDictionary.has('Annots')) {
if (annots) {
var index1 = annots.indexOf(reference);
if (index1 !== -1) {
return index1;
}
}
}
}
}
}
else if (this._dictionary && this._dictionary.has('Subtype') && this._dictionary.get('Subtype').name === 'Widget') {
if (this._ref) {
if (annots) {
var index1 = annots.indexOf(this._ref);
if (index1 !== -1) {
return index1;
}
}
}
}
return -1;
}
else {
return this._tabIndex;
}
},
/**
* Sets the tab index of a annotation in the current page.
*
* @param {number} value index.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the tab index of annotation in current page.
* field.tabIndex = 5;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._tabIndex = value;
if (this._isLoaded) {
var page = this.page;
if (page &&
(page.tabOrder === PdfFormFieldsTabOrder.manual ||
(this.form && this.form._tabOrder === PdfFormFieldsTabOrder.manual))) {
if (page._pageDictionary.has('Annots')) {
var annots = page._pageDictionary.get('Annots');
var annotationCollection = new PdfAnnotationCollection(annots, this._crossReference, page);
page._annotations = annotationCollection;
var index = annots.indexOf(this._ref);
if (index < 0) {
index = this._annotationIndex;
}
var annotations = page.annotations._reArrange(this._ref, this._tabIndex, index);
page._pageDictionary.update('Annots', annotations);
page._pageDictionary._updated = true;
}
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "page", {
/**
* Gets the page object of the form field (Read only).
*
* @returns {PdfPage} Page object.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the page object of the form field.
* let page: PdfPage = field.page;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (!this._page) {
var widget = this.itemAt(this._defaultIndex);
var dictionary = (typeof widget !== 'undefined') ? widget._dictionary : this._dictionary;
var document_1;
if (this._crossReference) {
document_1 = this._crossReference._document;
}
var page = void 0;
if (dictionary && dictionary.has('P')) {
var ref = dictionary.getRaw('P');
if (ref && document_1) {
for (var i = 0; i < document_1.pageCount; i++) {
var entry = document_1.getPage(i);
if (entry && entry._ref === ref) {
page = entry;
break;
}
}
}
}
if (!page && document_1) {
var widgetRef = (typeof widget !== 'undefined') ? widget._ref : this._ref;
if (!page && widgetRef) {
page = _findPage(document_1, widgetRef);
}
if (!page && this._kids && this._kids.length > 0) {
for (var i = 0; i < this._kids.length; i++) {
page = _findPage(document_1, this._kids[i]);
if (page) {
break;
}
}
}
}
this._page = page;
}
return this._page;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "flatten", {
/**
* Gets the boolean flag indicating whether the form field have been flattened or not.
*
* @returns {boolean} Flatten.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Get the first field
* let field: PdfField = document.form.fieldAt(0);
* // Gets the boolean flag indicating whether the form field have been flattened or not.
* let flatten: boolean = field.flatten;
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._flatten;
},
/**
* Sets the boolean flag indicating whether the form field have been flattened or not.
*
* @param {boolean} value Flatten.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Get the first field
* let field: PdfField = document.form.fieldAt(0);
* // Sets the boolean flag indicating whether the form field have been flattened or not.
* field.flatten = true;
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._flatten = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_grayBrush", {
/**
* Gets a cached gray brush or creates one when needed.
*
* @private
* @returns {PdfBrush} returns the pdfbrush
*/
get: function () {
if (!this._gray) {
this._gray = new PdfBrush({ r: 128, g: 128, b: 128 });
}
return this._gray;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_silverBrush", {
/**
* Gets a cached silver brush or creates one when needed.
*
* @private
* @returns {PdfBrush} returns the pdfbrush
*/
get: function () {
if (!this._silver) {
this._silver = new PdfBrush({ r: 198, g: 198, b: 198 });
}
return this._silver;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_whiteBrush", {
/**
* Gets a cached white brush or creates one when needed.
*
* @private
* @returns {PdfBrush} returns the pdfbrush
*/
get: function () {
if (!this._white) {
this._white = new PdfBrush({ r: 255, g: 255, b: 255 });
}
return this._white;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_blackBrush", {
/**
* Gets a cached black brush or creates one when needed.
*
* @private
* @returns {PdfBrush} returns the pdfbrush
*/
get: function () {
if (!this._black) {
this._black = new PdfBrush({ r: 0, g: 0, b: 0 });
}
return this._black;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_kidsCount", {
/**
* Gets the number of child items associated with the annotation.
*
* @private
* @returns {number} returns the kids count.
*/
get: function () {
return this._kids ? this._kids.length : 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_hasBackColor", {
/**
* Indicates whether a background color is defined in the annotation or its appearance dictionary.
*
* @private
* @returns {boolean} `true` if a background color is defined; otherwise, `false`.
*/
get: function () {
if (this._isLoaded) {
var mkDictionary = this._mkDictionary;
if (!mkDictionary) {
var item = this.itemAt(this._defaultIndex);
if (item && item._dictionary.has('MK')) {
mkDictionary = item._dictionary.get('MK');
}
}
return (mkDictionary && mkDictionary.has('BG'));
}
else {
return !this._isTransparentBackColor;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_hasBorderColor", {
/**
* Indicates whether a border color is defined in the annotation or its appearance dictionary.
*
* @private
* @returns {boolean} `true` if a border color is defined; otherwise, `false`.
*/
get: function () {
if (this._isLoaded) {
var mkDictionary = this._mkDictionary;
if (!mkDictionary) {
var item = this.itemAt(this._defaultIndex);
if (item && item._dictionary.has('MK')) {
mkDictionary = item._dictionary.get('MK');
}
}
return (mkDictionary && mkDictionary.has('BC'));
}
else {
return !this._isTransparentBorderColor;
}
},
enumerable: true,
configurable: true
});
/**
* Determines the effective background color using widget and appearance dictionaries.
*
* @private
* @param {boolean} hasTransparency is true if it has parsed backcolor.
* @returns {PdfColor} of the background.
*/
PdfField.prototype._parseBackColor = function (hasTransparency) {
var value;
if ((!hasTransparency) || ((this._isLoaded && this._hasBackColor) || (!this._isLoaded && !this._isTransparentBackColor))) {
var widget = this.itemAt(this._defaultIndex);
if (widget && widget.backColor) {
value = widget.backColor;
}
else if (this._mkDictionary) {
var mkDict = this._mkDictionary;
if (mkDict && mkDict.has('BG')) {
var bgArray = mkDict.getArray('BG');
if (bgArray) {
value = _parseColor(bgArray);
}
}
}
if (typeof value === 'undefined' || value === null) {
value = { r: 255, g: 255, b: 255 };
}
}
return value;
};
/**
* Determines the effective border color using widget and appearance dictionaries.
*
* @private
* @param {boolean} hasTransparency - It returns true if transparency is present.
* @returns {PdfColor} returns the pdfcolor.
*/
PdfField.prototype._parseBorderColor = function (hasTransparency) {
var value;
if ((!hasTransparency) || ((this._isLoaded && this._hasBorderColor) || (!this._isLoaded && !this._isTransparentBorderColor))) {
var widget = this.itemAt(this._defaultIndex);
if (widget && widget.borderColor) {
value = widget.borderColor;
}
else if (this._mkDictionary) {
var mkDict = this._mkDictionary;
if (mkDict.has('BC')) {
var bgArray = mkDict.getArray('BC');
if (bgArray) {
value = _parseColor(bgArray);
}
}
}
if (value === null || typeof value === 'undefined') {
value = { r: 0, g: 0, b: 0 };
}
}
return value;
};
/**
* Updates the border style width and dash entries in the appearance dictionary.
*
* @private
* @param {PdfColor} value - The background color.
* @param {boolean} hasTransparency - It returns true if transparency is present.
* @returns {void} nothing.
*/
PdfField.prototype._updateBackColor = function (value, hasTransparency) {
if (hasTransparency === void 0) { hasTransparency = false; }
if (hasTransparency && _isNullOrUndefined(value) && value.isTransparent) {
this._isTransparentBackColor = true;
if (this._dictionary && this._dictionary.has('BG')) {
delete this._dictionary._map.BG;
}
var mkDictionary = this._mkDictionary;
if (mkDictionary && mkDictionary.has('BG')) {
delete mkDictionary._map.BG;
this._dictionary._updated = true;
}
var item = this.itemAt(this._defaultIndex);
if (item) {
item.backColor = value;
}
}
else {
this._isTransparentBackColor = false;
var widget = this.itemAt(this._defaultIndex);
if (widget && widget.backColor !== value) {
widget.backColor = value;
}
else {
var mkDictionary = this._mkDictionary;
if (typeof mkDictionary === 'undefined') {
var dictionary = new _PdfDictionary(this._crossReference);
dictionary.update('BG', [Number.parseFloat((value.r / 255).toFixed(3)),
Number.parseFloat((value.g / 255).toFixed(3)),
Number.parseFloat((value.b / 255).toFixed(3))]);
this._dictionary.update('MK', dictionary);
}
else if (!mkDictionary.has('BG') || _parseColor(mkDictionary.getArray('BG')) !== value) {
mkDictionary.update('BG', [Number.parseFloat((value.r / 255).toFixed(3)),
Number.parseFloat((value.g / 255).toFixed(3)),
Number.parseFloat((value.b / 255).toFixed(3))]);
this._dictionary._updated = true;
}
}
}
};
/**
* Updates the annotation border color handling transparency and appearance entries.
*
* @private
* @param {PdfColor} value - The border color to apply (RGB; may include `isTransparent`).
* @param {boolean} [hasTransparency=false] - When `true`, treats `value.isTransparent` as a request to clear border color.
* @returns {void} nothing.
*/
PdfField.prototype._updateBorderColor = function (value, hasTransparency) {
if (hasTransparency === void 0) { hasTransparency = false; }
if (hasTransparency && value.isTransparent) {
this._isTransparentBorderColor = true;
if (this._dictionary.has('BC')) {
delete this._dictionary._map.BC;
}
var mkDictionary = this._mkDictionary;
if (mkDictionary && mkDictionary.has('BC')) {
delete mkDictionary._map.BC;
if (this._dictionary.has('BS')) {
var bsDictionary = this._dictionary.get('BS');
if (bsDictionary && bsDictionary.has('W')) {
delete bsDictionary._map.W;
}
}
this._dictionary._updated = true;
}
var item = this.itemAt(this._defaultIndex);
if (item) {
item.borderColor = value;
}
}
else {
this._isTransparentBorderColor = false;
var widget = this.itemAt(this._defaultIndex);
if (widget && widget.borderColor !== value) {
widget.borderColor = value;
}
else {
var mkDictionary = this._mkDictionary;
if (typeof mkDictionary === 'undefined') {
var dictionary = new _PdfDictionary(this._crossReference);
dictionary.update('BC', [Number.parseFloat((value.r / 255).toFixed(3)),
Number.parseFloat((value.g / 255).toFixed(3)),
Number.parseFloat((value.b / 255).toFixed(3))]);
this._dictionary.update('MK', dictionary);
}
else if (!mkDictionary.has('BC') || _parseColor(mkDictionary.getArray('BC')) !== value) {
mkDictionary.update('BC', [Number.parseFloat((value.r / 255).toFixed(3)),
Number.parseFloat((value.g / 255).toFixed(3)),
Number.parseFloat((value.b / 255).toFixed(3))]);
this._dictionary._updated = true;
}
}
}
};
/**
* Gets the field item as `PdfWidgetAnnotation` at the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the loaded form field
* let field: PdfField = document.form.fieldAt(0);
* // Access the count of the field items.
* let count: number = field.count;
* // Access the first item
* let item: PdfWidgetAnnotation = field.itemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Item index.
* @returns {PdfWidgetAnnotation} Loaded PDF form field item at the specified index.
*/
PdfField.prototype.itemAt = function (index) {
var item;
if (index >= 0 && index < this._kidsCount) {
if (this._parsedItems.has(index)) {
item = this._parsedItems.get(index);
}
else {
var dictionary = void 0;
var reference = this._kids[index];
if (reference && reference instanceof _PdfReference) {
dictionary = this._crossReference._fetch(reference);
}
if (dictionary) {
item = PdfWidgetAnnotation._load(dictionary, this._crossReference);
item._ref = reference;
this._parsedItems.set(index, item);
}
}
}
return item;
};
/**
* Sets the flag to indicate the new appearance creation.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Set boolean flag to create a new appearance stream for form fields.
* document.form.fieldAt(0).setAppearance(true);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {boolean} value Set appearance.
* @returns {void} Nothing.
*/
PdfField.prototype.setAppearance = function (value) {
this._setAppearance = value;
};
/**
* Gets the value associated with the specified key.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Gets the value associated with the key 'Author'.
* let value: string = document.form.fieldAt(0).getValue('Author');
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {string} name Key.
* @returns {string} Value associated with the key.
*/
PdfField.prototype.getValue = function (name) {
var value;
if (this._dictionary && this._dictionary.has(name)) {
var element = this._dictionary.get(name); // eslint-disable-line
if (element !== null && typeof element !== 'undefined' && element instanceof _PdfName) {
value = element.name;
}
else if (typeof element === 'string') {
value = element;
}
else {
throw new Error('PdfException: ' + name + ' is not found');
}
}
else {
throw new Error('PdfException: ' + name + ' is not found');
}
return value;
};
/**
* Sets the value associated with the specified key.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Set custom value
* field.setValue('Author', 'John');
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {string} name Key.
* @param {string} value Value associated with the key..
* @returns {void} Nothing.
*/
PdfField.prototype.setValue = function (name, value) {
if (name && name !== '' && value && value !== '') {
this._dictionary.update(name, value);
}
};
/**
* Remove the form field item from the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Remove the first item of the form field
* field.removeItemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Item index to remove.
* @returns {void} Nothing.
*/
PdfField.prototype.removeItemAt = function (index) {
if (this._dictionary !== null && typeof this._dictionary !== 'undefined' && this._dictionary.has('Kids') && this.itemsCount > 0) {
var item = this.itemAt(index);
if (item && item._ref) {
var page = item._getPage();
if (page) {
page._removeAnnotation(item._ref);
}
this._kids.splice(index, 1);
this._dictionary.set('Kids', this._kids);
this._dictionary._updated = true;
this._parsedItems.delete(index);
if (this._parsedItems.size > 0) {
var parsedItems_1 = new Map();
this._parsedItems.forEach(function (value, key) {
if (key > index) {
parsedItems_1.set(key - 1, value);
}
else {
parsedItems_1.set(key, value);
}
});
this._parsedItems = parsedItems_1;
}
}
}
};
/**
* Remove the specified form field item.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Remove the first item of the form field
* field.removeItem(field.itemAt(0));
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {PdfWidgetAnnotation} item Item to remove.
* @returns {void} Nothing.
*/
PdfField.prototype.removeItem = function (item) {
if (item && item._ref) {
var index = this._kids.indexOf(item._ref);
if (index !== -1) {
this.removeItemAt(index);
}
}
};
Object.defineProperty(PdfField.prototype, "_fieldFlags", {
/**
* Gets the resolved field flag value using inheritable properties with default fallback.
*
* @private
* @returns {_FieldFlag} The resolved field flags.
*/
get: function () {
if (typeof this._flags === 'undefined') {
this._flags = _getInheritableProperty(this._dictionary, 'Ff', false, true, 'Parent');
if (typeof this._flags === 'undefined') {
this._flags = _FieldFlag.default;
}
}
return this._flags;
},
/**
* Sets the field flag value and updates the field dictionary.
*
* @private
* @param {_FieldFlag} value - The new field flags to apply.
* @returns {void} nothing.
*/
set: function (value) {
if (this._fieldFlags !== value) {
this._flags = value;
this._dictionary.update('Ff', value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_defaultAppearance", {
/**
* Gets the default appearance by reading the inheritable appearance string.
*
* @private
* @returns {_PdfDefaultAppearance} The default appearance if defined.
*/
get: function () {
if (typeof this._da === 'undefined') {
var da = _getInheritableProperty(this._dictionary, 'DA', false, true, 'Parent');
if (da && da !== '') {
this._da = new _PdfDefaultAppearance(da);
}
}
return this._da;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfField.prototype, "_mkDictionary", {
/**
* Gets the appearance characteristics dictionary for the widget.
*
* @private
* @returns {_PdfDictionary} The appearance characteristics (MK) dictionary.
*/
get: function () {
var value;
if (this._dictionary && this._dictionary.has('MK')) {
value = this._dictionary.get('MK');
}
return value;
},
enumerable: true,
configurable: true
});
/**
* Updates the border style width and dash entries in the appearance dictionary.
*
* @private
* @param {_PdfDictionary} dictionary - The dictionary to update (usually widget or field dictionary).
* @param {PdfInteractiveBorder} value - The border settings to apply (width, style, dash).
* @returns {void} nothing.
*/
PdfField.prototype._updateBorder = function (dictionary, value) {
var bs;
var isNew = false;
if (dictionary && dictionary.has('BS')) {
bs = dictionary.get('BS');
}
else {
bs = new _PdfDictionary(this._crossReference);
dictionary.update('BS', bs);
isNew = true;
}
if (typeof value.width !== 'undefined') {
bs.update('W', value.width);
dictionary._updated = true;
}
else if (isNew) {
bs.update('W', 0);
}
if (typeof value.style !== 'undefined') {
bs.update('S', _mapBorderStyle(value.style));
dictionary._updated = true;
}
else if (isNew) {
bs.update('S', _mapBorderStyle(PdfBorderStyle.solid));
}
if (typeof value.dash !== 'undefined') {
bs.update('D', value.dash);
dictionary._updated = true;
}
};
/**
* Returns true when the annotation flags indicate print and no view state.
*
* @private
* @param {_PdfDictionary} dictionary - The annotation/field dictionary.
* @returns {boolean} `true` if print & no-view flags are set; otherwise, `false`.
*/
PdfField.prototype._checkFieldFlag = function (dictionary) {
var flag;
if (dictionary && dictionary instanceof _PdfDictionary) {
flag = dictionary.get('F');
}
return (typeof flag !== 'undefined' && flag === 6);
};
/**
* Registers the font resource and populates default appearance entries.
*
* @private
* @param {PdfFont} font - The font to register and use for appearances.
* @returns {void} nothing.
*/
PdfField.prototype._initializeFont = function (font) {
var _this = this;
this._font = font;
var document = this._crossReference._document;
var resource;
if (document) {
if (document.form._dictionary.has('DR')) {
resource = document.form._dictionary.get('DR');
}
else {
resource = new _PdfDictionary(this._crossReference);
}
}
var fontDict;
var isReference = false;
if (resource && resource.has('Font')) {
var obj = resource.getRaw('Font'); // eslint-disable-line
if (obj && obj instanceof _PdfReference) {
isReference = true;
fontDict = this._crossReference._fetch(obj);
}
else if (obj instanceof _PdfDictionary) {
fontDict = obj;
}
}
if (!fontDict) {
fontDict = new _PdfDictionary(this._crossReference);
resource.update('Font', fontDict);
}
var keyName;
var reference;
var hasFont = false;
if (this._font && (this._font._key !== null && typeof this._font._key !== 'undefined') && this._font._reference) {
keyName = _PdfName.get(this._font._key);
reference = this._font._reference;
hasFont = true;
}
else {
keyName = _PdfName.get(_getNewGuidString());
reference = this._crossReference._getNextReference();
if (this._font) {
this._font._key = keyName.name;
this._font._reference = reference;
}
}
if (reference && !hasFont) {
if (font instanceof PdfTrueTypeFont) {
if (this._font._pdfFontInternals) {
this._crossReference._cacheMap.set(reference, this._font._pdfFontInternals);
this._font._reference = reference;
}
}
else if (this._font._dictionary) {
this._crossReference._cacheMap.set(reference, this._font._dictionary);
fontDict.update(keyName.name, reference);
resource._updated = true;
document.form._dictionary.update('DR', resource);
document.form._dictionary._updated = true;
}
}
fontDict.update(keyName.name, reference);
resource._updated = true;
document.form._dictionary.update('DR', resource);
document.form._dictionary._updated = true;
this._fontName = keyName.name;
var defaultAppearance = new _PdfDefaultAppearance();
defaultAppearance.fontName = this._fontName;
defaultAppearance.fontSize = this._font._size;
defaultAppearance.color = this.color ? this.color : { r: 0, g: 0, b: 0 };
if (this._dictionary.has('Kids')) {
var widgetDictionary = this._dictionary.getArray('Kids');
widgetDictionary.forEach(function (dictionary, index) {
var widget = _this.itemAt(index);
dictionary.update('DA', defaultAppearance.toString());
if (widget) {
widget._da = defaultAppearance;
}
});
}
else if (this._dictionary.has('Subtype') && this._dictionary.get('Subtype').name === 'Widget') {
this._dictionary.update('DA', defaultAppearance.toString());
}
if (isReference) {
resource._updated = true;
}
};
/**
* Draws a rectangular control background, border and bevel or inset shadows.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {_PaintParameter} parameter - Drawing parameters.
* @returns {void} nothing.
*/
PdfField.prototype._drawRectangularControl = function (g, parameter) {
g.drawRectangle(parameter.bounds, parameter.backBrush);
this._drawBorder(g, parameter.bounds, parameter.borderPen, parameter.borderStyle, parameter.borderWidth);
switch (parameter.borderStyle) {
case PdfBorderStyle.inset:
this._drawLeftTopShadow(g, parameter.bounds, parameter.borderWidth, this._grayBrush);
this._drawRightBottomShadow(g, parameter.bounds, parameter.borderWidth, this._silverBrush);
break;
case PdfBorderStyle.beveled:
this._drawLeftTopShadow(g, parameter.bounds, parameter.borderWidth, this._whiteBrush);
this._drawRightBottomShadow(g, parameter.bounds, parameter.borderWidth, parameter.shadowBrush);
break;
}
};
/**
* Renders the control border using rectangle or underline style.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {Rectangle} bounds - Target bounds.
* @param {PdfPen} borderPen - Border pen.
* @param {PdfBorderStyle} style - Border style.
* @param {number} borderWidth - Border width in points.
* @returns {void} nothing.
*/
PdfField.prototype._drawBorder = function (g, bounds, borderPen, style, borderWidth) {
if (borderPen && borderWidth > 0) {
if (style === PdfBorderStyle.underline) {
g.drawLine(borderPen, { x: bounds.x, y: bounds.x + bounds.height - borderWidth / 2 }, { x: bounds.x + bounds.width, y: bounds.y + bounds.height - borderWidth / 2 });
}
else {
var actual = { x: bounds.x + borderWidth / 2,
y: bounds.y + borderWidth / 2,
width: bounds.width - borderWidth,
height: bounds.height - borderWidth };
g.drawRectangle(actual, borderPen);
}
}
};
/**
* Draws the highlight shadow along the left and top edges of the control.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {Rectangle} bounds - Target bounds.
* @param {number} width - Shadow width in points.
* @param {PdfBrush} brush - Shadow brush.
* @returns {void} nothing.
*/
PdfField.prototype._drawLeftTopShadow = function (g, bounds, width, brush) {
var path = new PdfPath();
var points = [];
points.push({ x: bounds.x + width, y: bounds.y + width });
points.push({ x: bounds.x + width, y: (bounds.y + bounds.height) - width });
points.push({ x: bounds.x + 2 * width, y: (bounds.y + bounds.height) - 2 * width });
points.push({ x: bounds.x + 2 * width, y: bounds.y + 2 * width });
points.push({ x: (bounds.x + bounds.width) - 2 * width, y: bounds.y + 2 * width });
points.push({ x: (bounds.x + bounds.width) - width, y: bounds.y + width });
path.addPolygon(points);
g.drawPath(path, brush);
};
/**
* Draws the shadow along the right and bottom edges of the control.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {Rectangle} bounds - Target bounds.
* @param {number} width - Shadow width in points.
* @param {PdfBrush} brush - Shadow brush.
* @returns {void} nothing.
*/
PdfField.prototype._drawRightBottomShadow = function (g, bounds, width, brush) {
var path = new PdfPath();
var points = [];
points.push({ x: bounds.x + width, y: (bounds.y + bounds.height) - width });
points.push({ x: bounds.x + 2 * width, y: (bounds.y + bounds.height) - 2 * width });
points.push({ x: (bounds.x + bounds.width) - 2 * width, y: (bounds.y + bounds.height) - 2 * width });
points.push({ x: (bounds.x + bounds.width) - 2 * width, y: bounds.y + 2 * width });
points.push({ x: bounds.x + bounds.width - width, y: bounds.y + width });
points.push({ x: (bounds.x + bounds.width) - width, y: (bounds.y + bounds.height) - width });
path.addPolygon(points);
g.drawPath(path, brush);
};
/**
* Paints a radio button appearance including fill, border, shadows and mark.
*
* @private
* @param {PdfGraphics} graphics - Graphics context.
* @param {_PaintParameter} parameter - Drawing parameters.
* @param {string} checkSymbol - Glyph used for the selected state.
* @param {_PdfCheckFieldState} state - Visual state to render.
* @returns {void} nothing.
*/
PdfField.prototype._drawRadioButton = function (graphics, parameter, checkSymbol, state) {
if (checkSymbol === 'l') {
var bounds = parameter.bounds;
var diameter = bounds.width;
if (this._enableGrouping) {
diameter = Math.min(bounds.width, bounds.height);
}
switch (state) {
case _PdfCheckFieldState.checked:
case _PdfCheckFieldState.unchecked:
graphics.drawEllipse({ x: bounds.x, y: bounds.y, width: diameter, height: bounds.height }, parameter.backBrush);
break;
case _PdfCheckFieldState.pressedChecked:
case _PdfCheckFieldState.pressedUnchecked:
if ((parameter.borderStyle === PdfBorderStyle.beveled) || (parameter.borderStyle === PdfBorderStyle.underline)) {
graphics.drawEllipse(bounds, parameter.backBrush);
}
else {
graphics.drawEllipse({ x: bounds.x, y: bounds.y, width: diameter, height: bounds.height }, parameter.shadowBrush);
}
break;
}
this._drawRoundBorder(graphics, bounds, parameter.borderPen, parameter.borderWidth);
this._drawRoundShadow(graphics, parameter, state);
if (state === _PdfCheckFieldState.checked || state === _PdfCheckFieldState.pressedChecked) {
var outward = [bounds.x + parameter.borderWidth / 2,
bounds.y + parameter.borderWidth / 2,
diameter - parameter.borderWidth,
bounds.height - parameter.borderWidth];
graphics.drawEllipse({ x: outward[0] + (outward[2] / 4),
y: outward[1] + (outward[2] / 4),
width: outward[2] - (outward[2] / 2),
height: outward[3] - (outward[2] / 2) }, parameter.foreBrush);
}
}
else {
this._drawCheckBox(graphics, parameter, checkSymbol, state);
}
};
/**
* Draws a circular border sized to the bounds and border width.
*
* @private
* @param {PdfGraphics} graphics - Graphics context.
* @param {Rectangle} bounds - Target bounds.
* @param {PdfPen} borderPen - Border pen.
* @param {number} borderWidth - Border width.
* @returns {void} nothing.
*/
PdfField.prototype._drawRoundBorder = function (graphics, bounds, borderPen, borderWidth) {
if (bounds.x !== 0 || bounds.y !== 0 || bounds.width !== 0 || bounds.height !== 0) {
graphics.drawEllipse({ x: bounds.x + borderWidth / 2, y: bounds.y + borderWidth / 2, width: (this._enableGrouping ?
Math.min(bounds.width, bounds.height) : bounds.width) - borderWidth, height: bounds.height - borderWidth }, borderPen);
}
};
/**
* Draws beveled or inset arc shadows around a circular control.
*
* @private
* @param {PdfGraphics} graphics - Graphics context.
* @param {_PaintParameter} parameter - Drawing parameters.
* @param {_PdfCheckFieldState} state - Visual state to render.
* @returns {void} nothing.
*/
PdfField.prototype._drawRoundShadow = function (graphics, parameter, state) {
var borderWidth = parameter.borderWidth;
var inflateValue = -1.5 * borderWidth;
var x = parameter.bounds.x + inflateValue;
var y = parameter.bounds.y + inflateValue;
var width = parameter.bounds.width + (2 * inflateValue);
var height = parameter.bounds.height + (2 * inflateValue);
var shadowBrush = parameter.shadowBrush;
if (shadowBrush) {
var shadowColor = shadowBrush._color;
var leftTop = void 0;
var rightBottom = void 0;
switch (parameter.borderStyle) {
case PdfBorderStyle.beveled:
switch (state) {
case _PdfCheckFieldState.pressedChecked:
case _PdfCheckFieldState.pressedUnchecked:
leftTop = new PdfPen(shadowColor, borderWidth);
rightBottom = new PdfPen({ r: 255, g: 255, b: 255 }, borderWidth);
break;
case _PdfCheckFieldState.checked:
case _PdfCheckFieldState.unchecked:
leftTop = new PdfPen({ r: 255, g: 255, b: 255 }, borderWidth);
rightBottom = new PdfPen(shadowColor, borderWidth);
break;
}
break;
case PdfBorderStyle.inset:
switch (state) {
case _PdfCheckFieldState.pressedChecked:
case _PdfCheckFieldState.pressedUnchecked:
leftTop = new PdfPen({ r: 0, g: 0, b: 0 }, borderWidth);
rightBottom = new PdfPen({ r: 0, g: 0, b: 0 }, borderWidth);
break;
case _PdfCheckFieldState.checked:
case _PdfCheckFieldState.unchecked:
leftTop = new PdfPen({ r: 128, g: 128, b: 128 }, borderWidth);
rightBottom = new PdfPen({ r: 192, g: 192, b: 192 }, borderWidth);
break;
}
break;
}
if (leftTop && rightBottom) {
graphics.drawArc({ x: x, y: y, width: width, height: height }, 135, 180, leftTop);
graphics.drawArc({ x: x, y: y, width: width, height: height }, -45, 180, rightBottom);
}
}
};
/**
* Paints a checkbox appearance including background, border, shadows and glyph.
*
* @private
* @param {PdfGraphics} graphics - Graphics context.
* @param {_PaintParameter} parameter - Drawing parameters.
* @param {string} checkSymbol - Glyph used for the selected state.
* @param {_PdfCheckFieldState} state - Visual state to render.
* @param {PdfFont} [font] - Optional font to use for glyph drawing.
* @returns {void} nothing.
*/
PdfField.prototype._drawCheckBox = function (graphics, parameter, checkSymbol, state, font) {
switch (state) {
case _PdfCheckFieldState.unchecked:
case _PdfCheckFieldState.checked:
if (parameter.borderPen || parameter.backBrush) {
graphics.drawRectangle(parameter.bounds, parameter.backBrush);
}
break;
case _PdfCheckFieldState.pressedChecked:
case _PdfCheckFieldState.pressedUnchecked:
if ((parameter.borderStyle === PdfBorderStyle.beveled || parameter.backBrush) ||
(parameter.borderStyle === PdfBorderStyle.underline)) {
if (parameter.borderPen || parameter.backBrush) {
graphics.drawRectangle(parameter.bounds, parameter.backBrush);
}
}
else if (parameter.borderPen || parameter.shadowBrush) {
graphics.drawRectangle(parameter.bounds, parameter.shadowBrush);
}
break;
}
var rectangle = parameter.bounds;
this._drawBorder(graphics, parameter.bounds, parameter.borderPen, parameter.borderStyle, parameter.borderWidth);
if ((state === _PdfCheckFieldState.pressedChecked) || (state === _PdfCheckFieldState.pressedUnchecked)) {
switch (parameter.borderStyle) {
case PdfBorderStyle.inset:
this._drawLeftTopShadow(graphics, parameter.bounds, parameter.borderWidth, this._blackBrush);
this._drawRightBottomShadow(graphics, parameter.bounds, parameter.borderWidth, this._whiteBrush);
break;
case PdfBorderStyle.beveled:
this._drawLeftTopShadow(graphics, parameter.bounds, parameter.borderWidth, parameter.shadowBrush);
this._drawRightBottomShadow(graphics, parameter.bounds, parameter.borderWidth, this._whiteBrush);
break;
}
}
else {
switch (parameter.borderStyle) {
case PdfBorderStyle.inset:
this._drawLeftTopShadow(graphics, parameter.bounds, parameter.borderWidth, this._grayBrush);
this._drawRightBottomShadow(graphics, parameter.bounds, parameter.borderWidth, this._silverBrush);
break;
case PdfBorderStyle.beveled:
this._drawLeftTopShadow(graphics, parameter.bounds, parameter.borderWidth, this._whiteBrush);
this._drawRightBottomShadow(graphics, parameter.bounds, parameter.borderWidth, parameter.shadowBrush);
break;
}
}
var yOffset = 0;
var size = 0;
switch (state) {
case _PdfCheckFieldState.pressedChecked:
case _PdfCheckFieldState.checked:
if (!font) {
var extraBorder = parameter.borderStyle === PdfBorderStyle.beveled ||
parameter.borderStyle === PdfBorderStyle.inset;
var borderWidth = parameter.borderWidth;
if (extraBorder) {
borderWidth *= 2;
}
var xPosition = Math.max((extraBorder ? 2 * parameter.borderWidth : parameter.borderWidth), 1);
var xOffset = Math.min(borderWidth, xPosition);
size = (parameter.bounds.width > parameter.bounds.height) ? parameter.bounds.height : parameter.bounds.width;
var fontSize = size - 2 * xOffset;
font = new PdfStandardFont(PdfFontFamily.zapfDingbats, fontSize);
if (parameter.bounds.width > parameter.bounds.height) {
yOffset = ((parameter.bounds.height - font._getHeight()) / 2);
}
}
else {
font = new PdfStandardFont(PdfFontFamily.zapfDingbats, font._size);
}
if (size === 0) {
size = parameter.bounds.height;
}
if (parameter.pageRotationAngle !== PdfRotationAngle.angle0 || parameter.rotationAngle > 0) {
var state_1 = graphics.save();
var size_1 = graphics._size;
if (parameter.pageRotationAngle !== PdfRotationAngle.angle0) {
if (parameter.pageRotationAngle === PdfRotationAngle.angle90) {
graphics.translateTransform({ x: size_1.height, y: 0 });
graphics.rotateTransform(90);
var y = size_1.height - (rectangle.x + rectangle.width);
var x = rectangle.y;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
else if (parameter.pageRotationAngle === PdfRotationAngle.angle180) {
graphics.translateTransform({ x: size_1.width, y: size_1.height });
graphics.rotateTransform(-180);
var x = size_1.width - (rectangle.x + rectangle.width);
var y = size_1.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
else if (parameter.pageRotationAngle === PdfRotationAngle.angle270) {
graphics.translateTransform({ x: 0, y: size_1.width });
graphics.rotateTransform(270);
var x = size_1.width - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
}
if (parameter.rotationAngle > 0) {
if (parameter.rotationAngle === 90) {
if (parameter.pageRotationAngle === PdfRotationAngle.angle90) {
graphics.translateTransform({ x: 0, y: size_1.height });
graphics.rotateTransform(-90);
var x = size_1.height - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
else {
if (rectangle.width > rectangle.height) {
graphics.translateTransform({ x: 0, y: size_1.height });
graphics.rotateTransform(-90);
rectangle = parameter.bounds;
}
else {
var z = rectangle.x;
rectangle.x = -(rectangle.y + rectangle.height);
rectangle.y = z;
var height = rectangle.height;
rectangle.height = rectangle.width > font._getHeight() ? rectangle.width : font._getHeight();
rectangle.width = height;
graphics.rotateTransform(-90);
}
}
}
else if (parameter.rotationAngle === 270) {
graphics.translateTransform({ x: size_1.width, y: 0 });
graphics.rotateTransform(-270);
var x = rectangle.y;
var y = size_1.width - (rectangle.x + rectangle.width);
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
else if (parameter.rotationAngle === 180) {
graphics.translateTransform({ x: size_1.width, y: size_1.height });
graphics.rotateTransform(-180);
var x = size_1.width - (rectangle.x + rectangle.width);
var y = size_1.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
graphics.drawString(checkSymbol, font, { x: rectangle.x, y: rectangle.y - yOffset, width: rectangle.width, height: rectangle.height }, null, parameter.foreBrush, new PdfStringFormat(PdfTextAlignment.center, PdfVerticalAlignment.middle));
graphics.restore(state_1);
}
else {
graphics.drawString(checkSymbol, font, { x: rectangle.x, y: rectangle.y - yOffset, width: rectangle.width, height: rectangle.height }, null, parameter.foreBrush, new PdfStringFormat(PdfTextAlignment.center, PdfVerticalAlignment.middle));
}
break;
}
}
};
/**
* Adds the widget to the kids array and updates cached item mappings.
*
* @private
* @param {PdfWidgetAnnotation} item - The widget annotation to add.
* @returns {void} nothing.
*/
PdfField.prototype._addToKid = function (item) {
if (this._dictionary && this._dictionary.has('Kids')) {
this._kids = this._dictionary.get('Kids');
}
else {
this._kids = [];
this._dictionary.update('Kids', this._kids);
this._parsedItems = new Map();
}
if (this._kids.indexOf(item._ref) === -1) {
var currentIndex = this._kidsCount;
item._index = currentIndex;
this._kids.push(item._ref);
this._parsedItems.set(currentIndex, item);
}
};
/* eslint-disable */
/**
* Draws a template on the page respecting page rotation and text mode.
*
* @private
* @param {PdfTemplate} template - The template to draw.
* @param {PdfPage} page - Target page.
* @param {{x: number, y: number, width: number, height: number}} bounds - Destination bounds. // eslint-disable-line
* @returns {void} nothing.
*/
PdfField.prototype._drawTemplate = function (template, page, bounds) {
if (template && page) {
var graphics = page.graphics;
graphics.save();
if (page.rotation === PdfRotationAngle.angle90) {
graphics.translateTransform({ x: graphics._size.height, y: 0 });
graphics.rotateTransform(90);
}
else if (page.rotation === PdfRotationAngle.angle180) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(-180);
}
else if (page.rotation === PdfRotationAngle.angle270) {
graphics.translateTransform({ x: 0, y: graphics._size.width });
graphics.rotateTransform(270);
}
graphics._sw._setTextRenderingMode(_TextRenderingMode.fill);
graphics.drawTemplate(template, bounds);
graphics.restore();
}
};
/**
* Appends a list option to the field and updates the options array entry.
*
* @private
* @param {PdfListFieldItem} item - The list item to append.
* @param {PdfListField} field - The target list field.
* @returns {void} nothing.
*/
PdfField.prototype._addToOptions = function (item, field) {
if (field instanceof PdfListBoxField) {
field._listValues.push(item._text);
}
field._options.push([item._value, item._text]);
field._dictionary.set('Opt', field._options);
field._dictionary._updated = true;
if (!item._isFont && item._pdfFont) {
this._initializeFont(item._pdfFont);
}
};
/**
* Adds or replaces an appearance stream entry for the specified state key.
*
* @private
* @param {_PdfDictionary} dictionary - The widget or field dictionary.
* @param {PdfTemplate} template - The appearance template.
* @param {string} key - The state key (e.g., 'N', 'D', 'R', or a /Yes-like name).
* @returns {void} nothing.
*/
PdfField.prototype._addAppearance = function (dictionary, template, key) {
var appearance = new _PdfDictionary();
if (dictionary && dictionary.has('AP')) {
appearance = dictionary.get('AP');
_removeDuplicateReference(dictionary.get('AP'), this._crossReference, key);
}
else {
appearance = new _PdfDictionary(this._crossReference);
dictionary.update('AP', appearance);
}
var reference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(reference, template._content);
appearance.update(key, reference);
};
/**
* Computes the rotated rectangle for rendering text within a rotated page.
*
* @private
* @param {Rectangle} rect - Original rectangle.
* @param {Size} size - Page size.
* @param {PdfRotationAngle} angle - Page rotation angle.
* @returns {Rectangle} The rotated rectangle.
*/
PdfField.prototype._rotateTextBox = function (rect, size, angle) {
var rectangle = { x: 0, y: 0, width: 0, height: 0 };
if (angle === PdfRotationAngle.angle180) {
rectangle = { x: size.width - (rect.x + rect.width),
y: size.height - (rect.y + rect.height),
width: rect.width,
height: rect.height };
}
else if (angle === PdfRotationAngle.angle270) {
rectangle = { x: rect.y, y: size.width - (rect.x + rect.width), width: rect.height, height: rect.width };
}
else if (angle === PdfRotationAngle.angle90) {
rectangle = { x: size.height - (rect.y + rect.height), y: rect.x, width: rect.height, height: rect.width };
}
return rectangle;
};
/**
* Validates the index is within range and throws when out of bounds.
*
* @private
* @param {number} value - Index to validate.
* @param {number} length - Valid length (upper bound).
* @returns {void} nothing.
* @throws {Error} When the index is out of range.
*/
PdfField.prototype._checkIndex = function (value, length) {
if (value < 0 || (value !== 0 && value >= length)) {
throw Error('Index out of range.');
}
};
/**
* Resolves the current appearance state value from the widget or field.
*
* @private
* @returns {string} The appearance state value, if any.
*/
PdfField.prototype._getAppearanceStateValue = function () {
var value;
if (this._dictionary && this._dictionary.has('Kids')) {
for (var i = 0; i < this._kidsCount; i++) {
var item = this.itemAt(i);
if (item && item._dictionary && item._dictionary.has('AS')) {
var state = item._dictionary.get('AS');
if (state && state.name !== 'Off') {
value = state.name;
break;
}
}
}
}
else if (this._dictionary && this._dictionary.has('AS')) {
var state = this._dictionary.get('AS');
if (state && state.name !== 'Off') {
value = state.name;
}
}
return value;
};
/**
* Gets the text alignment from widget or field dictionaries with default fallback.
*
* @private
* @returns {PdfTextAlignment} The effective text alignment.
*/
PdfField.prototype._getTextAlignment = function () {
if (this._textAlignment === null || typeof this._textAlignment === 'undefined') {
if (this._isLoaded) {
var widget = this.itemAt(this._defaultIndex);
if (widget && widget._dictionary && widget._dictionary.has('Q')) {
this._textAlignment = widget._dictionary.get('Q');
}
else if (this._dictionary.has('Q')) {
this._textAlignment = this._dictionary.get('Q');
}
else {
this._textAlignment = PdfTextAlignment.left;
}
}
else {
this._textAlignment = PdfTextAlignment.left;
}
}
return this._textAlignment;
};
/**
* Sets the text alignment on the widget or field dictionary and updates formatting.
*
* @private
* @param {PdfTextAlignment} value - The alignment to set.
* @returns {void} nothing.
*/
PdfField.prototype._setTextAlignment = function (value) {
var widget = this.itemAt(this._defaultIndex);
if (this._isLoaded && !this.readOnly) {
if (widget && widget._dictionary) {
widget._dictionary.update('Q', value);
}
else {
this._dictionary.update('Q', value);
}
}
if (!this._isLoaded && this._textAlignment !== value) {
if (widget && widget._dictionary) {
widget._dictionary.update('Q', value);
}
else if (this._dictionary) {
this._dictionary.update('Q', value);
}
}
this._textAlignment = value;
this._stringFormat = new PdfStringFormat(value, PdfVerticalAlignment.middle);
};
/**
* Materializes and returns the widget collection for the field.
*
* @private
* @returns {PdfWidgetAnnotation[]} The materialized widget annotations.
*/
PdfField.prototype._parseItems = function () {
var collection = [];
for (var i = 0; i < this.itemsCount; i++) {
collection.push(this.itemAt(i));
}
return collection;
};
return PdfField;
}());
export { PdfField };
/**
* `PdfTextBoxField` class represents the text box field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfTextBoxField = /** @class */ (function (_super) {
__extends(PdfTextBoxField, _super);
function PdfTextBoxField(page, name, bounds, properties) {
var _this = _super.call(this) || this;
/**
* Automatically resizes text to fit the field bounds.
*
* @private
*/
_this._autoResizeText = false;
/**
* Indicates whether the text content has changed.
*
* @private
*/
_this._isTextChanged = false;
if (page && name && bounds) {
_this._initialize(page, name, bounds);
}
if (properties) {
if ('text' in properties && _isNullOrUndefined(properties.text)) {
_this.text = properties.text;
}
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
if ('font' in properties && _isNullOrUndefined(properties.font)) {
_this.font = properties.font;
}
}
return _this;
}
/**
* Parse an existing text box field.
*
* @private
* @param {PdfForm} form Form object.
* @param {_PdfDictionary} dictionary Field dictionary.
* @param {_PdfCrossReference} crossReference Cross reference object.
* @param {_PdfReference} reference Field reference.
* @returns {PdfTextBoxField} Text box field.
*/
PdfTextBoxField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfTextBoxField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
field._defaultIndex = 0;
field._parsedItems = new Map();
return field;
};
Object.defineProperty(PdfTextBoxField.prototype, "actions", {
/**
* Gets the actions of the field. [Read-Only]
*
* @returns {PdfFieldActions} The actions.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access the text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Get the action value from the text box field.
* const PdfFieldActions: PdfFieldActions = field.actions;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (!this._actions) {
this._actions = new PdfFieldActions(this);
}
return this._actions;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "text", {
/**
* Gets the value of the text box field.
*
* @returns {string} Text.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the text value from text box field
* let text: string = field.text;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._text === 'undefined') {
if (this._isLoaded) {
var text = _getInheritableProperty(this._dictionary, 'V', false, true, 'Parent');
if (text) {
this._text = _stringToPdfString(text);
}
else {
var widget = this.itemAt(this._defaultIndex);
if (widget) {
text = widget._dictionary.get('V');
if (text) {
this._text = _stringToPdfString(text);
}
}
}
}
else {
this._text = '';
}
}
return this._text;
},
/**
* Sets the value of the text box field.
*
* @param {string} value Text.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the text value to text box field
* field.text = 'Syncfusion';
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._isLoaded) {
if (!this.readOnly) {
if (!(this._dictionary.has('V') && this._dictionary.get('V') === value)) {
this._dictionary.update('V', value);
}
var widget = this.itemAt(this._defaultIndex);
if (widget && !(widget._dictionary.has('V') && widget._dictionary.get('V') === value)) {
widget._dictionary.update('V', value);
}
this._text = value;
this._isTextChanged = true;
}
}
else if (this._text !== value) {
this._dictionary.update('V', value);
this._text = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "textAlignment", {
/**
* Gets the text alignment in a text box.
*
* @returns {PdfTextAlignment} Text alignment.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the text alignment from text box field
* let alignment: PdfTextAlignment = field.textAlignment;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._getTextAlignment();
},
/**
* Sets the text alignment in a text box.
*
* @param {PdfTextAlignment} value Text alignment.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the text alignment of form field as center
* field.textAlignment = PdfTextAlignment.center;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._textAlignment !== value) {
this._setTextAlignment(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "defaultValue", {
/**
* Gets the default value of the field.
*
* @returns {string} Default value.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the default value from the text box field
* let value: string = field.defaultValue;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._defaultValue === 'undefined') {
var text = _getInheritableProperty(this._dictionary, 'DV', false, true, 'Parent');
if (text) {
this._defaultValue = text;
}
}
return this._defaultValue;
},
/**
* Sets the default value of the field.
*
* @param {string} value Default value.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the default value of the text box field
* field.defaultValue = 'Syncfusion';
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value !== this.defaultValue) {
this._dictionary.update('DV', value);
this._defaultValue = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "multiLine", {
/**
* Gets a value indicating whether this `PdfTextBoxField` is multiline.
*
* @returns {boolean} multiline.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets a value indicating whether this `PdfTextBoxField` is multiline.
* let multiLine: boolean = field.multiLine;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return (this._fieldFlags & _FieldFlag.multiLine) !== 0;
},
/**
* Sets a value indicating whether this `PdfTextBoxField` is multiline.
*
* @param {boolean} value multiLine or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets a value indicating whether this `PdfTextBoxField` is multiline.
* field.multiLine = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags |= _FieldFlag.multiLine;
}
else {
this._fieldFlags &= ~_FieldFlag.multiLine;
}
if (this._stringFormat) {
this._stringFormat.lineAlignment = value ? PdfVerticalAlignment.top : PdfVerticalAlignment.middle;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "password", {
/**
* Gets a value indicating whether this `PdfTextBoxField` is password.
*
* @returns {boolean} password.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets a value indicating whether this `PdfTextBoxField` is password.
* let password: boolean = field.password;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return (this._fieldFlags & _FieldFlag.password) !== 0;
},
/**
* Sets a value indicating whether this `PdfTextBoxField` is password.
*
* @param {boolean} value password or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets a value indicating whether this `PdfTextBoxField` is password.
* field.password = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags |= _FieldFlag.password;
}
else {
this._fieldFlags &= ~_FieldFlag.password;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "scrollable", {
/**
* Gets a value indicating whether this `PdfTextBoxField` is scrollable.
*
* @returns {boolean} scrollable.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets a value indicating whether this `PdfTextBoxField` is scrollable.
* let scrollable: boolean = field.scrollable;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return !((this._fieldFlags & _FieldFlag.doNotScroll) !== 0);
},
/**
* Sets a value indicating whether this `PdfTextBoxField` is scrollable.
*
* @param {boolean} value scrollable or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets a value indicating whether this `PdfTextBoxField` is scrollable.
* field.scrollable = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags &= ~_FieldFlag.doNotScroll;
}
else {
this._fieldFlags |= _FieldFlag.doNotScroll;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "spellCheck", {
/**
* Gets a value indicating whether to check spelling.
*
* @returns {boolean} spellCheck.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets a value indicating whether to check spelling
* let spellCheck: boolean = field.spellCheck;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return !((this._fieldFlags & _FieldFlag.doNotSpellCheck) !== 0);
},
/**
* Sets a value indicating whether to check spelling.
*
* @param {boolean} value spellCheck or not.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets a value indicating whether to check spelling
* field.spellCheck = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags &= ~_FieldFlag.doNotSpellCheck;
}
else {
this._fieldFlags |= _FieldFlag.doNotSpellCheck;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "insertSpaces", {
/**
* Meaningful only if the MaxLength property is set and the Multiline, Password properties are false.
* If set, the field is automatically divided into as many equally spaced positions, or combs,
* as the value of MaxLength, and the text is laid out into those combs.
*
* @returns {boolean} insertSpaces.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets a value indicating whether this `PdfTextBoxField` is insertSpaces.
* let insertSpaces: boolean = field.insertSpaces;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var flags = this._fieldFlags;
return ((_FieldFlag.comb & flags) !== 0) &&
((flags & _FieldFlag.multiLine) === 0) &&
((flags & _FieldFlag.password) === 0) &&
((flags & _FieldFlag.fileSelect) === 0);
},
/**
* Meaningful only if the MaxLength property is set and the Multiline, Password properties are false.
* If set, the field is automatically divided into as many equally spaced positions, or combs,
* as the value of MaxLength, and the text is laid out into those combs.
*
* @param {boolean} value insertSpaces.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets a value indicating whether this `PdfTextBoxField` is insertSpaces.
* field.insertSpaces = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags |= _FieldFlag.comb;
}
else {
this._fieldFlags &= ~_FieldFlag.comb;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "highlightMode", {
/**
* Gets the highlight mode of the field.
*
* @returns {PdfHighlightMode} highlight mode.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the highlight mode of text box field
* let mode: PdfHighlightMode = field.highlightMode;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var widget = this.itemAt(this._defaultIndex);
var mode;
if (widget && typeof widget.highlightMode !== 'undefined') {
mode = widget.highlightMode;
}
else if (this._dictionary && this._dictionary.has('H')) {
var name_4 = this._dictionary.get('H');
mode = _mapHighlightMode(name_4.name);
}
return (typeof mode !== 'undefined') ? mode : PdfHighlightMode.noHighlighting;
},
/**
* Sets the highlight mode of the field.
*
* @param {PdfHighlightMode} value highlight mode.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the highlight mode of text box field as outline
* field.highlightMode = PdfHighlightMode.outline;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var widget = this.itemAt(this._defaultIndex);
if (widget && (typeof widget.highlightMode === 'undefined' || widget.highlightMode !== value)) {
widget.highlightMode = value;
}
else if (!this._dictionary.has('H') || _mapHighlightMode(this._dictionary.get('H')) !== value) {
this._dictionary.update('H', _reverseMapHighlightMode(value));
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "maxLength", {
/**
* Gets the maximum length of the field, in characters.
*
* @returns {number} maximum length.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the maximum length of the field, in characters.
* let maxLength: number = field.maxLength;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (typeof this._maxLength === 'undefined') {
var length_1 = _getInheritableProperty(this._dictionary, 'MaxLen', false, true, 'Parent');
this._maxLength = (typeof length_1 !== 'undefined' && Number.isInteger(length_1)) ? length_1 : 0;
}
return this._maxLength;
},
/**
* Sets the maximum length of the field, in characters.
*
* @param {number} value maximum length.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the maximum length of the field, in characters.
* field.maxLength = 20;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this.maxLength !== value) {
this._dictionary.update('MaxLen', value);
this._maxLength = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "isAutoResizeText", {
/**
* Gets the flag indicating whether the auto resize text enabled or not.
* Note: Applicable only for newly created PDF fields.
*
* @returns {boolean} Enable or disable auto resize text.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the flag indicating whether the auto resize text enabled or not.
* let isAutoResize: boolean = field.isAutoResizeText;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._autoResizeText;
},
/**
* Sets the flag indicating whether the auto resize text enabled or not.
* Note: Applicable only for newly created PDF fields.
*
* @param {boolean} value Enable or disable auto resize text.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the flag indicating whether the auto resize text enabled or not.
* field.isAutoResizeText = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._autoResizeText = value;
var widget = this.itemAt(this._defaultIndex);
if (widget) {
widget._isAutoResize = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "font", {
/**
* Gets the font of the field.
*
* @returns {PdfFont} font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Gets the font of the field.
* let font: PdfFont = field.font;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._font) {
return this._font;
}
else {
var widget = this.itemAt(this._defaultIndex);
this._font = _obtainFontDetails(this._form, widget, this);
}
return this._font;
},
/**
* Sets the font of the field.
*
* @param {PdfFont} value font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfTextBoxField = document.form.fieldAt(0) as PdfTextBoxField;
* // Sets the font of the field
* field.font = document.embedFont(PdfFontFamily.helvetica, 12, PdfFontStyle.bold);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value && value instanceof PdfFont) {
this._font = value;
this._initializeFont(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfTextBoxField.prototype, "backColor", {
/**
* Gets the background color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the background color of the field.
* let backColor: PdfColor = field.backColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBackColor(true);
},
/**
* Sets the background color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the text box field at index 0
* let firstName: PdfField = document.form.fieldAt(0);
* // Sets the background color of the field.
* firstName.backColor = {r: 255, g: 0, b: 0};
* // Access the text box field at index 1
* let secondName: PdfField = document.form.fieldAt(1);
* // Sets the background color of the field to transparent.
* secondName.backColor = {r: 0, g: 0, b: 0, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBackColor(value, true);
},
enumerable: true,
configurable: true
});
/**
* Initializes the field with dictionary entries default values and widget creation.
*
* @private
* @param {PdfPage} page - The page to place the field in.
* @param {string} name - The field name.
* @param {Rectangle} bounds - The field bounds.
* @returns {void} nothing.
*/
PdfTextBoxField.prototype._initialize = function (page, name, bounds) {
this._crossReference = page._crossReference;
this._page = page;
this._name = name;
this._text = '';
this._defaultValue = '';
this._defaultIndex = 0;
this._spellCheck = false;
this._insertSpaces = false;
this._multiline = false;
this._password = false;
this._scrollable = false;
this._dictionary = new _PdfDictionary(this._crossReference);
this._ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(this._ref, this._dictionary);
this._dictionary.objId = this._ref.toString();
this._dictionary.update('FT', _PdfName.get('Tx'));
this._dictionary.update('T', name);
this._fieldFlags |= _FieldFlag.doNotSpellCheck;
this._createItem(bounds);
this._initializeFont(this._defaultFont);
};
/**
* Creates the widget annotation for the field and initializes appearance settings.
*
* @private
* @param {Rectangle} bounds - Widget bounds.
* @returns {void} nothing.
*/
PdfTextBoxField.prototype._createItem = function (bounds) {
var widget = new PdfWidgetAnnotation();
widget._create(this._page, bounds, this);
widget.textAlignment = PdfTextAlignment.left;
this._stringFormat = new PdfStringFormat(widget.textAlignment, PdfVerticalAlignment.middle);
widget._dictionary.update('MK', new _PdfDictionary(this._crossReference));
widget._mkDictionary.update('BC', [0, 0, 0]);
widget._mkDictionary.update('BG', [1, 1, 1]);
widget._mkDictionary.update('CA', this.actualName);
this._addToKid(widget);
};
/**
* Generates appearances or flattens widgets based on settings and field state.
*
* @private
* @param {boolean} [isFlatten=false] - When `true`, flattens the field appearances.
* @returns {void} nothing.
*/
PdfTextBoxField.prototype._doPostProcess = function (isFlatten) {
if (isFlatten === void 0) { isFlatten = false; }
if (isFlatten || this._setAppearance || this._form._setAppearance) {
var count = this._kidsCount;
if (this._isLoaded) {
if (count > 0) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item) {
this._postProcess(isFlatten, item);
}
}
}
else if ((isFlatten || this._form._setAppearance || this._setAppearance) && !this._checkFieldFlag(this._dictionary)) {
this._postProcess(isFlatten);
}
}
else if (isFlatten || this._form._setAppearance || this._setAppearance) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item && !this._checkFieldFlag(item._dictionary)) {
var template = this._createAppearance(isFlatten, item);
if (isFlatten) {
var bounds = { x: item.bounds.x,
y: item.bounds.y,
width: template._size.width,
height: template._size.height };
this._drawTemplate(template, item._page, bounds);
}
else {
this._addAppearance(item._dictionary, template, 'N');
}
item._dictionary._updated = !isFlatten;
}
}
}
if (isFlatten) {
this._dictionary._updated = false;
}
}
};
/**
* Processes a single widget appearance or flattens it onto the page.
*
* @private
* @param {boolean} isFlatten - When `true`, flattens the appearance on page.
* @param {PdfWidgetAnnotation} [widget] - Optional widget to process; defaults to the field.
* @returns {void} nothing.
*/
PdfTextBoxField.prototype._postProcess = function (isFlatten, widget) {
var template;
var bounds;
var source = widget ? widget : this;
if ((widget !== null && typeof widget !== 'undefined' && widget._setAppearance && widget._enableGrouping) || this._form._setAppearance || this._setAppearance || (isFlatten && !source._dictionary.has('AP'))) {
template = this._createAppearance(isFlatten, source);
}
else if (source._dictionary.has('AP')) {
var appearanceStream = void 0;
var dictionary = source._dictionary.get('AP');
if (dictionary && dictionary.has('N')) {
appearanceStream = dictionary.get('N');
var reference = dictionary.getRaw('N');
if (reference) {
appearanceStream.reference = reference;
}
if (appearanceStream) {
template = new PdfTemplate(appearanceStream, this._crossReference);
}
}
}
if (template) {
if (isFlatten) {
var page = source instanceof PdfWidgetAnnotation ? source._getPage() : source.page;
if (page) {
var graphics = page.graphics;
graphics.save();
if (page.rotation === PdfRotationAngle.angle90) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(90);
}
else if (page.rotation === PdfRotationAngle.angle180) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(-180);
}
else if (page.rotation === PdfRotationAngle.angle270) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(270);
}
bounds = { x: source.bounds.x, y: source.bounds.y, width: template._size.width, height: template._size.height };
graphics.drawTemplate(template, bounds);
graphics.restore();
}
source._dictionary._updated = false;
}
else {
this._addAppearance(source._dictionary, template, 'N');
}
}
};
/**
* Builds the appearance template for the widget including background border and text.
*
* @private
* @param {boolean} isFlatten - Whether generating for flattening.
* @param {PdfWidgetAnnotation | PdfTextBoxField} widget - The source widget/field.
* @returns {PdfTemplate} The generated appearance template.
*/
PdfTextBoxField.prototype._createAppearance = function (isFlatten, widget) {
var bounds = widget.bounds;
var isRotated270 = widget.rotate === 270 && this.page.rotation === PdfRotationAngle.angle270;
var width = isRotated270 ? bounds.height : bounds.width;
var height = isRotated270 ? bounds.width : bounds.height;
var template = new PdfTemplate([0, 0, width, height], this._crossReference);
_setMatrix(template, isRotated270 ? widget.rotate : null);
template._writeTransformation = false;
var graphics = template.graphics;
var parameter = new _PaintParameter();
parameter.bounds = { x: 0, y: 0, width: width, height: height };
var backcolor = widget.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(widget.color);
var border = widget.border;
if (widget.borderColor) {
if (border.width === 0) {
widget.borderColor = { r: 255, g: 255, b: 255 };
}
parameter.borderPen = new PdfPen(widget.borderColor, border.width);
_updateDashedBorderStyle(border, parameter);
}
parameter.borderWidth = border.width;
parameter.borderStyle = border.style;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
parameter.rotationAngle = widget.rotate;
parameter.insertSpaces = this.insertSpaces;
var text = this.text;
var pdfFont;
var stringFormat;
var enableGrouping = false;
var action = this.actions;
if (action) {
var format = action.format;
if (format) {
var script = format.script;
var pattern = this._tryParseAcrobatFormFormat(script);
if (pattern) {
text = this._normalizeDateValue(this.text, pattern);
}
}
}
if (text === null || typeof text === 'undefined') {
text = '';
}
if (this.password) {
var password = '';
for (var i = 0; i < text.length; i++) {
password += '*';
}
text = password;
}
if (this.maxLength && text.length > this.maxLength) {
text = text.substring(0, this.maxLength);
}
parameter.required = this.required;
if (!this.required) {
graphics._sw._beginMarkupSequence('Tx');
graphics._initializeCoordinates();
}
if (widget !== null && typeof widget !== 'undefined' && widget instanceof PdfWidgetAnnotation && widget._enableGrouping) {
enableGrouping = true;
}
if (enableGrouping && widget.font !== null && typeof widget.font !== 'undefined') {
pdfFont = widget.font;
}
else if (typeof this._font === 'undefined' || this._font === null) {
this._font = this.font ? this.font : this._defaultFont;
}
if (enableGrouping && widget.textAlignment !== null && typeof widget.textAlignment !== 'undefined') {
stringFormat = stringFormat = new PdfStringFormat(widget.textAlignment, PdfVerticalAlignment.middle);
}
else if (typeof this._stringFormat === 'undefined' || this._stringFormat === null) {
if (typeof this.textAlignment === 'undefined' || this.textAlignment === null) {
this._stringFormat = new PdfStringFormat(this.textAlignment, PdfVerticalAlignment.middle);
}
else {
this._stringFormat = new PdfStringFormat(PdfTextAlignment.left, PdfVerticalAlignment.middle);
}
}
if (_isRightToLeftCharacters(text)) {
this._stringFormat.textDirection = PdfTextDirection.rightToLeft;
}
if (this._isLoaded && !this.multiLine) {
if (this._stringFormat) {
this._stringFormat.lineLimit = false;
}
else if (stringFormat) {
stringFormat.lineLimit = false;
}
}
if (enableGrouping) {
this._drawTextBox(graphics, parameter, text, pdfFont, stringFormat, this.multiLine, this.scrollable, this.maxLength);
}
else {
this._drawTextBox(graphics, parameter, text, this._font, this._stringFormat, this.multiLine, this.scrollable, this.maxLength);
}
if (!this.required) {
graphics._sw._endMarkupSequence();
}
return template;
};
/**
* Normalizes a date value using the acrobat format pattern when possible.
*
* @private
* @param {string} textValue - The input text value.
* @param {string} afFormat - The Acrobat format pattern.
* @returns {string} The normalized value.
*/
PdfTextBoxField.prototype._normalizeDateValue = function (textValue, afFormat) {
var date = this._parseUnknownDate(textValue);
if (date) {
return this._formatDateUsingAcrobatFormat(date, afFormat);
}
else {
return textValue;
}
};
/**
* Attempts to parse an unknown date value using multiple common date patterns.
*
* @private
* @param {string} text - Date text to parse.
* @returns {Date} Parsed date if valid; otherwise `undefined`.
*/
PdfTextBoxField.prototype._parseUnknownDate = function (text) {
var result;
if (text) {
var normalized = text.trim().replace(/[.-]/g, '/');
var native = new Date(normalized);
if (isNaN(native.getTime()) === false) {
result = native;
}
else {
// Extracts year, month, day, and optional time (hours, minutes, optional seconds) from a date string formatted as YYYY/MM/DD or similar.
var dateRegEx = /^(\d{1,4})\/(\d{1,2})\/(\d{1,4})$/;
var parts = normalized.trim().split(/\s+/);
var datePart = parts[0];
var dateMatch = void 0;
if (datePart) {
dateMatch = datePart.match(dateRegEx);
}
var timePart = parts[1];
if (dateMatch) {
var toNumberOrZero = function (value) { return isNaN(Number(value)) ? 0 : Number(value); };
var firstPart = toNumberOrZero(dateMatch[1]);
var secondPart = toNumberOrZero(dateMatch[2]);
var thirdPart = toNumberOrZero(dateMatch[3]);
var year = void 0;
var monthIndex = void 0;
var dayOfMonth = void 0;
if (thirdPart > 31) {
year = thirdPart;
if (firstPart > 12) {
dayOfMonth = firstPart;
monthIndex = secondPart - 1;
}
var hours = 0;
var minutes = 0;
var seconds = 0;
if (timePart) {
var timeSegments = timePart.split(':');
if (timeSegments.length >= 2 && timeSegments.length <= 3) {
var h = toNumberOrZero(timeSegments[0]);
var m = toNumberOrZero(timeSegments[1]);
var s = timeSegments[2] ? toNumberOrZero(timeSegments[2]) : 0;
if (h >= 0 && h <= 99 &&
m >= 0 && m <= 59 &&
s >= 0 && s <= 59) {
hours = h;
minutes = m;
seconds = s;
}
}
}
result = new Date(year, monthIndex, dayOfMonth, hours, minutes, seconds);
}
}
}
}
return result;
};
/**
* Formats a date using an acrobat date pattern with mapped tokens.
*
* @private
* @param {Date} date - Date to format.
* @param {string} format - Acrobat pattern.
* @returns {string} The formatted string.
*/
PdfTextBoxField.prototype._formatDateUsingAcrobatFormat = function (date, format) {
var monthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var monthsLong = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var pad = function (input) { return ('0' + input).slice(-2); };
var hours24 = date.getHours();
var hours12 = hours24 % 12 || 12;
var map = new Map([
['yyyy', date.getFullYear().toString()],
['yy', date.getFullYear().toString().slice(-2)],
['mmmm', monthsLong[date.getMonth()]],
['mmm', monthsShort[date.getMonth()]],
['mm', pad(date.getMonth() + 1)],
['m', (date.getMonth() + 1).toString()],
['dd', pad(date.getDate())],
['d', date.getDate().toString()],
['HH', pad(hours24)],
['H', hours24.toString()],
['hh', pad(hours12)],
['h', hours12.toString()],
['MM', pad(date.getMinutes())],
['M', date.getMinutes().toString()],
['ss', pad(date.getSeconds())],
['s', date.getSeconds().toString()],
['tt', hours24 < 12 ? 'AM' : 'PM']
]);
return format.replace(/yyyy|mmmm|mmm|mm|dd|HH|hh|MM|ss|yy|H|h|m|d|M|s|tt/g, function (token) {
var value = map.get(token);
return value;
});
};
/**
* Extracts an acrobat format pattern from a JavaScript format function call.
*
* @private
* @param {string} js - The JavaScript source.
* @returns {string} The extracted format pattern, if any.
*/
PdfTextBoxField.prototype._tryParseAcrobatFormFormat = function (js) {
var result;
if (typeof js === 'string') {
var source = js.replace(/\s+/g, ' ').trim();
var regex = /[a-zA-Z0-9_]*_FormatEx\s*\(\s*(['"])(.*?)\1\s*\)/i;
var match = source.match(regex);
if (Array.isArray(match) && match.length > 2 && typeof match[2] === 'string') {
result = match[2];
}
}
return result;
};
/**
* Draws the text content of a field applying borders background alignment and rotation.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {_PaintParameter} parameter - Paint parameters.
* @param {string} text - Text to render.
* @param {PdfFont} font - Font to use.
* @param {PdfStringFormat} format - String format.
* @param {boolean} multiline - Whether multiline is enabled.
* @param {boolean} scroll - Whether scrolling is enabled.
* @param {number} [maxLength] - Optional maximum length for comb fields.
* @returns {void} nothing.
*/
PdfTextBoxField.prototype._drawTextBox = function (g, parameter, text, font, format, multiline, scroll, maxLength) {
if (typeof maxLength !== 'undefined') {
if (parameter.insertSpaces) {
var width = 0;
if (typeof maxLength !== 'undefined' && maxLength > 0 && this.borderColor) {
width = parameter.bounds.width / maxLength;
g.drawRectangle(parameter.bounds, parameter.borderPen, parameter.backBrush);
var current = text;
for (var i = 0; i < maxLength; i++) {
if (format.alignment === PdfTextAlignment.right) {
if (maxLength - current.length <= i) {
text = current[i - (maxLength - current.length)];
}
else {
text = '';
}
}
else {
if (format.alignment === PdfTextAlignment.center && current.length < maxLength) {
var startlocation = Math.floor(maxLength / 2 - Math.ceil(current.length / 2));
if (i >= startlocation && i < startlocation + current.length) {
text = current[i - startlocation];
}
else {
text = '';
}
}
else {
if (current.length > i) {
text = current[i];
}
else {
text = '';
}
}
}
parameter.bounds.width = width;
var stringFormat = new PdfStringFormat(PdfTextAlignment.center, PdfVerticalAlignment.middle);
this._drawTextBox(g, parameter, text, font, stringFormat, multiline, scroll);
parameter.bounds.x = parameter.bounds.x + width;
if (parameter.borderWidth) {
g.drawLine(parameter.borderPen, { x: parameter.bounds.x, y: parameter.bounds.y }, { x: parameter.bounds.x, y: parameter.bounds.y + parameter.bounds.height });
}
}
}
else {
this._drawTextBox(g, parameter, text, font, format, multiline, scroll);
}
}
else {
this._drawTextBox(g, parameter, text, font, format, multiline, scroll);
}
}
else {
if (g._isTemplateGraphics && parameter.required) {
g.save();
g._initializeCoordinates();
}
if (!parameter.insertSpaces) {
this._drawRectangularControl(g, parameter);
}
if (g._isTemplateGraphics && parameter.required) {
g.restore();
g.save();
g._sw._beginMarkupSequence('Tx');
g._initializeCoordinates();
}
var rectangle = parameter.bounds;
var rotate = this.rotate;
if (rotate !== null && typeof rotate !== 'undefined' && rotate === 90) {
rectangle.y = rectangle.width / 2;
}
if (parameter.borderStyle === PdfBorderStyle.beveled || parameter.borderStyle === PdfBorderStyle.inset) {
rectangle.x = rectangle.x + 4 * parameter.borderWidth;
rectangle.width = rectangle.width - 8 * parameter.borderWidth;
}
else {
rectangle.x = rectangle.x + 2 * parameter.borderWidth;
rectangle.width = rectangle.width - 4 * parameter.borderWidth;
}
if (multiline) {
var tempheight = (typeof format === 'undefined' || format === null || format.lineSpacing === 0) ?
font._getHeight() :
format.lineSpacing;
var ascent = font._getAscent(format);
var shift = tempheight - ascent;
if (text.indexOf('\n') !== -1) {
if (rectangle.x === 0 && rectangle.y === 1) {
rectangle.y = -(rectangle.y - shift);
}
}
else if (rectangle.x === 0 && rectangle.y === 1) {
rectangle.y = -(rectangle.y - shift);
}
if (parameter.isAutoFontSize) {
if (parameter.borderWidth !== 0) {
rectangle.y = rectangle.y + 2.5 * parameter.borderWidth;
}
}
}
if ((g._page &&
typeof g._page.rotation !== 'undefined' &&
g._page.rotation !== PdfRotationAngle.angle0) ||
parameter.rotationAngle > 0) {
var state = g.save();
if (typeof parameter.pageRotationAngle !== 'undefined' && parameter.pageRotationAngle !== PdfRotationAngle.angle0) {
if (parameter.pageRotationAngle === PdfRotationAngle.angle90) {
g.translateTransform({ x: g._size.height, y: 0 });
g.rotateTransform(90);
var y = g._size.height - (rectangle.x + rectangle.width);
var x = rectangle.y;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
else if (parameter.pageRotationAngle === PdfRotationAngle.angle180) {
g.translateTransform({ x: g._size.width, y: g._size.height });
g.rotateTransform(-180);
var x = g._size.width - (rectangle.x + rectangle.width);
var y = g._size.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
else if (parameter.pageRotationAngle === PdfRotationAngle.angle270) {
g.translateTransform({ x: 0, y: g._size.width });
g.rotateTransform(270);
var x = g._size.width - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
}
if (parameter.rotationAngle) {
if (parameter.rotationAngle === 90) {
if (parameter.pageRotationAngle === PdfRotationAngle.angle90) {
g.translateTransform({ x: 0, y: g._size.height });
g.rotateTransform(-90);
var x = g._size.height - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
parameter.stringFormat = new PdfStringFormat(PdfTextAlignment.center, PdfVerticalAlignment.middle);
}
else {
if (rectangle.width > rectangle.height) {
g.translateTransform({ x: 0, y: g._size.height });
g.rotateTransform(-90);
rectangle = parameter.bounds;
rectangle.y = (rectangle.width / 2) - (8 * parameter.borderWidth);
}
else {
var z = rectangle.x;
rectangle.x = -(rectangle.y + rectangle.height);
rectangle.y = z;
var height = rectangle.height;
rectangle.height = rectangle.width > font._getHeight() ? rectangle.width : font._getHeight();
rectangle.width = height;
g.rotateTransform(-90);
}
}
}
else if (parameter.rotationAngle === 270) {
rectangle = { x: rectangle.x, y: rectangle.y, width: rectangle.width - (4 * parameter.borderWidth), height: rectangle.height };
}
else if (parameter.rotationAngle === 180) {
g.translateTransform({ x: g._size.width, y: g._size.height });
g.rotateTransform(-180);
var x = g._size.width - (rectangle.x + rectangle.width);
var y = g._size.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
}
g.drawString(text, font, rectangle, null, parameter.foreBrush, format);
g.restore(state);
}
else {
g.drawString(text, font, rectangle, null, parameter.foreBrush, format);
}
if (g._isTemplateGraphics && parameter.required) {
g._sw._endMarkupSequence();
g.restore();
}
}
};
return PdfTextBoxField;
}(PdfField));
export { PdfTextBoxField };
/**
* `PdfButtonField` class represents the button field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new button field
* let field: PdfButtonField = new PdfButtonField(page , 'Button1', {x: 100, y: 40, width: 100, height: 20});
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfButtonField = /** @class */ (function (_super) {
__extends(PdfButtonField, _super);
function PdfButtonField(page, name, bounds, properties) {
var _this = _super.call(this) || this;
if (page && name && bounds) {
_this._initialize(page, name, bounds);
}
if (properties) {
if ('text' in properties && _isNullOrUndefined(properties.text)) {
_this.text = properties.text;
}
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
if ('highlightMode' in properties && _isNullOrUndefined(properties.highlightMode)) {
_this.highlightMode = properties.highlightMode;
}
}
return _this;
}
Object.defineProperty(PdfButtonField.prototype, "actions", {
/**
* Gets the actions of the field. [Read-Only]
*
* @returns {PdfFieldActions} The actions.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access button field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Get the action value from button field
* let action: PdfAction = field.actions.mouseEnter;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (!this._actions) {
this._actions = new PdfFieldActions(this);
}
return this._actions;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfButtonField.prototype, "text", {
/**
* Gets value of the text box field.
*
* @returns {string} Text.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access text box field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Gets the text value from button field
* let text: string = field.text;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded) {
if (typeof this._text === 'undefined') {
var widget = this.itemAt(this._defaultIndex);
if (widget && widget._mkDictionary && widget._mkDictionary.has('CA')) {
this._text = widget._mkDictionary.get('CA');
}
else if (this._mkDictionary && this._mkDictionary.has('CA')) {
this._text = this._mkDictionary.get('CA');
}
}
if (typeof this._text === 'undefined') {
var value = _getInheritableProperty(this._dictionary, 'V', false, true, 'Parent');
if (value) {
this._text = value;
}
}
}
if (typeof this._text === 'undefined') {
this._text = '';
}
return this._text;
},
/**
* Sets value of the text box field.
*
* @param {string} value Text.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access button field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Sets the text value of form field
* field.text = 'Click to submit';
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._isLoaded && !this.readOnly) {
var widget = this.itemAt(this._defaultIndex);
if (widget && widget._dictionary) {
this._assignText(widget._dictionary, value);
}
else {
this._assignText(this._dictionary, value);
}
this._text = value;
}
if (!this._isLoaded && this._text !== value) {
var widget = this.itemAt(this._defaultIndex);
this._assignText(widget._dictionary, value);
this._text = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfButtonField.prototype, "textAlignment", {
/**
* Gets the text alignment in a button field.
*
* @returns {PdfTextAlignment} Text alignment.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access button field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Gets the text alignment from button field
* let alignment: PdfTextAlignment = field.textAlignment;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._getTextAlignment();
},
/**
* Sets the text alignment in a button field.
*
* @param {PdfTextAlignment} value Text alignment.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access button field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Sets the text alignment of form field as center
* field.textAlignment = PdfTextAlignment.center;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._textAlignment !== value) {
this._setTextAlignment(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfButtonField.prototype, "highlightMode", {
/**
* Gets the highlight mode of the field.
*
* @returns {PdfHighlightMode} highlight mode.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access button field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Gets the highlight mode from button field
* let highlightMode: PdfHighlightMode = field. highlightMode;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var widget = this.itemAt(this._defaultIndex);
var mode;
if (widget && typeof widget.highlightMode !== 'undefined') {
mode = widget.highlightMode;
}
else if (this._dictionary && this._dictionary.has('H')) {
var highlight = this._dictionary.get('H');
mode = _mapHighlightMode(highlight.name);
}
return (typeof mode !== 'undefined') ? mode : PdfHighlightMode.invert;
},
/**
* Sets the highlight mode of the field.
*
* @param {PdfHighlightMode} value highlight mode.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access button field
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Sets the highlight mode of button field as outline
* field.highlightMode = PdfHighlightMode.outline;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var widget = this.itemAt(this._defaultIndex);
if (widget && (typeof widget.highlightMode === 'undefined' || widget.highlightMode !== value)) {
widget.highlightMode = value;
}
else if (!this._dictionary.has('H') || _mapHighlightMode(this._dictionary.get('H')) !== value) {
this._dictionary.update('H', _reverseMapHighlightMode(value));
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfButtonField.prototype, "font", {
/**
* Gets the font of the field.
*
* @returns {PdfFont} font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Gets the font of the field.
* let font: PdfFont = field.font;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._font) {
return this._font;
}
else {
var widget = this.itemAt(this._defaultIndex);
this._font = _obtainFontDetails(this._form, widget, this);
}
return this._font;
},
/**
* Sets the font of the field.
*
* @param {PdfFont} value font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
* // Sets the font of the field
* field.font = document.embedFont(PdfFontFamily.helvetica, 12, PdfFontStyle.bold);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value && value instanceof PdfFont) {
this._font = value;
this._initializeFont(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfButtonField.prototype, "backColor", {
/**
* Gets the background color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the background color of the field.
* let backColor: PdfColor = field.backColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBackColor(true);
},
/**
* Sets the background color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the button field at index 0
* let submitButton: PdfField = document.form.fieldAt(0);
* // Sets the background color of the field.
* submitButton.backColor = {r: 255, g: 0, b: 0};
* // Access the button field at index 1
* let cancelButton: PdfField = document.form.fieldAt(1);
* // Sets the background color of the field to transparent.
* cancelButton.backColor = {r: 0, g: 0, b: 0, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBackColor(value, true);
},
enumerable: true,
configurable: true
});
/**
* Updates the appearance characteristics dictionary with the given caption text.
*
* @private
* @param {_PdfDictionary} fieldDictionary - Field or widget dictionary.
* @param {string} value - Caption text.
* @returns {void} nothing.
*/
PdfButtonField.prototype._assignText = function (fieldDictionary, value) {
var dictionary;
if (fieldDictionary && fieldDictionary.has('MK')) {
dictionary = fieldDictionary.get('MK');
}
else {
dictionary = new _PdfDictionary(this._crossReference);
fieldDictionary.set('MK', dictionary);
}
dictionary.update('CA', value);
fieldDictionary._updated = true;
};
/**
* Parse an existing button field.
*
* @private
* @param {PdfForm} form Form object.
* @param {_PdfDictionary} dictionary Field dictionary.
* @param {_PdfCrossReference} crossReference Cross reference object.
* @param {_PdfReference} reference Field reference.
* @returns {PdfButtonField} Button field.
*/
PdfButtonField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfButtonField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
field._defaultIndex = 0;
field._parsedItems = new Map();
return field;
};
/* eslint-disable */
/**
* Initializes the button field with core dictionary entries widget creation and font setup.
*
* @private
* @param {PdfPage} page - The page where the field is drawn.
* @param {string} name - The field name.
* @param {{x: number, y: number, width: number, height: number}} bounds - The field bounds.
* @returns {void} nothing.
*/
PdfButtonField.prototype._initialize = function (page, name, bounds) {
this._crossReference = page._crossReference;
this._page = page;
this._name = name;
this._defaultIndex = 0;
this._dictionary = new _PdfDictionary(this._crossReference);
this._ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(this._ref, this._dictionary);
this._dictionary.objId = this._ref.toString();
this._dictionary.update('FT', _PdfName.get('Btn'));
this._dictionary.update('T', name);
this._fieldFlags |= _FieldFlag.pushButton;
this._initializeFont(this._defaultFont);
this._createItem(bounds);
};
/**
* Creates the button widget initializes appearance values and registers the kid.
*
* @private
* @param {{x: number, y: number, width: number, height: number}} bounds - Widget bounds. // eslint-disable-line
* @returns {void} nothing.
*/
PdfButtonField.prototype._createItem = function (bounds) {
var widget = new PdfWidgetAnnotation();
widget._create(this._page, bounds, this);
widget.textAlignment = PdfTextAlignment.center;
this._stringFormat = new PdfStringFormat(widget.textAlignment, PdfVerticalAlignment.middle);
widget._dictionary.update('MK', new _PdfDictionary(this._crossReference));
widget._mkDictionary.update('BC', [0, 0, 0]);
widget._mkDictionary.update('BG', [.827451, .827451, .827451]);
widget._mkDictionary.update('CA', (typeof this._name !== 'undefined' && this._name !== null) ? this._name : this._actualName);
this._addToKid(widget);
};
/* eslint-enable */
/**
* Builds appearances or flattens widgets based on settings and field state.
*
* @private
* @param {boolean} [isFlatten=false] - When `true`, flattens the field appearances.
* @returns {void} nothing.
*/
PdfButtonField.prototype._doPostProcess = function (isFlatten) {
if (isFlatten === void 0) { isFlatten = false; }
if (isFlatten || this._setAppearance || this._form._setAppearance) {
var count = this._kidsCount;
if (this._isLoaded) {
if (count > 0) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item) {
this._postProcess(isFlatten, item);
}
}
}
else if ((isFlatten || this._form._setAppearance || this._setAppearance) && !this._checkFieldFlag(this._dictionary)) {
this._postProcess(isFlatten);
}
}
else if (isFlatten || this._form._setAppearance || this._setAppearance) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item && !this._checkFieldFlag(item._dictionary)) {
var template = this._createAppearance(item);
if (isFlatten) {
var bounds = { x: item.bounds.x,
y: item.bounds.y,
width: template._size.width,
height: template._size.height };
this._drawTemplate(template, item._getPage(), bounds);
}
else {
this._addAppearance(item._dictionary, template, 'N');
var pressed = this._createAppearance(item, true);
if (pressed) {
this._addAppearance(item._dictionary, pressed, 'D');
}
}
item._dictionary._updated = !isFlatten;
}
}
}
if (isFlatten) {
this._dictionary._updated = false;
}
}
};
/**
* Processes one widget to load reuse or rebuild its appearance and optionally flatten.
*
* @private
* @param {boolean} isFlatten - Whether to flatten onto the page.
* @param {PdfWidgetAnnotation} [widget] - Optional widget to process; defaults to the field.
* @returns {void} nothing.
*/
PdfButtonField.prototype._postProcess = function (isFlatten, widget) {
var template;
var bounds;
var source = widget ? widget : this;
if ((widget !== null && typeof widget !== 'undefined' && widget._setAppearance && widget._enableGrouping) || this._form._setAppearance || this._setAppearance || (isFlatten && !source._dictionary.has('AP'))) {
template = this._createAppearance(source);
}
else if (source._dictionary.has('AP')) {
var appearanceStream = void 0;
var dictionary = source._dictionary.get('AP');
if (dictionary && dictionary.has('N')) {
appearanceStream = dictionary.get('N');
var reference = dictionary.getRaw('N');
if (reference) {
appearanceStream.reference = reference;
}
if (appearanceStream && appearanceStream instanceof _PdfDictionary && source._dictionary.has('AS')) {
var name_5 = source._dictionary.get('AS');
if (name_5 && name_5 instanceof _PdfName && appearanceStream.has(name_5.name)) {
var reference_1 = appearanceStream.getRaw(name_5.name);
var value = appearanceStream.get(name_5.name);
if (reference_1 && value && value instanceof _PdfBaseStream) {
appearanceStream = value;
appearanceStream.reference = reference_1;
}
}
}
if (appearanceStream) {
template = new PdfTemplate(appearanceStream, this._crossReference);
}
}
}
if (template) {
if (isFlatten) {
var page = source instanceof PdfWidgetAnnotation ? source._getPage() : source.page;
if (page) {
var graphics = page.graphics;
graphics.save();
if (page.rotation === PdfRotationAngle.angle90) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(90);
}
else if (page.rotation === PdfRotationAngle.angle180) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(-180);
}
else if (page.rotation === PdfRotationAngle.angle270) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(270);
}
bounds = { x: source.bounds.x, y: source.bounds.y, width: template._size.width, height: template._size.height };
graphics.drawTemplate(template, bounds);
graphics.restore();
}
source._dictionary._updated = false;
}
else {
this._addAppearance(source._dictionary, template, 'N');
}
}
};
/**
* Constructs the button appearance template including background border caption and shadows.
*
* @private
* @param {PdfWidgetAnnotation | PdfButtonField} widget - Source widget or field.
* @param {boolean} [isPressed=false] - Whether to render the pressed state.
* @returns {PdfTemplate} The generated appearance template.
*/
PdfButtonField.prototype._createAppearance = function (widget, isPressed) {
if (isPressed === void 0) { isPressed = false; }
var bounds = widget.bounds;
var template = new PdfTemplate([0, 0, bounds.width, bounds.height], this._crossReference);
var parameter = new _PaintParameter();
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
var text;
var font;
var stringFormat;
var enableGrouping = false;
var isSizeZero = false;
var backcolor = widget.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(widget.color);
var border = widget.border;
if (widget.borderColor) {
parameter.borderPen = new PdfPen(widget.borderColor, border.width);
_updateDashedBorderStyle(border, parameter);
}
parameter.borderWidth = border.width;
parameter.borderStyle = border.style;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = [shadowColor[0] >= 0 ? shadowColor[0] : 0,
shadowColor[1] >= 0 ? shadowColor[1] : 0,
shadowColor[2] >= 0 ? shadowColor[2] : 0];
parameter.shadowBrush = new PdfBrush({ r: color[0], g: color[1], b: color[2] });
}
parameter.rotationAngle = widget.rotate;
if (widget !== null && typeof widget !== 'undefined' && widget instanceof PdfWidgetAnnotation && widget._enableGrouping) {
enableGrouping = true;
}
if (enableGrouping) {
if (widget._mkDictionary && widget._mkDictionary && widget._mkDictionary.has('CA')) {
text = widget._mkDictionary.get('CA');
}
else {
text = '';
}
if (typeof widget.font !== 'undefined' && widget.font.size !== null && widget.font.size !== 0) {
font = widget.font;
}
stringFormat = new PdfStringFormat(widget.textAlignment, PdfVerticalAlignment.middle);
}
else if (typeof this._font === 'undefined' || this._font === null) {
this._font = this._defaultFont;
}
if (this._isLoaded && widget instanceof PdfWidgetAnnotation &&
widget !== null && typeof widget !== 'undefined' && widget._defaultAppearance) {
var fontName = widget._defaultAppearance.fontName;
if (fontName === null || typeof fontName === 'undefined') {
fontName = 'Helvetica';
}
var fontSize = widget._defaultAppearance.fontSize;
if (fontSize === null || typeof fontSize === 'undefined') {
fontSize = this._defaultFont.size;
}
else if (fontSize === 0) {
isSizeZero = true;
}
var previousFont = void 0;
var currentFont = void 0;
var font_1;
this._stringFormat = new PdfStringFormat();
this._stringFormat.lineAlignment = PdfVerticalAlignment.middle;
this._stringFormat.alignment = PdfTextAlignment.center;
if (fontSize !== null && typeof fontSize !== 'undefined' && fontName) {
font_1 = _mapFont(fontName, fontSize, PdfFontStyle.regular, widget);
}
if (font_1 !== null && typeof font_1 !== 'undefined') {
currentFont = font_1;
}
else {
currentFont = this._defaultFont;
}
var textWidth = currentFont.measureString(this.text, this._stringFormat);
if (isSizeZero && currentFont && currentFont instanceof PdfStandardFont) {
if (this._isLoaded && !widget._dictionary.has('AP')) {
var width = widget.bounds.width - 8 * border.width;
var height = widget.bounds.height - 8 * border.width;
while (textWidth.width < width || textWidth.height < height) {
previousFont = currentFont;
currentFont = new PdfStandardFont(currentFont.fontFamily, currentFont._size + 1);
textWidth = currentFont.measureString(this.text, this._stringFormat);
if (textWidth.width > width || textWidth.height > height) {
currentFont = previousFont;
break;
}
}
this._font = currentFont;
}
}
}
if (enableGrouping) {
if (isPressed) {
this._drawPressedButton(template.graphics, parameter, text, font, stringFormat);
}
else {
this._drawButton(template.graphics, parameter, text, font, stringFormat);
}
}
else {
if (isPressed) {
this._drawPressedButton(template.graphics, parameter, this.text, this._font, this._stringFormat);
}
else {
this._drawButton(template.graphics, parameter, this.text, this._font, this._stringFormat);
}
}
return template;
};
/**
* Draws the normal button state with background border and caption respecting rotation.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {_PaintParameter} parameter - Paint parameters.
* @param {string} text - Caption text.
* @param {PdfFont} font - Caption font.
* @param {PdfStringFormat} format - Caption format.
* @returns {void} nothing.
*/
PdfButtonField.prototype._drawButton = function (g, parameter, text, font, format) {
this._drawRectangularControl(g, parameter);
var rectangle = parameter.bounds;
if ((g._page &&
typeof g._page.rotation !== 'undefined' &&
g._page.rotation !== PdfRotationAngle.angle0) ||
parameter.rotationAngle > 0) {
var state = g.save();
if (typeof parameter.pageRotationAngle !== 'undefined' && parameter.pageRotationAngle !== PdfRotationAngle.angle0) {
if (parameter.pageRotationAngle === PdfRotationAngle.angle90) {
g.translateTransform({ x: g._size.height, y: 0 });
g.rotateTransform(90);
var y = g._size.height - (rectangle.x + rectangle.width);
var x = rectangle.y;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
else if (parameter.pageRotationAngle === PdfRotationAngle.angle180) {
g.translateTransform({ x: g._size.width, y: g._size.height });
g.rotateTransform(-180);
var x = g._size.width - (rectangle.x + rectangle.width);
var y = g._size.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
else if (parameter.pageRotationAngle === PdfRotationAngle.angle270) {
g.translateTransform({ x: 0, y: g._size.width });
g.rotateTransform(270);
var x = g._size.width - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
}
if (parameter.rotationAngle) {
if (parameter.rotationAngle === 90) {
if (parameter.pageRotationAngle === PdfRotationAngle.angle90) {
g.translateTransform({ x: 0, y: g._size.height });
g.rotateTransform(-90);
var x = g._size.height - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
}
else {
if (rectangle.width > rectangle.height) {
g.translateTransform({ x: 0, y: g._size.height });
g.rotateTransform(-90);
var x = g._size.height - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
format._wordWrapType = _PdfWordWrapType.none;
}
else {
var z = rectangle.x;
rectangle.x = -(rectangle.y + rectangle.height);
rectangle.y = z;
var height = rectangle.height;
rectangle.height = rectangle.width > font._getHeight() ? rectangle.width : font._getHeight();
rectangle.width = height;
g.rotateTransform(-90);
}
}
}
else if (parameter.rotationAngle === 270) {
g.translateTransform({ x: g._size.width, y: 0 });
g.rotateTransform(-270);
var x = rectangle.y;
var y = g._size.width - (rectangle.x + rectangle.width);
rectangle = { x: x, y: y, width: rectangle.height, height: rectangle.width };
format._wordWrapType = _PdfWordWrapType.none;
}
else if (parameter.rotationAngle === 180) {
g.translateTransform({ x: g._size.width, y: g._size.height });
g.rotateTransform(-180);
var x = g._size.width - (rectangle.x + rectangle.width);
var y = g._size.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
}
g.drawString(text, font, rectangle, null, parameter.foreBrush, format);
g.restore(state);
}
else {
g.drawString(text, font, rectangle, null, parameter.foreBrush, format);
}
};
/**
* Draws the pressed button state with adjusted fill border shadows and caption position.
*
* @private
* @param {PdfGraphics} g - Graphics context.
* @param {_PaintParameter} parameter - Paint parameters.
* @param {string} text - Caption text.
* @param {PdfFont} font - Caption font.
* @param {PdfStringFormat} format - Caption format.
* @returns {void} nothing.
*/
PdfButtonField.prototype._drawPressedButton = function (g, parameter, text, font, format) {
switch (parameter.borderStyle) {
case PdfBorderStyle.inset:
g.drawRectangle(parameter.bounds, parameter.shadowBrush);
break;
default:
g.drawRectangle(parameter.bounds, parameter.backBrush);
break;
}
this._drawBorder(g, parameter.bounds, parameter.borderPen, parameter.borderStyle, parameter.borderWidth);
var rectangle = { x: parameter.borderWidth,
y: parameter.borderWidth,
width: parameter.bounds.width - parameter.borderWidth,
height: parameter.bounds.height - parameter.borderWidth };
g.drawString(text, font, rectangle, null, parameter.foreBrush, format);
switch (parameter.borderStyle) {
case PdfBorderStyle.inset:
this._drawLeftTopShadow(g, parameter.bounds, parameter.borderWidth, this._grayBrush);
this._drawRightBottomShadow(g, parameter.bounds, parameter.borderWidth, this._silverBrush);
break;
case PdfBorderStyle.beveled:
this._drawLeftTopShadow(g, parameter.bounds, parameter.borderWidth, parameter.shadowBrush);
this._drawRightBottomShadow(g, parameter.bounds, parameter.borderWidth, this._whiteBrush);
break;
default:
this._drawLeftTopShadow(g, parameter.bounds, parameter.borderWidth, parameter.shadowBrush);
break;
}
};
return PdfButtonField;
}(PdfField));
export { PdfButtonField };
/**
* `PdfCheckBoxField` class represents the check box field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new check box field
* let field: PdfCheckBoxField = new PdfCheckBoxField('CheckBox1', {x: 100, y: 40, width: 20, height: 20}, page);
* // Sets the checked flag as true.
* field.checked = true;
* // Sets the tool tip value
* field.toolTip = 'Checked';
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfCheckBoxField = /** @class */ (function (_super) {
__extends(PdfCheckBoxField, _super);
function PdfCheckBoxField(name, bounds, page, properties) {
var _this = _super.call(this) || this;
if (page && name && bounds) {
_this._initialize(page, name, bounds);
}
if (properties) {
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
if ('checked' in properties && _isNullOrUndefined(properties.checked)) {
_this.checked = properties.checked;
}
}
return _this;
}
/**
* Parse an existing check box field.
*
* @private
* @param {PdfForm} form Form object.
* @param {_PdfDictionary} dictionary Field dictionary.
* @param {_PdfCrossReference} crossReference Cross reference object.
* @param {_PdfReference} reference Field reference.
* @returns {PdfCheckBoxField} Check box field.
*/
PdfCheckBoxField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfCheckBoxField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
field._defaultIndex = 0;
field._parsedItems = new Map();
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
else {
var item = PdfStateItem._load(dictionary, crossReference, field);
item._isLoaded = true;
item._ref = reference;
field._parsedItems.set(0, item);
}
return field;
};
/**
* Gets the item at the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the check box field
* let field: PdfCheckBoxField = form.fieldAt(0) as PdfCheckBoxField;
* // Gets the first list item.
* let item: PdfStateItem = field.itemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Index of the field item.
* @returns {PdfStateItem} Field item at the index.
*/
PdfCheckBoxField.prototype.itemAt = function (index) {
if (index < 0 || (index !== 0 && index >= this._kidsCount)) {
throw Error('Index out of range.');
}
var item;
if (this._parsedItems.has(index)) {
item = this._parsedItems.get(index);
}
else {
var dictionary = void 0;
if (index >= 0 && this._kids && this._kids.length > 0 && index < this._kids.length) {
var ref = this._kids[index];
if (ref && ref instanceof _PdfReference) {
dictionary = this._crossReference._fetch(ref);
}
if (dictionary) {
item = PdfStateItem._load(dictionary, this._crossReference, this);
item._isLoaded = true;
item._ref = ref;
this._parsedItems.set(index, item);
}
}
}
return item;
};
Object.defineProperty(PdfCheckBoxField.prototype, "font", {
/**
* Gets the font of the field.
*
* @returns {PdfFont} font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfCheckBoxField = document.form.fieldAt(0) as PdfCheckBoxField;
* // Gets the font of the field.
* let font: PdfFont = field.font;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._font) {
return this._font;
}
else {
var widget = this.itemAt(this._defaultIndex);
this._font = _obtainFontDetails(this._form, widget, this);
}
return this._font;
},
/**
* Sets the font of the field.
*
* @param {PdfFont} value font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfCheckBoxField = document.form.fieldAt(0) as PdfCheckBoxField;
* // Sets the font of the field
* field.font = document.embedFont(PdfFontFamily.helvetica, 12, PdfFontStyle.bold);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value && value instanceof PdfFont) {
this._font = value;
this._initializeFont(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfCheckBoxField.prototype, "checked", {
/**
* Gets the flag indicating whether the field is checked or not.
*
* @returns {boolean} Checked.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the check box field
* let field: PdfCheckBoxField = form.fieldAt(0) as PdfCheckBoxField;
* // Gets the flag indicating whether the field is checked or not.
* let checked: Boolean = field.checked;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return (this._kidsCount > 0) ? this.itemAt(this._defaultIndex).checked : _checkField(this._dictionary);
},
/**
* Sets the flag indicating whether the field is checked or not.
*
* @param {boolean} value Checked.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the check box field
* let field: PdfCheckBoxField = form.fieldAt(0) as PdfCheckBoxField;
* // Sets the flag indicating whether the field is checked or not.
* field.checked = true;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this.checked !== value) {
if (this._crossReference && this._crossReference._document &&
this._crossReference._document._isFormImport && this._kidsCount > 0) {
var kidItem = this.itemAt(this._defaultIndex);
if (kidItem.checked !== value) {
kidItem.checked = value;
return;
}
}
if (this._kidsCount > 0) {
for (var i = 0; i < this._kidsCount; i++) {
var kidItem = this.itemAt(i);
if (kidItem.checked !== value) {
kidItem.checked = value;
}
}
}
if (value) {
if (this._isLoaded) {
var entry = _getItemValue((this._kidsCount > 0) ?
this.itemAt(this._defaultIndex)._dictionary : this._dictionary);
this._dictionary.update('V', _PdfName.get(entry));
this._dictionary.update('AS', _PdfName.get(entry));
}
else {
this._dictionary.update('V', _PdfName.get(this.exportValue));
this._dictionary.update('AS', _PdfName.get(this.exportValue));
}
}
else {
if (this._dictionary.has('V')) {
delete this._dictionary._map.V;
}
if (this._dictionary.has('AS')) {
delete this._dictionary._map.AS;
}
}
this._dictionary._updated = true;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfCheckBoxField.prototype, "exportValue", {
/**
* Gets the export value of the check box field.
*
* @returns {boolean} Checked.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the check box field
* let field: PdfCheckBoxField = form.fieldAt(0) as PdfCheckBoxField;
* // Gets the export value of the checkbox field.
* let value: string = field.exportValue;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded && typeof this._exportValue === 'string' && this._exportValue !== 'Yes') {
return this._exportValue;
}
return this._isLoaded ? _getItemValue(this._dictionary) : this._exportValue;
},
/**
* Sets the export value of the check box field.
*
* @param {boolean} value Checked.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the check box field
* let field: PdfCheckBoxField = form.fieldAt(0) as PdfCheckBoxField;
* // Sets the export value.
* field.exportValue = 'Value';
* // Set the chexk box field as checked
* field.checked = true;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this.itemAt(this._defaultIndex)) {
this.itemAt(this._defaultIndex).exportValue = value;
}
if (!(this._dictionary.has('V')) && value !== '') {
this._exportValue = value;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfCheckBoxField.prototype, "textAlignment", {
/**
* Gets the text alignment in a check box field.
*
* @returns {PdfTextAlignment} Text alignment.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access check box field
* let field: PdfCheckBoxField = document.form.fieldAt(0) as PdfCheckBoxField;
* // Gets the text alignment from check box field
* let alignment: PdfTextAlignment = field.textAlignment;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._getTextAlignment();
},
/**
* Sets the text alignment in a check box field.
*
* @param {PdfTextAlignment} value Text alignment.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access check box field
* let field: PdfCheckBoxField = document.form.fieldAt(0) as PdfCheckBoxField;
* // Sets the text alignment of form field as center
* field.textAlignment = PdfTextAlignment.center;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._textAlignment !== value) {
this._setTextAlignment(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfCheckBoxField.prototype, "backColor", {
/**
* Gets the background color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the background color of the field.
* let backColor: PdfColor = field.backColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBackColor(true);
},
/**
* Sets the background color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the check box field at index 0
* let checkBox1: PdfField = document.form.fieldAt(0);
* // Sets the background color of the field.
* checkBox1.backColor = {r: 255, g: 0, b: 0};
* // Access the check box field at index 1
* let checkBox2: PdfField = document.form.fieldAt(1);
* // Sets the background color of the field to transparent.
* checkBox2.backColor = {r: 0, g: 0, b: 0, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBackColor(value, true);
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfCheckBoxField.prototype, "borderColor", {
/**
* Gets the border color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the border color of the field.
* let borderColor: PdfColor = field.borderColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBorderColor(true);
},
/**
* Sets the border color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the border color of the field.
* field.borderColor = {r: 255, g: 0, b: 0};
* // Sets the background color of the field to transparent.
* field.backColor = {r: 0, g: 0, b: 0, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBorderColor(value, true);
if (this._isLoaded) {
this._setAppearance = true;
}
},
enumerable: true,
configurable: true
});
/**
* Initializes the checkbox field with core dictionary entries and widget creation.
*
* @private
* @param {PdfPage} page - The page where the field is placed.
* @param {string} name - The field name.
* @param {Rectangle} bounds - The field bounds.
* @returns {void} nothing.
*/
PdfCheckBoxField.prototype._initialize = function (page, name, bounds) {
this._crossReference = page._crossReference;
this._page = page;
this._name = name;
this._defaultIndex = 0;
this._dictionary = new _PdfDictionary(this._crossReference);
this._ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(this._ref, this._dictionary);
this._dictionary.objId = this._ref.toString();
this._dictionary.update('FT', _PdfName.get('Btn'));
this._dictionary.update('T', name);
this._createItem(bounds);
};
/**
* Creates the checkbox widget, sets default appearance, and registers the kid.
*
* @private
* @param {Rectangle} bounds - The widget bounds.
* @returns {void} nothing.
*/
PdfCheckBoxField.prototype._createItem = function (bounds) {
var widget = new PdfStateItem();
widget._create(this._page, bounds, this);
widget.textAlignment = PdfTextAlignment.center;
this._stringFormat = new PdfStringFormat(widget.textAlignment, PdfVerticalAlignment.middle);
widget._dictionary.update('MK', new _PdfDictionary(this._crossReference));
widget._mkDictionary.update('BC', [0, 0, 0]);
widget._mkDictionary.update('BG', [1, 1, 1]);
widget.style = PdfCheckBoxStyle.check;
widget._dictionary.update('DA', '/TiRo 0 Tf 0 0 0 rg');
this._addToKid(widget);
};
/**
* Builds appearances or flattens each widget based on state and settings.
*
* @private
* @param {boolean} [isFlatten=false] - When `true`, flattens the widget appearance onto the page.
* @returns {void} nothing.
*/
PdfCheckBoxField.prototype._doPostProcess = function (isFlatten) {
if (isFlatten === void 0) { isFlatten = false; }
var count = this._kidsCount;
if (!this._isLoaded) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item) {
var state = item.checked ? _PdfCheckFieldState.checked : _PdfCheckFieldState.unchecked;
item._postProcess(item.checked ? item.exportValue : 'Off');
if (isFlatten) {
var template = this._createAppearance(item, state);
this._drawTemplate(template, item._getPage(), item.bounds);
}
else {
var exportValue = void 0;
if (item.exportValue === '') {
exportValue = 'Yes';
}
else {
exportValue = item.exportValue;
}
this._drawAppearance(item, exportValue);
}
item._dictionary._updated = !isFlatten;
}
}
}
else if (isFlatten || this._setAppearance || this._dictionary._updated || this._isImport) {
if (count > 0) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item) {
if (!this._checkFieldFlag(item._dictionary)) {
if (isFlatten) {
var template = void 0;
var state = item.checked ?
_PdfCheckFieldState.checked :
_PdfCheckFieldState.unchecked;
if (this._setAppearance || this._form._setAppearance || !item._dictionary.has('AP')) {
template = this._createAppearance(item, state);
}
else {
template = _getStateTemplate(state, item);
}
this._drawTemplate(template, item._getPage(), item.bounds);
}
else if (this._setAppearance || this._form._setAppearance || !item._isLoaded) {
item._postProcess(item.checked ? item.exportValue : 'Off');
this._drawAppearance(item, item.exportValue);
}
}
item._dictionary._updated = !isFlatten;
}
}
}
else {
var style = this.checked ?
_PdfCheckFieldState.checked :
_PdfCheckFieldState.unchecked;
this._drawTemplate(_getStateTemplate(style, this), this.page, this.bounds);
}
}
this._dictionary._updated = !isFlatten;
};
/**
* Constructs the checkbox appearance template including background, border, and mark.
*
* @private
* @param {PdfStateItem} widget - The widget to build the appearance for.
* @param {_PdfCheckFieldState} state - The check state to render.
* @returns {PdfTemplate} The generated appearance template.
*/
PdfCheckBoxField.prototype._createAppearance = function (widget, state) {
var bounds = widget.bounds;
var parameter = new _PaintParameter();
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
var backcolor = widget.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(widget.color);
var border = widget.border;
if (widget.borderColor) {
parameter.borderPen = new PdfPen(widget.borderColor, border.width);
}
parameter.borderWidth = border.width;
parameter.borderStyle = border.style;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
parameter.rotationAngle = widget.rotate;
var template = new PdfTemplate(parameter.bounds, this._crossReference);
var graphics = template.graphics;
if (widget._styleText) {
this._drawCheckBox(graphics, parameter, widget._styleText, state);
}
else {
this._drawCheckBox(graphics, parameter, _styleToString(widget._style), state);
}
return template;
};
/**
* Writes the normal and pressed appearance streams for a widget's state values.
*
* @private
* @param {PdfStateItem} item - The widget state item whose appearance will be created/updated.
* @param {string} [itemValue] - Optional on-state name to use (defaults to "Yes" when not provided).
* @returns {void} nothing.
*/
PdfCheckBoxField.prototype._drawAppearance = function (item, itemValue) {
var appearance = new _PdfDictionary();
if (item._dictionary.has('AP')) {
appearance = item._dictionary.get('AP');
if (appearance) {
if (appearance.has('N')) {
_removeReferences(appearance.get('N'), this._crossReference, 'Yes', 'Off');
}
if (appearance.has('D')) {
_removeReferences(appearance.get('D'), this._crossReference, 'Yes', 'Off');
}
}
_removeDuplicateReference(appearance, this._crossReference, 'N');
_removeDuplicateReference(appearance, this._crossReference, 'D');
}
else {
var reference = this._crossReference._getNextReference();
appearance = new _PdfDictionary(this._crossReference);
this._crossReference._cacheMap.set(reference, appearance);
item._dictionary.update('AP', reference);
}
var normalChecked = this._createAppearance(item, _PdfCheckFieldState.checked);
var normalCheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(normalCheckedReference, normalChecked._content);
var normalUnchecked = this._createAppearance(item, _PdfCheckFieldState.unchecked);
var normalUncheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(normalUncheckedReference, normalUnchecked._content);
var normalDictionary = new _PdfDictionary(this._crossReference);
if (itemValue !== null && typeof itemValue !== 'undefined') {
normalDictionary.update(itemValue, normalCheckedReference);
}
else {
normalDictionary.update('Yes', normalCheckedReference);
}
normalDictionary.update('Off', normalUncheckedReference);
var normalReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(normalReference, normalDictionary);
appearance.update('N', normalReference);
var pressChecked = this._createAppearance(item, _PdfCheckFieldState.pressedChecked);
var pressCheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pressCheckedReference, pressChecked._content);
var pressUnchecked = this._createAppearance(item, _PdfCheckFieldState.pressedUnchecked);
var pressUncheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pressUncheckedReference, pressUnchecked._content);
var pressedDictionary = new _PdfDictionary(this._crossReference);
if (itemValue !== null && typeof itemValue !== 'undefined') {
pressedDictionary.update(itemValue, pressCheckedReference);
}
else {
pressedDictionary.update('Yes', pressCheckedReference);
}
pressedDictionary.update('Off', pressUncheckedReference);
var pressedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pressedReference, pressedDictionary);
appearance.update('D', pressedReference);
item._dictionary._updated = true;
};
return PdfCheckBoxField;
}(PdfField));
export { PdfCheckBoxField };
/**
* `PdfRadioButtonListField` class represents the radio button field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new radio button list field
* let field: PdfRadioButtonListField = new PdfRadioButtonListField(page, 'Age');
* // Create and add first item
* let first: PdfRadioButtonListItem = field.add('1-9', {x: 100, y: 140, width: 20, height: 20});
* // Create and add second item
* let second: PdfRadioButtonListItem = new PdfRadioButtonListItem('10-49', {x: 100, y: 170, width: 20, height: 20}, page);
* field.add(second);
* // Sets selected index of the radio button list field
* field.selectedIndex = 0;
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfRadioButtonListField = /** @class */ (function (_super) {
__extends(PdfRadioButtonListField, _super);
function PdfRadioButtonListField(page, name, properties) {
var _this = _super.call(this) || this;
/**
* Selected radio item index, or -1 if none.
*
* @private
*/
_this._selectedIndex = -1;
/**
* Allows selecting all items in unison.
*
* @private
*/
_this._allowUnisonSelection = false;
/**
* Indicates whether duplicate widgets exist for this field.
*
* @private
*/
_this._hasDuplicates = false;
if (page && name) {
_this._initialize(page, name);
}
if (properties) {
if ('items' in properties && _isNullOrUndefined(properties.items)) {
properties.items.forEach(function (item) {
_this.add(new PdfRadioButtonListItem(item.name, item.bounds, _this));
});
}
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
if ('allowUnisonSelection' in properties && _isNullOrUndefined(properties.allowUnisonSelection)) {
_this.allowUnisonSelection = properties.allowUnisonSelection;
}
if ('selectedIndex' in properties && _isNullOrUndefined(properties.selectedIndex)) {
_this.selectedIndex = properties.selectedIndex;
}
}
return _this;
}
/**
* Parse an existing radio button list field.
*
* @private
* @param {PdfForm} form Form object.
* @param {_PdfDictionary} dictionary Field dictionary.
* @param {_PdfCrossReference} crossReference Cross reference object.
* @param {_PdfReference} reference Field reference.
* @returns {PdfRadioButtonListField} Radio button list field.
*/
PdfRadioButtonListField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfRadioButtonListField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
field._defaultIndex = 0;
field._parsedItems = new Map();
if (field._kidsCount > 0) {
field._retrieveOptionValue();
}
return field;
};
Object.defineProperty(PdfRadioButtonListField.prototype, "checked", {
/**
* Gets the flag indicating whether the field is checked or not (Read only).
*
* @returns {boolean} Checked.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the radio button list field
* let field: PdfRadioButtonListField = form.fieldAt(0) as PdfRadioButtonListField;
* // Gets the flag indicating whether the field is checked or not.
* let checked: boolean = field.checked;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var check = false;
if (this._kidsCount > 0) {
check = this.itemAt(this._defaultIndex).checked;
}
return check;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfRadioButtonListField.prototype, "selectedIndex", {
/**
* Gets the selected item index.
*
* @returns {number} Index.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the radio button list field
* let field: PdfRadioButtonListField = form.fieldAt(0) as PdfRadioButtonListField;
* // Gets the selected index.
* let index: number = field.selectedIndex;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded && this._selectedIndex === -1) {
this._selectedIndex = this._obtainSelectedIndex();
}
return this._selectedIndex;
},
/**
* Sets the selected item index.
*
* @param {number} value Selected index.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new radio button list field
* let field: PdfRadioButtonListField = new PdfRadioButtonListField(page, 'Age');
* // Create and add first item
* let first: PdfRadioButtonListItem = field.add('1-9', {x: 100, y: 140, width: 20, height: 20});
* // Create and add second item
* let second: PdfRadioButtonListItem = new PdfRadioButtonListItem('10-49', {x: 100, y: 170, width: 20, height: 20}, page);
* field.add(second);
* // Sets selected index of the radio button list field
* field.selectedIndex = 0;
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this.selectedIndex !== value) {
var selectedItem = this.itemAt(value);
this._hasDuplicates = this._hasDuplicateItems();
var isAllowUnison = false;
var isUpdatedWithValue = false;
if (!this._isLoaded) {
isAllowUnison = (this.allowUnisonSelection && this._isUserRequired) ? true : false;
}
else {
isAllowUnison = this.allowUnisonSelection;
if (this.selectedIndex >= 0) {
var selectedIndexItem = this.itemAt(this.selectedIndex);
if (selectedIndexItem && selectedIndexItem._dictionary && selectedIndexItem._dictionary.has('AS')) {
var selectedVal = selectedIndexItem._dictionary.get('AS').name;
if (selectedVal === selectedIndexItem.value) {
isUpdatedWithValue = true;
}
}
}
}
for (var i = 0; i < this._kidsCount; i++) {
var item = this.itemAt(i);
if ((isAllowUnison && !this._isLoaded) || (isAllowUnison && isUpdatedWithValue && this._isLoaded)) {
if (item.value === selectedItem.value) {
item._dictionary.update('AS', _PdfName.get(item.value));
this._dictionary.update('V', _PdfName.get(item.value));
this._dictionary.update('DV', _PdfName.get(item.value));
}
else {
item._dictionary.update('AS', _PdfName.get('Off'));
}
}
else {
if (i === value) {
if (!this._hasDuplicates) {
item._dictionary.update('AS', _PdfName.get(item.value));
this._dictionary.update('V', _PdfName.get(item.value));
this._dictionary.update('DV', _PdfName.get(item.value));
}
else {
item._dictionary.update('AS', _PdfName.get(i.toString()));
this._dictionary.update('V', _PdfName.get(i.toString()));
this._dictionary.update('DV', _PdfName.get(i.toString()));
}
}
else {
item._dictionary.update('AS', _PdfName.get('Off'));
}
}
}
this._selectedIndex = value;
}
},
enumerable: true,
configurable: true
});
/**
* Determines whether the radio button list contains any duplicate item values.
* Scans the current items and reports if any `value` occurs more than once.
*
* @private
* @returns {boolean} Returns `true` if at least one item `value` is duplicated; otherwise, `false`.
*/
PdfRadioButtonListField.prototype._hasDuplicateItems = function () {
if (this._kidsCount > 2 || this.itemsCount > 2) {
var seenValues = new Set();
var duplicateValues = new Set();
for (var j = 0; j < this._kidsCount; j++) {
var items = this.itemAt(j);
if (seenValues.has(items.value)) {
if (!duplicateValues.has(items.value)) {
duplicateValues.add(items.value);
}
}
else {
seenValues.add(items.value);
}
}
return duplicateValues.size > 0;
}
return false;
};
Object.defineProperty(PdfRadioButtonListField.prototype, "allowUnisonSelection", {
/**
* Gets a value that specifies whether multiple radio buttons in the same group can be selected simultaneously within the form.
*
* @returns {boolean} Indicates if unison selection is enabled.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access the form
* let form: PdfForm = document.form;
* // Gets the value indicating if unison selection is enabled
* let isUnisonSelectionEnabled: boolean = form.allowUnisonSelection;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if ((this._fieldFlags & _FieldFlag.radiosInUnison) !== 0) {
this._allowUnisonSelection = true;
}
return this._allowUnisonSelection;
},
/**
* Sets a value that specifies whether multiple radio buttons in the same group can be selected simultaneously within the form.
*
* @param {boolean} value Enable or disable unison selection. The default value is false.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access the form
* let form: PdfForm = document.form;
* // Disable the unison selection.
* form.allowUnisonSelection = false;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value) {
this._fieldFlags |= _FieldFlag.radiosInUnison;
}
this._isUserRequired = value;
this._allowUnisonSelection = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfRadioButtonListField.prototype, "borderColor", {
/**
* Gets the border color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the border color of the field.
* let borderColor: PdfColor = field.borderColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBorderColor(!this._isLoaded);
},
/**
* Sets the border color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Sets the border color of the field.
* field.borderColor = {r: 255, g: 0, b: 0};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBorderColor(value, true);
},
enumerable: true,
configurable: true
});
/**
* Gets the item at the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the radio button list field
* let field: PdfRadioButtonListField = form.fieldAt(0) as PdfRadioButtonListField;
* // Gets the first list item.
* let item: PdfRadioButtonListField = field.itemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Index of the field item.
* @returns {PdfRadioButtonListItem} Field item at the index.
*/
PdfRadioButtonListField.prototype.itemAt = function (index) {
if (index < 0 || (index !== 0 && index >= this._kidsCount)) {
throw Error('Index out of range.');
}
var item;
if (this._parsedItems.has(index)) {
item = this._parsedItems.get(index);
}
else {
var dictionary = void 0;
if (index >= 0 && this._kids && this._kids.length > 0 && index < this._kids.length) {
var ref = this._kids[index];
if (ref && ref instanceof _PdfReference) {
dictionary = this._crossReference._fetch(ref);
}
if (dictionary) {
item = PdfRadioButtonListItem._load(dictionary, this._crossReference, this);
item._ref = ref;
item._index = index;
this._parsedItems.set(index, item);
}
}
}
return item;
};
PdfRadioButtonListField.prototype.add = function (value, bounds) {
if (value instanceof PdfRadioButtonListItem) {
value._field = this;
value._dictionary.update('Parent', this._ref);
value._setField(this);
return this._kidsCount;
}
else {
return new PdfRadioButtonListItem(value, bounds, this);
}
};
/**
* Remove the radio button list item from the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Remove the first item of the form field
* field.removeItemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Item index to remove.
* @returns {void} Nothing.
*/
PdfRadioButtonListField.prototype.removeItemAt = function (index) {
var item = this.itemAt(index);
if (item && item._ref) {
var page = item._getPage();
if (page) {
page._removeAnnotation(item._ref);
}
this._kids.splice(index, 1);
this._dictionary.set('Kids', this._kids);
this._dictionary._updated = true;
this._parsedItems.delete(index);
if (this._parsedItems.size > 0) {
var parsedItems_2 = new Map();
this._parsedItems.forEach(function (value, key) {
if (key > index) {
parsedItems_2.set(key - 1, value);
}
else {
parsedItems_2.set(key, value);
}
});
this._parsedItems = parsedItems_2;
}
if (this._dictionary.has('Opt')) {
var options = this._dictionary.getArray('Opt');
if (options && options.length > 0) {
options.splice(index, 1);
this._dictionary.set('Opt', options);
}
}
}
};
/**
* Remove the specified radio button list field item.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Remove the first item of the form field
* field.removeItem(field.itemAt(0));
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {PdfRadioButtonListItem} item Item to remove.
* @returns {void} Nothing.
*/
PdfRadioButtonListField.prototype.removeItem = function (item) {
if (item && item._ref) {
var index = this._kids.indexOf(item._ref);
if (index !== -1) {
this.removeItemAt(index);
}
}
};
/**
* Initializes the radio button field on the specified page with the given name,
* creating its dictionary, reference, and default flags.
*
* @private
* @param {PdfPage} page The page on which the field is initialized.
* @param {string} name The field name to assign to the radio button group.
* @returns {void}
*/
PdfRadioButtonListField.prototype._initialize = function (page, name) {
this._defaultIndex = 0;
this._crossReference = page._crossReference;
this._page = page;
this._name = name;
this._dictionary = new _PdfDictionary(this._crossReference);
this._ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(this._ref, this._dictionary);
this._dictionary.objId = this._ref.toString();
this._dictionary.update('FT', _PdfName.get('Btn'));
this._dictionary.update('T', name);
this._parsedItems = new Map();
this._fieldFlags |= _FieldFlag.radio;
};
/**
* Extracts and assigns option values from the field dictionary's `Opt` entry
* to corresponding radio button items, if present.
*
* @private
* @returns {void}
*/
PdfRadioButtonListField.prototype._retrieveOptionValue = function () {
if (this._dictionary.has('Opt')) {
var options = this._dictionary.getArray('Opt');
if (options && options.length > 0) {
var itemsCount = this._kidsCount;
var count = options.length <= itemsCount ? options.length : itemsCount;
for (var i = 0; i < count; i++) {
if (options[i]) {
this.itemAt(i)._optionValue = options[i];
}
}
}
}
};
/**
* Determines the selected radio button index by inspecting inherited field value (`V`)
* and the item's appearance state (`AS`), ignoring the `Off` state.
*
* @private
* @returns {number} Returns the zero-based index of the selected item, or `-1` if none is selected.
*/
PdfRadioButtonListField.prototype._obtainSelectedIndex = function () {
var index = -1;
for (var i = 0; i < this._kidsCount; ++i) {
var item = this.itemAt(i);
if (item) {
var checkName = _getInheritableProperty(item._dictionary, 'V', false, true, 'Parent');
if (checkName && item._dictionary.has('AS')) {
var asName = item._dictionary.get('AS');
if (asName && asName.name.toLowerCase() !== 'off') {
if (checkName instanceof _PdfName && checkName.name.toLowerCase() !== 'off') {
if (asName.name === checkName.name || item._optionValue === checkName.name) {
index = i;
break;
}
}
else if (typeof checkName === 'string' && checkName.toLowerCase() !== 'off') {
if (asName.name === checkName || item._optionValue === checkName) {
index = i;
break;
}
}
}
}
}
}
return index;
};
/**
* Finalizes item appearances and dictionary updates after selection changes or load,
* optionally flattening the field to static content.
*
* @private
* @param {boolean} [isFlatten=false] When `true`, draws static appearances and prevents further updates.
* @returns {void}
*/
PdfRadioButtonListField.prototype._doPostProcess = function (isFlatten) {
if (isFlatten === void 0) { isFlatten = false; }
this._hasDuplicates = this._hasDuplicateItems();
var count = this._kidsCount;
if (this._isLoaded) {
if (count > 0) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item && !this._checkFieldFlag(item._dictionary)) {
if (isFlatten) {
var template = void 0;
var state = this.selectedIndex === i ?
_PdfCheckFieldState.checked :
_PdfCheckFieldState.unchecked;
if (this._setAppearance || this._form._setAppearance || !item._dictionary.has('AP')) {
template = this._createAppearance(item, state);
}
else {
template = _getStateTemplate(state, item);
}
this._drawTemplate(template, item._getPage(), item.bounds);
}
else if (this._setAppearance || this._form._setAppearance || !item._isLoaded) {
item._postProcess(this.allowUnisonSelection ? (this.selectedIndex >= 0 && item.value === this.itemAt(this.selectedIndex).value ? item.value : 'Off') :
this._hasDuplicates ? (this.selectedIndex === i ? i.toString() : 'Off') : (this.selectedIndex === i ? item.value : 'Off'));
this._drawAppearance(item);
}
item._dictionary._updated = !isFlatten;
}
}
}
else {
var style = this.selectedIndex !== -1 ?
_PdfCheckFieldState.checked :
_PdfCheckFieldState.unchecked;
this._drawTemplate(_getStateTemplate(style, this), this.page, this.bounds);
}
}
else {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
var state = this.selectedIndex === i ? _PdfCheckFieldState.checked : _PdfCheckFieldState.unchecked;
if (!this._isDuplicatePage) {
item._dictionary.update('AS', _PdfName.get(this.selectedIndex === i ? item.value : 'Off'));
}
if (isFlatten) {
var template = this._createAppearance(item, state);
this._drawTemplate(template, item._getPage(), item.bounds);
}
else if (!this._isDuplicatePage) {
item._postProcess((this.allowUnisonSelection && this._isUserRequired) ? (this.selectedIndex >= 0 && item.value === this.itemAt(this.selectedIndex).value ? item.value : 'Off') :
this._hasDuplicates ? (this.selectedIndex === i ? i.toString() : 'Off') : (this.selectedIndex === i ? item.value : 'Off'));
this._drawAppearance(item);
}
item._dictionary._updated = !isFlatten;
}
}
this._dictionary._updated = !isFlatten;
};
/**
* Builds a visual appearance template for a radio button item based on the specified state,
* applying border, background, color, rotation, and style.
*
* @private
* @param {PdfRadioButtonListItem} widget The radio button item for which to create the appearance.
* @param {_PdfCheckFieldState} state The visual state to render (e.g., checked, unchecked, pressed).
* @returns {PdfTemplate} The generated appearance template for the requested state.
*/
PdfRadioButtonListField.prototype._createAppearance = function (widget, state) {
var bounds = widget.bounds;
var parameter = new _PaintParameter();
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
var backcolor = widget.backColor;
if (backcolor) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(widget.color);
var border = widget.border;
if (widget.borderColor) {
parameter.borderPen = new PdfPen(widget.borderColor, border.width);
}
parameter.borderWidth = border.width;
parameter.borderStyle = border.style;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = [shadowColor[0] >= 0 ? shadowColor[0] : 0,
shadowColor[1] >= 0 ? shadowColor[1] : 0,
shadowColor[2] >= 0 ? shadowColor[2] : 0];
parameter.shadowBrush = new PdfBrush({ r: color[0], g: color[1], b: color[2] });
}
parameter.rotationAngle = widget.rotate;
var template = new PdfTemplate(parameter.bounds, this._crossReference);
var graphics = template.graphics;
if (widget._styleText) {
this._drawRadioButton(graphics, parameter, widget._styleText, state);
}
else {
this._drawRadioButton(graphics, parameter, _styleToString(widget.style), state);
}
return template;
};
/**
* Updates the item's appearance streams (`AP`) for normal (`N`) and pressed (`D`) states,
* generating and wiring templates for the actual value and `Off`.
*
* @private
* @param {PdfRadioButtonListItem} item The radio button item whose appearance is to be updated.
* @returns {void}
*/
PdfRadioButtonListField.prototype._drawAppearance = function (item) {
var appearance = new _PdfDictionary();
if (item._dictionary.has('AP')) {
appearance = item._dictionary.get('AP');
if (appearance) {
if (appearance.has('N')) {
_removeReferences(appearance.get('N'), this._crossReference, item.value, 'Off');
}
if (appearance.has('D')) {
_removeReferences(appearance.get('D'), this._crossReference, item.value, 'Off');
}
}
_removeDuplicateReference(appearance, this._crossReference, 'N');
_removeDuplicateReference(appearance, this._crossReference, 'D');
}
else {
var reference = this._crossReference._getNextReference();
appearance = new _PdfDictionary(this._crossReference);
this._crossReference._cacheMap.set(reference, appearance);
item._dictionary.update('AP', reference);
}
var normalChecked = this._createAppearance(item, _PdfCheckFieldState.checked);
var normalCheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(normalCheckedReference, normalChecked._content);
var normalUnchecked = this._createAppearance(item, _PdfCheckFieldState.unchecked);
var normalUncheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(normalUncheckedReference, normalUnchecked._content);
var normalDictionary = new _PdfDictionary(this._crossReference);
var itemField = item._field;
var actualValue;
if (!this._isLoaded) {
actualValue = (this.allowUnisonSelection && this._isUserRequired && itemField.allowUnisonSelection) ?
item.value
: (this._hasDuplicates ? (item._index).toString() : item.value);
}
else {
actualValue = itemField.allowUnisonSelection
? item.value
: (this._hasDuplicates ? (item._index).toString() : item.value);
}
if (!actualValue && item._enableGrouping) {
actualValue = 'check' + item._index;
}
normalDictionary.update(actualValue, normalCheckedReference);
normalDictionary.update('Off', normalUncheckedReference);
var normalReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(normalReference, normalDictionary);
appearance.update('N', normalReference);
var pressChecked = this._createAppearance(item, _PdfCheckFieldState.pressedChecked);
var pressCheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pressCheckedReference, pressChecked._content);
var pressUnchecked = this._createAppearance(item, _PdfCheckFieldState.pressedUnchecked);
var pressUncheckedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pressUncheckedReference, pressUnchecked._content);
var pressedDictionary = new _PdfDictionary(this._crossReference);
pressedDictionary.update(actualValue, pressCheckedReference);
pressedDictionary.update('Off', pressUncheckedReference);
var pressedReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(pressedReference, pressedDictionary);
appearance.update('D', pressedReference);
item._dictionary._updated = true;
};
return PdfRadioButtonListField;
}(PdfField));
export { PdfRadioButtonListField };
/**
* Represents the base class for list box and combo box fields.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxField: PdfListField = form.fieldAt(0) as PdfListField;
* // Gets the count of the loaded combo box field items.
* let comboItemsCount: number = comboBoxField.itemsCount;
* // Access the list box field
* let listBoxField: PdfListField = form.fieldAt(1) as PdfListField;
* // Gets the count of the loaded list box field items.
* let ListItemsCount: number = listBoxField.itemsCount;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfListField = /** @class */ (function (_super) {
__extends(PdfListField, _super);
function PdfListField() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(PdfListField.prototype, "itemsCount", {
/**
* Gets the count of the loaded field items (Read only).
*
* @returns {number} Items count.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(0) as PdfComboBoxField;
* // Gets the count of the loaded combo box field items.
* let comboItemsCount: number = comboBoxField.itemsCount;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(1) as PdfListBoxField;
* // Gets the count of the loaded list box field items.
* let ListItemsCount: number = listBoxField.itemsCount;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._options.length;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "bounds", {
/**
* Gets the bounds.
*
* @returns {Rectangle} Bounds.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(0) as PdfComboBoxField;
* // Gets the bounds of combo box field.
* let comboBoxBounds: Rectangle = comboBoxField.bounds;
* // Access the combo box field
* let listBoxField: PdfListBoxField = form.fieldAt(1) as PdfListBoxField;
* // Gets the bounds of list box field.
* let listBoxBounds: Rectangle = listBoxField.bounds;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var value;
var widget = this.itemAt(this._defaultIndex);
if (widget) {
widget._page = this.page;
}
if (widget && widget.bounds) {
value = widget.bounds;
}
else if (this._dictionary.has('Rect')) {
value = _calculateBounds(this._dictionary, this.page);
}
if (value) {
return value;
}
else if (this._bounds) {
return this._bounds;
}
return value;
},
/**
* Sets the bounds.
*
* @param {Rectangle} value bounds.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(0) as PdfComboBoxField;
* // Sets the bounds of combo box field.
* comboBoxField.bounds = {x: 10, y: 10, width: 100, height: 30};
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(1) as PdfListBoxField;
* // Sets the bounds of list box field.
* listBoxField.bounds = {x: 10, y: 50, width: 100, height: 30};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value.x === 0 && value.y === 0 && value.width === 0 && value.height === 0) {
throw new Error('Cannot set empty bounds');
}
var widget = this.itemAt(this._defaultIndex);
if (this._isLoaded) {
if (typeof widget === 'undefined' || this._dictionary.has('Rect')) {
this._dictionary.update('Rect', _getUpdatedBounds([value.x, value.y, value.width, value.height], this.page));
}
else {
widget._page = this.page;
widget.bounds = value;
}
}
else {
if (widget) {
widget._page = this.page;
widget.bounds = value;
}
else {
this._bounds = value;
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "selectedIndex", {
/**
* Gets the selected item index or indexes.
*
* @returns {number | number[]} Index.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxfield: PdfComboBoxField = form.fieldAt(0) as PdfComboBoxField;
* // Gets the selected item index or indexes from combo box field.
* let comboBoxIndex: number = comboBoxfield.selectedIndex;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(1) as PdfListBoxField;
* // Gets the selected item index or indexes from list box field.
* let listBoxIndex: number = listBoxField.selectedIndex;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var value = this._dictionary.get('I');
if (typeof value === 'undefined') {
return [];
}
else {
if (value.length === 1) {
return value[0];
}
else {
return value;
}
}
},
/**
* Sets the selected item index or indexes.
*
* @param {number | number[]} value Selected index.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new list box field
* let listField: PdfListField = new PdfListBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* listField.addItem(new PdfListFieldItem('English', 'English'));
* listField.addItem(new PdfListFieldItem('French', 'French'));
* listField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* listField.selectedIndex = 2;
* // Sets the flag indicates whether the list box allows multiple selections.
* listField.multiSelect = true;
* // Add the field into PDF form
* form.add(listField);
* // Create a new combo box field
* let comboField: PdfComboBoxField = new PdfComboBoxField(page, 'list1', {x: 100, y: 160, width: 100, height: 50});
* // Add list items to the field.
* comboField.addItem(new PdfListFieldItem('English', 'English'));
* comboField.addItem(new PdfListFieldItem('French', 'French'));
* comboField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* comboField.selectedIndex = 2;
* // Sets the flag indicates whether the combo box allows multiple selections.
* comboField.multiSelect = true;
* // Add the field into PDF form
* form.add(comboField);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var _this = this;
var length = this._options.length;
if (typeof value === 'number') {
this._checkIndex(value, length);
this._dictionary.update('I', [value]);
this._dictionary.update('V', [this._options[value][0]]);
}
else {
var values_1 = [];
value.forEach(function (entry) {
_this._checkIndex(entry, length);
values_1.push(_this._options[entry][0]);
});
this._dictionary.update('I', value);
this._dictionary.update('V', values_1);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "selectedValue", {
/**
* Gets the selected item value or values.
*
* @returns {string | string[]} Selected values.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(0) as PdfListBoxField;
* // Gets the selected item value or values from list box field.
* if (listBoxField.multiSelect) {
* let listBoxValues: string[]; = listBoxField.selectedValue;
* } else {
* let listBoxValues: string = listBoxField.selectedValue;
* }
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
var _this = this;
var values = [];
if (this._dictionary && this._dictionary.has('V')) {
var value = this._dictionary.getArray('V'); // eslint-disable-line
if (typeof value !== 'undefined') {
if (Array.isArray(value)) {
values.push.apply(values, value);
}
else if (typeof value === 'string') {
values.push(value);
}
}
}
if (values.length === 0 && this._dictionary && this._dictionary.has('I')) {
var value = this._dictionary.get('I');
if (value && value.length > 0) {
values.push.apply(values, value.map(function (index) { return _this._options[index][0]; }));
}
}
if (values.length === 1) {
return values[0];
}
else {
return values;
}
},
/**
* Sets the selected item value or values.
*
* @param {string | string[]} value Selected values.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new list box field
* let listField: PdfListField = new PdfListBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* listField.addItem(new PdfListFieldItem('English', 'English'));
* listField.addItem(new PdfListFieldItem('French', 'French'));
* listField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the flag indicates whether the list box allows multiple selections.
* listField.multiSelect = true;
* // Sets the selected values
* listField.selectedValue = ['English', 'German'];
* // Add the field into PDF form
* form.add(listField);
* // Create a new combo box field
* let comboField: PdfComboBoxField = new PdfComboBoxField(page, 'list1', {x: 100, y: 160, width: 100, height: 50});
* // Add list items to the field.
* comboField.addItem(new PdfListFieldItem('English', 'English'));
* comboField.addItem(new PdfListFieldItem('French', 'French'));
* comboField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected value
* comboField.selectedValue = ['French'];
* // Add the field into PDF form
* form.add(comboField);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
var _this = this;
if (typeof value === 'string') {
var index = this._tryGetIndex(value);
if (index !== -1) {
this._dictionary.update('I', [index]);
this._dictionary.update('V', [value]);
}
}
else {
var values_2 = [];
var indices_1 = [];
value.forEach(function (entry) {
var index = _this._tryGetIndex(entry);
if (index !== -1) {
indices_1.push(index);
values_2.push(entry);
}
});
if (values_2.length > 0) {
this._dictionary.update('I', indices_1);
this._dictionary.update('V', values_2);
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "multiSelect", {
/**
* Gets the flag indicates whether the list field allows multiple selections.
*
* @returns {boolean} Value indicates whether the list field allows multiple selections.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(0) as PdfComboBoxField;
* // Gets the flag indicates whether the combo box allows multiple selections.
* let comboBoxFlag: Boolean = comboBoxField.multiSelect;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(1) as PdfListBoxField;
* // Gets the flag indicates whether the list box allows multiple selections.
* let listBoxFlag: boolean = listBoxField.multiSelect;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded) {
return (this._fieldFlags & _FieldFlag.multiSelect) !== 0;
}
else {
return this._multiSelect;
}
},
/**
* Sets the flag indicates whether the list field allows multiple selections.
*
* @param {boolean} value Indicates whether the list field allows multiple selections.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new list box field
* let listField: PdfListField = new PdfListBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* listField.addItem(new PdfListFieldItem('English', 'English'));
* listField.addItem(new PdfListFieldItem('French', 'French'));
* listField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* listField.selectedIndex = 2;
* // Sets the flag indicates whether the list box allows multiple selections.
* listField.multiSelect = true;
* // Add the field into PDF form
* form.add(listField);
* // Create a new combo box field
* let comboField: PdfComboBoxField = new PdfComboBoxField(page, 'list1', {x: 100, y: 160, width: 100, height: 50});
* // Add list items to the field.
* comboField.addItem(new PdfListFieldItem('English', 'English'));
* comboField.addItem(new PdfListFieldItem('French', 'French'));
* comboField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* comboField.selectedIndex = 2;
* // Sets the flag indicates whether the combo box allows multiple selections.
* comboField.multiSelect = true;
* // Add the field into PDF form
* form.add(comboField);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this.multiSelect !== value) {
this._multiSelect = value;
if (value) {
this._fieldFlags |= _FieldFlag.multiSelect;
}
else {
this._fieldFlags &= ~_FieldFlag.multiSelect;
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "editable", {
/**
* Gets the flag indicates whether the list field is editable.
*
* @returns {boolean} Value indicates whether the list field is editable.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(0) as PdfComboBoxField;
* // Gets the flag indicates whether the combo box is editable.
* let comboBoxFlag: Boolean = comboBoxField.editable;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(1) as PdfListBoxField;
* // Gets the flag indicates whether the list box is editable.
* let listBoxFlag: boolean = listBoxField.editable;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._isLoaded) {
return (this._fieldFlags & _FieldFlag.edit) !== 0;
}
else {
return this._editable;
}
},
/**
* Sets the flag indicates whether the list field is editable.
*
* @param {boolean} value Indicates whether the list field is editable.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new list box field
* let listField: PdfListField = new PdfListBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* listField.addItem(new PdfListFieldItem('English', 'English'));
* listField.addItem(new PdfListFieldItem('French', 'French'));
* listField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* listField.selectedIndex = 2;
* // Sets the flag indicates whether the list box is editable.
* listField.editable = true;
* // Add the field into PDF form
* form.add(listField);
* // Create a new combo box field
* let comboField: PdfComboBoxField = new PdfComboBoxField(page, 'list1', {x: 100, y: 160, width: 100, height: 50});
* // Add list items to the field.
* comboField.addItem(new PdfListFieldItem('English', 'English'));
* comboField.addItem(new PdfListFieldItem('French', 'French'));
* comboField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* comboField.selectedIndex = 2;
* // Sets the flag indicates whether the combo box is editable.
* comboField.editable = true;
* // Add the field into PDF form
* form.add(comboField);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._editable !== value) {
this._editable = value;
if (value) {
this._fieldFlags |= _FieldFlag.edit;
}
else {
this._fieldFlags &= ~_FieldFlag.edit;
}
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "font", {
/**
* Gets the font of the field.
*
* @returns {PdfFont} font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfListBoxField = document.form.fieldAt(0) as PdfListBoxField;
* // Gets the font of the field.
* let font: PdfFont = field.font;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (this._font) {
return this._font;
}
else {
var widget = this.itemAt(this._defaultIndex);
this._font = _obtainFontDetails(this._form, widget, this);
}
return this._font;
},
/**
* Sets the font of the field.
*
* @param {PdfFont} value font.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfListBoxField = document.form.fieldAt(0) as PdfListBoxField;
* // Sets the font of the field
* field.font = document.embedFont(PdfFontFamily.helvetica, 12, PdfFontStyle.bold);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (value && value instanceof PdfFont) {
this._font = value;
this._initializeFont(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "textAlignment", {
/**
* Gets the text alignment in a combo box field.
*
* @returns {PdfTextAlignment} Text alignment.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access combo box field
* let field: PdfComboBoxField = document.form.fieldAt(0) as PdfComboBoxField;
* // Gets the text alignment from combo box field
* let alignment: PdfTextAlignment = field.textAlignment;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._getTextAlignment();
},
/**
* Sets the text alignment in a combo box field.
*
* @param {PdfTextAlignment} value Text alignment.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access combo box field
* let field: PdfComboBoxField = document.form.fieldAt(0) as PdfComboBoxField;
* // Sets the text alignment of form field as center
* field.textAlignment = PdfTextAlignment.center;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
if (this._textAlignment !== value) {
this._setTextAlignment(value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "backColor", {
/**
* Gets the background color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the background color of the field.
* let backColor: PdfColor = field.backColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBackColor(true);
},
/**
* Sets the background color of the field.
*
* @param {PdfColor} value Array with R, G, B, A color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the list field at index 0
* let list1: PdfField = document.form.fieldAt(0);
* // Sets the background color of the field.
* list1.backColor = {r: 255, g: 0, b: 0};
* // Access the list field at index 1
* let list2: PdfField = document.form.fieldAt(1);
* // Sets the background color of the field to transparent.
* list2.backColor = {r: 0, g: 0, b: 0, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBackColor(value, true);
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfListField.prototype, "_options", {
/**
* Gets the option entries (`Opt`) for the radio button field, initializing the
* underlying array in the field dictionary if it does not already exist.
*
* @private
* @returns {Array<string[]>} The array of option entries associated with the field.
*/
get: function () {
if (!this._optionArray) {
if (this._dictionary && this._dictionary.has('Opt')) {
this._optionArray = this._dictionary.getArray('Opt');
}
else {
this._optionArray = [];
this._dictionary.update('Opt', this._optionArray);
}
}
return this._optionArray;
},
enumerable: true,
configurable: true
});
/**
* Gets the item at the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the list box field
* let listBox: PdfListBoxField = form.fieldAt(0) as PdfListBoxField;
* // Gets the first list item.
* let listBoxItem: PdfListFieldItem = listBox.itemAt(0);
* // Access the combo box field
* let comboBox: PdfComboBoxField = form.fieldAt(1) as PdfComboBoxField;
* // Gets the first list item.
* let comboBoxItem: PdfListFieldItem = comboBox.itemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Index of the field item.
* @returns {PdfListFieldItem} Field item at the index.
*/
PdfListField.prototype.itemAt = function (index) {
var item;
if (index < this._kidsCount) {
if (this._parsedItems.has(index)) {
item = this._parsedItems.get(index);
}
else {
var dictionary = void 0;
var reference = this._kids[index];
if (reference && reference instanceof _PdfReference) {
dictionary = this._crossReference._fetch(reference);
}
if (dictionary) {
item = PdfListFieldItem._load(dictionary, this._crossReference, this);
item._index = index;
item._ref = reference;
if (this._options && this._options.length > 0 && index < this._options.length) {
item._text = this._options[index][1];
}
else {
item._text = '';
}
this._parsedItems.set(index, item);
}
}
}
else {
if (this._parsedItems.has(index)) {
item = this._parsedItems.get(index);
}
else if (this._kidsCount > 0 && this._kids && this._kids.length > 0) {
var dictionary = void 0;
var reference = void 0;
if (this._kidsCount === 1) {
reference = this._kids[0];
}
else {
reference = this._kids[index];
}
if (reference && reference instanceof _PdfReference) {
dictionary = this._crossReference._fetch(reference);
}
if (dictionary) {
item = PdfListFieldItem._load(dictionary, this._crossReference, this);
item._index = index;
item._ref = reference;
if (this._options && this._options.length > 0 && index < this._options.length) {
item._text = this._options[index][1];
}
else {
item._text = '';
}
this._parsedItems.set(index, item);
}
}
}
return item;
};
/**
* Add list item.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new list box field
* let listField: PdfListField = new PdfListBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* listField.addItem(new PdfListFieldItem('English', 'English'));
* listField.addItem(new PdfListFieldItem('French', 'French'));
* listField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* listField.selectedIndex = 2;
* // Sets the flag indicates whether the list box allows multiple selections.
* listField.multiSelect = true;
* // Add the field into PDF form
* form.add(listField);
* // Create a new combo box field
* let comboField: PdfComboBoxField = new PdfComboBoxField(page, 'list1', {x: 100, y: 160, width: 100, height: 50});
* // Add list items to the field.
* comboField.addItem(new PdfListFieldItem('English', 'English'));
* comboField.addItem(new PdfListFieldItem('French', 'French'));
* comboField.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* comboField.selectedIndex = 2;
* // Sets the flag indicates whether the combo box allows multiple selections.
* comboField.multiSelect = true;
* // Add the field into PDF form
* form.add(comboField);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {PdfListFieldItem} item Item to add.
* @returns {number} Index of the field item.
*/
PdfListField.prototype.addItem = function (item) {
this._addToOptions(item, this);
return this._listValues.length - 1;
};
/**
* Remove the list item from the specified index.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(0) as PdfListBoxField;
* // Remove the list item from the list box field
* listBoxField.removeItemAt(1);
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(1) as PdfComboBoxField;
* // Remove the list item from the combo box field
* comboBoxField.removeItemAt(0);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {number} index Item index to remove.
* @returns {void} Nothing.
*/
PdfListField.prototype.removeItemAt = function (index) {
var item = this.itemAt(index);
if (item && item._ref) {
this._parsedItems.delete(index);
if (this._parsedItems.size > 0) {
var parsedItems_3 = new Map();
this._parsedItems.forEach(function (value, key) {
if (key > index) {
parsedItems_3.set(key - 1, value);
}
else {
parsedItems_3.set(key, value);
}
});
this._parsedItems = parsedItems_3;
}
if (this._dictionary && this._dictionary.has('Opt')) {
var options = this._options;
if (options && options.length > 0) {
options.splice(index, 1);
this._dictionary.set('Opt', options);
this._optionArray = options;
this._dictionary._updated = true;
}
}
}
};
/**
* Remove the list item.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Access the list box field
* let listBoxField: PdfListBoxField = form.fieldAt(0) as PdfListBoxField;
* // Remove the list item from the list box field
* listBoxField.removeItem(listBoxField.itemAt(1));
* // Access the combo box field
* let comboBoxField: PdfComboBoxField = form.fieldAt(1) as PdfComboBoxField;
* // Remove the list item from the combo box field
* comboBoxField.removeItem(comboBoxField.itemAt(0));
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {PdfListFieldItem} item Item to remove.
* @returns {void} Nothing.
*/
PdfListField.prototype.removeItem = function (item) {
if (item && item.text) {
var index = void 0;
for (var i = 0; i < this.itemsCount; i++) {
var fieldItem = this.itemAt(i);
if (fieldItem && item === fieldItem && fieldItem.text === item.text) {
index = i;
break;
}
}
if (index !== -1) {
this.removeItemAt(index);
}
}
};
/* eslint-disable */
/**
* Initializes the list/choice field on the specified page with the given name and bounds,
* creating its dictionary, reference, kids collection, and a default widget.
*
* @private
* @param {PdfPage} page The page on which the field is initialized.
* @param {string} name The field name to assign to the list/choice field.
* @param {{x: number, y: number, width: number, height: number}} bounds The field bounds in page coordinates. // eslint-disable-line
* @returns {void}
*/
PdfListField.prototype._initialize = function (page, name, bounds) {
this._defaultIndex = 0;
this._crossReference = page._crossReference;
this._page = page;
this._name = name;
this._dictionary = new _PdfDictionary(this._crossReference);
this._ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(this._ref, this._dictionary);
this._dictionary.objId = this._ref.toString();
this._dictionary.update('FT', _PdfName.get('Ch'));
this._dictionary.update('T', name);
this._parsedItems = new Map();
this._listValues = [];
this._kids = [];
this.bounds = bounds;
this._addEmptyWidget();
};
/**
* Resolves and returns the effective font for the field or a specific item by inspecting
* style entries (`DS`) or default appearance (`DA`), and, if needed, loads an embedded font
* from document resources.
*
* @private
* @param {PdfListFieldItem} [item] The item whose font should be resolved; when omitted, resolves at the field level.
* @returns {PdfFont} The resolved PDF font instance.
*/
PdfListField.prototype._obtainFont = function (item) {
var fontFamily = '';
var fontSize = 1;
if (item && (item._dictionary.has('DS') || item._dictionary.has('DA'))) {
if (item._dictionary.has('DS')) {
var collection = item._dictionary.get('DS').split(';');
collection.forEach(function (entryString) {
var entry = entryString.split(':');
if (entryString.indexOf('font-family') !== -1) {
fontFamily = entry[1];
}
else if (entryString.indexOf('font-size') !== -1) {
if (entry[1].endsWith('pt')) {
fontSize = Number.parseFloat(entry[1].replace('pt', ''));
}
}
else if (entryString.indexOf('font-style') === -1 && entryString.indexOf('font') !== -1) {
var name_6 = entry[1];
var split = name_6.split(' ');
split.forEach(function (splitEntry) {
if (splitEntry !== '' && !splitEntry.endsWith('pt')) {
fontFamily += splitEntry + ' ';
}
if (splitEntry.endsWith('pt')) {
fontSize = Number.parseFloat(splitEntry.replace('pt', ''));
}
});
while (fontFamily !== ' ' && fontFamily.endsWith(' ')) {
fontFamily = fontFamily.substring(0, fontFamily.length - 2);
}
if (fontFamily.indexOf(',') !== -1) {
fontFamily = fontFamily.split(',')[0];
}
}
});
}
else {
var value = item._dictionary.get('DA');
if (value && value !== '' && value.indexOf('Tf') !== -1) {
var textCollection_1 = value.split(' ');
textCollection_1.forEach(function (text, index) {
if (text.indexOf('Tf') !== -1) {
fontFamily = textCollection_1[index - 2];
while (fontFamily !== '' && fontFamily.length > 1 && fontFamily[0] === '/') {
fontFamily = fontFamily.substring(1);
}
fontSize = Number.parseFloat(textCollection_1[index - 1]);
}
});
var height = 0.0;
if (fontSize === 0) {
var font = new PdfStandardFont(PdfFontFamily.helvetica, height);
if (font !== null) {
height = this._getFontHeight(font._fontFamily);
if (Number.isNaN(height) || height === 0) {
height = 12;
}
font._size = height;
fontSize = height;
}
}
}
}
fontFamily = fontFamily.trim();
if (fontFamily) {
fontFamily = _decodeFontFamily(fontFamily);
}
switch (fontFamily) {
case 'Helv':
this._font = new PdfStandardFont(PdfFontFamily.helvetica, fontSize, PdfFontStyle.regular);
break;
case 'Courier':
case 'Cour':
this._font = new PdfStandardFont(PdfFontFamily.courier, fontSize, PdfFontStyle.regular);
break;
case 'Symb':
this._font = new PdfStandardFont(PdfFontFamily.symbol, fontSize, PdfFontStyle.regular);
break;
case 'TiRo':
this._font = new PdfStandardFont(PdfFontFamily.timesRoman, fontSize, PdfFontStyle.regular);
break;
case 'ZaDb':
this._font = new PdfStandardFont(PdfFontFamily.zapfDingbats, fontSize, PdfFontStyle.regular);
break;
default:
this._font = new PdfStandardFont(PdfFontFamily.helvetica, fontSize, PdfFontStyle.regular);
break;
}
if (this._font && this._font._dictionary && this._font._dictionary.has('BaseFont')) {
var fontName = this._font._dictionary.get('BaseFont').name;
if (fontName && fontName !== fontFamily) {
if (this.form._dictionary.has('DR')) {
var resources = this.form._dictionary.get('DR');
var fonts = resources.get('Font');
if (fonts) {
if (fonts.has(fontFamily)) {
var fontDictionary = fonts.get(fontFamily);
var fontSubtType = fontDictionary.get('Subtype').name; // eslint-disable-line
if (fontDictionary && fontFamily && fontDictionary.has('BaseFont')) {
var baseFont = fontDictionary.get('BaseFont');
var textFontStyle = PdfFontStyle.regular;
if (baseFont && baseFont.name !== null && typeof baseFont.name !== 'undefined') {
textFontStyle = _getFontStyle(baseFont.name);
if (fontSubtType && fontSubtType === 'TrueType') {
var fontData = _createFontStream(this.form, fontDictionary);
if (fontData && fontData.length > 0) {
var base64String = _encode(fontData);
if (base64String && base64String.length > 0) {
this._font = new PdfTrueTypeFont(base64String, fontSize, textFontStyle);
}
}
}
else if (fontSubtType && fontSubtType === 'Type0') {
var fontData = _getFontFromDescriptor(fontDictionary);
if (fontData && fontData.length > 0) {
var base64String = _encode(fontData);
if (base64String && base64String.length > 0) {
this._font = new PdfTrueTypeFont(base64String, fontSize, textFontStyle);
}
}
}
}
}
}
}
}
}
}
}
return this._font;
};
/**
* Retrieves the selected export value(s) for the list/choice field, preferring the `V`
* entry and falling back to the index array `I` mapped through the field `Opt` entries.
*
* @private
* @returns {string[]} An array of selected export values; returns an empty array if none.
*/
PdfListField.prototype._obtainSelectedValue = function () {
var _this = this;
var result = [];
if (this._dictionary.has('V')) {
var primitive = this._dictionary.get('V'); // eslint-disable-line
var array = this._dictionary.getArray('V'); // eslint-disable-line
if (primitive !== null && typeof primitive !== 'undefined') {
if (typeof primitive === 'string') {
result.push(primitive);
}
else if (Array.isArray(primitive)) {
result.push.apply(result, array);
}
}
}
else {
var selectedIndexes = this._dictionary.get('I');
if (selectedIndexes !== null &&
typeof selectedIndexes !== 'undefined' &&
selectedIndexes.length > 0 &&
selectedIndexes[0] > -1 &&
this._options &&
this._options.length > 0) {
result.push.apply(result, selectedIndexes.map(function (index) { return _this._options[index][0]; }));
}
}
return result;
};
/**
* Finalizes appearances for the field and its widgets, generating or attaching templates
* and optionally flattening the content into the page.
*
* @private
* @param {boolean} [isFlatten=false] When `true`, draws static appearances onto pages and prevents further updates.
* @returns {void}
*/
PdfListField.prototype._doPostProcess = function (isFlatten) {
if (isFlatten === void 0) { isFlatten = false; }
if (isFlatten || this._setAppearance || this._form._setAppearance) {
var count = this._kidsCount;
if (this._kids && this._kids.length > 0) {
if (count > 1) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item && !this._checkFieldFlag(item._dictionary)) {
var template = this._createAppearance(item);
if (isFlatten) {
var page = item._getPage();
if (page) {
this._drawTemplate(template, page, item.bounds);
}
}
else {
this._addAppearance(item._dictionary, template, 'N');
}
item._dictionary._updated = !isFlatten;
}
}
}
else {
var item = this.itemAt(0);
var template = this._createAppearance();
if (isFlatten) {
var page = this.page;
if (page) {
this._drawTemplate(template, page, this.bounds);
}
}
else {
this._addAppearance(item._dictionary, template, 'N');
}
item._dictionary._updated = !isFlatten;
}
}
else if (this._dictionary) {
var template = this._createAppearance();
if (isFlatten) {
var page = this.page;
if (page) {
this._drawTemplate(template, page, this.bounds);
}
}
else {
this._addAppearance(this._dictionary, template, 'N');
}
}
this._dictionary._updated = !isFlatten;
}
};
/**
* Looks up the zero-based option index whose export value matches the specified value.
*
* @private
* @param {string} value The export value to search for within the field options.
* @returns {number} The matching option index, or `-1` if not found.
*/
PdfListField.prototype._tryGetIndex = function (value) {
var index = -1;
if (this._options && this._options.length > 0) {
for (var i = 0; i < this._options.length; i++) {
if (value === this._options[i][0]) {
index = i;
break;
}
}
}
return index;
};
/**
* Adds a default widget annotation to the field with initial appearance-related entries,
* including `MK`, border/background colors, and a default `DA`.
*
* @private
* @returns {void}
*/
PdfListField.prototype._addEmptyWidget = function () {
var widget = new PdfWidgetAnnotation();
widget._create(this._page, this.bounds, this);
this._addToKid(widget);
widget._dictionary.update('MK', new _PdfDictionary(this._crossReference));
widget._mkDictionary.update('BC', [0, 0, 0]);
widget._mkDictionary.update('BG', [1, 1, 1]);
widget._dictionary.update('DA', '/TiRo 0 Tf 0 0 0 rg');
};
/**
* Builds the string format for text layout based on field flags (e.g., multiline)
* and the widget's justification (`Q`) entry.
*
* @private
* @returns {PdfStringFormat} The configured string format including alignment settings.
*/
PdfListField.prototype._getStringFormat = function () {
var widget = this.itemAt(this._defaultIndex);
var stringFormat = new PdfStringFormat();
stringFormat.lineAlignment =
(this._fieldFlags & _FieldFlag.multiLine) > 0
? PdfVerticalAlignment.top
: PdfVerticalAlignment.middle;
if (widget && widget._dictionary.has('Q')) {
var flagValue = widget._dictionary.get('Q');
if (flagValue !== null && typeof flagValue !== 'undefined') {
stringFormat.alignment = flagValue;
}
}
return stringFormat;
};
return PdfListField;
}(PdfField));
export { PdfListField };
/**
* `PdfComboBoxField` class represents the combo box field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new combo box field
* let field: PdfComboBoxField = new PdfComboBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* field.addItem(new PdfListFieldItem('English', 'English'));
* field.addItem(new PdfListFieldItem('French', 'French'));
* field.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* field.selectedIndex = 2;
* // Sets the flag indicates whether the combo box allows multiple selections.
* field.multiSelect = true;
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfComboBoxField = /** @class */ (function (_super) {
__extends(PdfComboBoxField, _super);
function PdfComboBoxField(page, name, bounds, properties) {
var _this = _super.call(this) || this;
if (page && name && bounds) {
_this._initialize(page, name, bounds);
_this._fieldFlags |= _FieldFlag.combo;
}
if (properties) {
if ('items' in properties && _isNullOrUndefined(properties.items)) {
properties.items.forEach(function (item) {
_this.addItem(new PdfListFieldItem(item.text, item.value));
});
}
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
if ('font' in properties && _isNullOrUndefined(properties.font)) {
_this.font = properties.font;
}
if ('selectedIndex' in properties && _isNullOrUndefined(properties.selectedIndex)) {
_this.selectedIndex = properties.selectedIndex;
}
}
return _this;
}
Object.defineProperty(PdfComboBoxField.prototype, "_isAutoFontSize", {
/**
* Gets the boolean flag indicates whether the combo box field is auto size.
*
* @private
* @returns {boolean} Returns the boolean value to check auto size.
*/
get: function () {
var _this = this;
var isAutoFontSize = false;
if (this._isLoaded && this._form) {
var acroForm = this._form._dictionary;
if (acroForm && acroForm.has('DA')) {
var fontString_1 = acroForm.get('DA');
if (fontString_1) {
var defaultAppearance_1 = new _PdfDefaultAppearance(fontString_1);
if (defaultAppearance_1.fontSize === 0) {
if (this._kids && this._kids.length > 0) {
var fontSize = false;
if (this._dictionary.has('DA')) {
fontString_1 = this._dictionary.get('DA');
if (fontString_1) {
defaultAppearance_1 = new _PdfDefaultAppearance(fontString_1);
if (defaultAppearance_1 && defaultAppearance_1.fontSize > 0) {
fontSize = true;
}
}
}
if (!fontSize) {
this._kids.forEach(function (reference) {
var dictionary;
if (reference && reference instanceof _PdfReference) {
dictionary = _this._crossReference._fetch(reference);
}
if (dictionary) {
if (dictionary.has('DA')) {
fontString_1 = dictionary.get('DA');
var height = 0;
if (fontString_1) {
defaultAppearance_1 = new _PdfDefaultAppearance(fontString_1);
if (defaultAppearance_1) {
height = defaultAppearance_1.fontSize;
}
}
if (height === 0) {
isAutoFontSize = true;
}
}
else {
isAutoFontSize = true;
}
}
});
}
}
else {
if (this._dictionary.has('DA')) {
fontString_1 = this._dictionary.get('DA');
var height = 0;
if (fontString_1) {
defaultAppearance_1 = new _PdfDefaultAppearance(fontString_1);
if (defaultAppearance_1) {
height = defaultAppearance_1.fontSize;
}
}
if (height === 0) {
isAutoFontSize = true;
}
}
else {
isAutoFontSize = true;
}
}
}
}
}
}
return isAutoFontSize;
},
enumerable: true,
configurable: true
});
/**
* Parse an existing combo box field.
*
* @private
* @param {PdfForm} form Form object.
* @param {_PdfDictionary} dictionary Field dictionary.
* @param {_PdfCrossReference} crossReference Cross reference object.
* @param {_PdfReference} reference Field reference.
* @returns {PdfComboBoxField} Combo box field.
*/
PdfComboBoxField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfComboBoxField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
var options = field._dictionary.getArray('Opt');
if (options !== null && typeof options !== 'undefined') {
field._listValues = new Array(options.length);
}
field._defaultIndex = 0;
field._parsedItems = new Map();
if (field._kidsCount > 0) {
field._retrieveOptionValue();
}
return field;
};
/**
* Loads display texts for the fields items from the dictionary `Opt` array and
* assigns them to the corresponding widget items `_text` property.
*
* @private
* @returns {void}
*/
PdfComboBoxField.prototype._retrieveOptionValue = function () {
if (this._dictionary.has('Opt')) {
var options = this._dictionary.getArray('Opt');
if (options && options.length > 0) {
var itemsCount = this._kidsCount;
var count = options.length <= itemsCount ? options.length : itemsCount;
for (var i = 0; i < count; i++) {
var text = options[i][1];
if (text) {
this.itemAt(i)._text = text ? text : '';
}
}
}
}
};
/**
* Creates the appearance template for the list/combobox field (or a specific widget item),
* computing effective bounds (respecting page/field rotation), border, background, colors,
* and string formatting, then renders the widget content.
*
* @private
* @param {PdfListFieldItem} [item] The specific widget item to render; when omitted, renders the field level appearance.
* @returns {PdfTemplate} The generated appearance template for the current visual state.
*/
PdfComboBoxField.prototype._createAppearance = function (item) {
var parameter = new _PaintParameter();
if (item) {
var bounds = item.bounds;
var page = item._getPage();
if (item._isLoaded && page && typeof page.rotation !== 'undefined' && page.rotation !== PdfRotationAngle.angle0) {
parameter.bounds = this._rotateTextBox(bounds, page.size, page.rotation);
}
else {
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
}
var backcolor = item.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(item.color ? item.color : this.color);
var border = item.border;
if (item.borderColor) {
parameter.borderPen = new PdfPen(item.borderColor, border.width);
_updateDashedBorderStyle(border, parameter);
}
parameter.borderStyle = border.style;
parameter.borderWidth = border.width;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
var alignment = typeof item.textAlignment !== 'undefined' ? item.textAlignment : PdfTextAlignment.left;
var verticalAlignment = this.multiSelect ? PdfVerticalAlignment.top : PdfVerticalAlignment.middle;
parameter.stringFormat = new PdfStringFormat(alignment, verticalAlignment);
}
else {
var bounds = this.bounds;
if (bounds) {
if (this._isLoaded &&
this.page &&
typeof this.page.rotation !== 'undefined' &&
this.page.rotation !== PdfRotationAngle.angle0) {
parameter.bounds = this._rotateTextBox(bounds, this.page.size, this.page.rotation);
}
else {
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
}
}
var backcolor = this.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(this.color);
var border = this.border;
if (this.borderColor) {
parameter.borderPen = new PdfPen(this.borderColor, border.width);
_updateDashedBorderStyle(border, parameter);
}
parameter.borderStyle = border.style;
parameter.borderWidth = border.width;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
parameter.rotationAngle = this.rotationAngle;
if (this.rotate !== null && typeof this.rotate !== 'undefined') {
parameter.rotationAngle = this.rotate;
}
var alignment = typeof this.textAlignment !== 'undefined' ? this.textAlignment : PdfTextAlignment.left;
var verticalAlignment = this.multiSelect ? PdfVerticalAlignment.top : PdfVerticalAlignment.middle;
parameter.stringFormat = new PdfStringFormat(alignment, verticalAlignment);
}
parameter.required = this.required;
if (parameter.bounds === null || typeof parameter.bounds === 'undefined') {
parameter.bounds = { x: 0, y: 0, width: 0, height: 0 };
}
var template = new PdfTemplate(parameter.bounds, this._crossReference);
var graphics = template.graphics;
graphics._sw._clear();
if (!this.required) {
graphics._sw._beginMarkupSequence('Tx');
graphics._initializeCoordinates();
}
if (this._isLoaded) {
var font = void 0;
if (item) {
font = this._obtainFont(item);
}
if (typeof font === 'undefined' || font === null) {
font = this.font;
}
if (!font || font.size === 0) {
font = this._appearanceFont;
}
this._drawComboBox(graphics, parameter, font, parameter.stringFormat);
}
else {
if (!this._font) {
this._font = new PdfStandardFont(PdfFontFamily.timesRoman, this._getFontHeight(PdfFontFamily.helvetica));
}
this._drawComboBox(graphics, parameter, this._font, parameter.stringFormat);
}
if (!this.required) {
graphics._sw._endMarkupSequence();
}
return template;
};
/**
* Draws the combobox/list widget: background, border, clipping, and the currently
* selected items text (resolved from `I` and `Opt`), honoring padding and rotation.
*
* @private
* @param {PdfGraphics} graphics The graphics context used for rendering.
* @param {_PaintParameter} [parameter] The paint parameters including bounds, colors, border, rotation, and string format.
* @param {PdfFont} [font] The font to use for rendering the selected items text.
* @param {PdfStringFormat} [stringFormat] The text layout configuration (alignment and line alignment).
* @returns {void}
*/
PdfComboBoxField.prototype._drawComboBox = function (graphics, parameter, font, stringFormat) {
if (graphics._isTemplateGraphics && parameter.required) {
graphics.save();
graphics._initializeCoordinates();
}
this._drawRectangularControl(graphics, parameter);
if (graphics._isTemplateGraphics && parameter.required) {
graphics.restore();
graphics.save();
graphics._sw._beginMarkupSequence('Tx');
graphics._initializeCoordinates();
}
var options = this._options;
var selectedIndexes = this._dictionary.get('I');
var i = -1;
if (selectedIndexes && selectedIndexes.length > 0) {
i = selectedIndexes[0];
}
if (i >= 0 && i < options.length) {
var item = options[i]; // eslint-disable-line
var offset = [0, 0];
var borderWidth = parameter.borderWidth;
var doubleBorderWidth = 2 * borderWidth;
var defaultPadding = 2;
var padding = (parameter.borderStyle === PdfBorderStyle.inset || parameter.borderStyle === PdfBorderStyle.beveled);
if (padding) {
offset[0] = 2 * doubleBorderWidth;
offset[1] = 2 * borderWidth;
}
else {
offset[0] = doubleBorderWidth + defaultPadding;
offset[1] = 1 * borderWidth;
}
var brush = parameter.foreBrush;
var rect = parameter.bounds;
var width = rect.width - doubleBorderWidth;
var rectangle = rect;
if (padding) {
rectangle.height -= doubleBorderWidth;
}
else {
rectangle.height -= borderWidth;
}
graphics.setClip(rectangle, PdfFillMode.winding);
if (parameter.rotationAngle === 0) {
if (padding) {
width -= doubleBorderWidth;
}
brush = new PdfBrush(this.color ? this.color : { r: 0, g: 0, b: 0 });
}
var value = void 0;
if (item && Array.isArray(item)) {
value = item[1] ? item[1] : item[0];
}
else {
value = item;
}
if (value) {
var itemTextBound = { x: offset[0], y: offset[1], width: width - offset[0], height: rect.height };
if (parameter.rotationAngle > 0) {
var state = graphics.save();
if (parameter.rotationAngle === 90) {
graphics.translateTransform({ x: 0, y: graphics._size.height });
graphics.rotateTransform(-90);
var x = graphics._size.height - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height + rectangle.width, height: rectangle.width };
}
else if (parameter.rotationAngle === 270) {
graphics.translateTransform({ x: graphics._size.width, y: 0 });
graphics.rotateTransform(-270);
var x = rectangle.y;
var y = graphics._size.width - (rectangle.x + rectangle.width);
rectangle = { x: x, y: y, width: rectangle.height + rectangle.width, height: rectangle.width };
}
else if (parameter.rotationAngle === 180) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(-180);
var x = graphics._size.width - (rectangle.x + rectangle.width);
var y = graphics._size.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
if (padding) {
width -= doubleBorderWidth;
}
brush = new PdfBrush({ r: 0, g: 0, b: 0 });
graphics.drawString(value, font, itemTextBound, null, brush, stringFormat);
graphics.restore(state);
}
else {
graphics.drawString(value, font, itemTextBound, null, brush, stringFormat);
}
}
}
if (graphics._isTemplateGraphics && parameter.required) {
graphics._sw._endMarkupSequence();
graphics.restore();
}
};
/**
* Computes a suitable font size (height) for rendering the selected value within
* the field bounds, based on the specified font family and current option text widths.
* Ensures the text fits the available width/height by scaling within border padding.
*
* @private
* @param {PdfFontFamily} fontFamily The font family to measure and scale.
* @returns {number} The resolved font size in points that fits the controls content.
*/
PdfComboBoxField.prototype._getFontHeight = function (fontFamily) {
var values = this._dictionary.get('I');
var s;
var itemFont;
var format;
var options;
var bounds;
var borderWidth = this.border.width;
if (this._isLoaded) {
itemFont = new PdfStandardFont(fontFamily, 12);
format = new PdfStringFormat(PdfTextAlignment.center, PdfVerticalAlignment.middle);
options = this._dictionary.getArray('Opt');
bounds = this.bounds;
var widths_1 = [];
if (values && values.length > 0) {
values.forEach(function (entry) {
widths_1.push(itemFont.measureString(options[entry][1], { width: 0, height: 0 }, format, 0, 0).width);
});
}
else if (options.length > 0) {
var max = itemFont.measureString(options[0][1], { width: 0, height: 0 }, format, 0, 0).width;
for (var i = 1; i < options.length; ++i) {
var width = itemFont.measureString(options[i][1], { width: 0, height: 0 }, format, 0, 0).width;
max = Math.max(max, width);
widths_1.push(max);
}
}
s = (widths_1.length > 0) ? ((12 * (bounds.width - 4 * borderWidth)) / ((widths_1.sort())[widths_1.length - 1])) : 12;
}
else {
s = 0;
if (values && values.length > 0) {
itemFont = new PdfStandardFont(fontFamily, 12);
format = new PdfStringFormat(PdfTextAlignment.center, PdfVerticalAlignment.middle);
options = this._dictionary.getArray('Opt');
var selectedValue = this.selectedValue; // eslint-disable-line
var width = itemFont.measureString((selectedValue !== null && typeof selectedValue === 'string') ? selectedValue :
options[values[0]][1], { width: 0, height: 0 }, format, 0, 0).width;
bounds = this.bounds;
if (width) {
s = (12 * (bounds.width - 4 * borderWidth)) / width;
}
else {
s = 12;
}
}
else {
return s;
}
}
var fontSize = 0;
if (values && values.length > 0) {
if (s !== 12) {
itemFont = new PdfStandardFont(fontFamily, s);
var selectedValue = this.selectedValue; // eslint-disable-line
var text = (selectedValue !== null && typeof selectedValue === 'string') ? selectedValue :
options[values[0]][1];
var textSize = itemFont.measureString(text);
if (textSize.width > bounds.width || textSize.height > bounds.height) {
var width = bounds.width - 4 * borderWidth;
var h = bounds.height - 4 * borderWidth;
var min = 0.248;
for (var i = 1; i <= bounds.height; i++) {
itemFont = new PdfStandardFont(fontFamily, i);
var size = itemFont.measureString(text);
if (size.width > bounds.width || size.height > h) {
fontSize = i;
do {
fontSize = fontSize - 0.001;
itemFont = new PdfStandardFont(fontFamily, fontSize);
var textWidth = itemFont.getLineWidth(text, format);
if (fontSize < min) {
itemFont._size = min;
break;
}
size = itemFont.measureString(text, { width: 0, height: 0 }, format, 0, 0);
if (textWidth < width && size.height < h) {
itemFont._size = fontSize;
break;
}
} while (fontSize > min);
s = fontSize;
break;
}
}
}
}
}
else if (s > 12) {
s = 12;
}
return s;
};
return PdfComboBoxField;
}(PdfListField));
export { PdfComboBoxField };
/**
* `PdfListBoxField` class represents the list box field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new list box field
* let field: PdfListBoxField = new PdfListBoxField(page, 'list1', {x: 100, y: 60, width: 100, height: 50});
* // Add list items to the field.
* field.addItem(new PdfListFieldItem('English', 'English'));
* field.addItem(new PdfListFieldItem('French', 'French'));
* field.addItem(new PdfListFieldItem('German', 'German'));
* // Sets the selected index
* field.selectedIndex = 2;
* // Sets the flag indicates whether the list box allows multiple selections.
* field.multiSelect = true;
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfListBoxField = /** @class */ (function (_super) {
__extends(PdfListBoxField, _super);
function PdfListBoxField(page, name, bounds, properties) {
var _this = _super.call(this) || this;
if (page && name && bounds) {
_this._initialize(page, name, bounds);
}
if (properties) {
if ('items' in properties && _isNullOrUndefined(properties.items)) {
properties.items.forEach(function (item) {
_this.addItem(new PdfListFieldItem(item.text, item.value));
});
}
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
if ('font' in properties && _isNullOrUndefined(properties.font)) {
_this.font = properties.font;
}
if ('multiSelect' in properties && _isNullOrUndefined(properties.multiSelect)) {
_this.multiSelect = properties.multiSelect;
}
if ('selectedIndex' in properties && _isNullOrUndefined(properties.selectedIndex)) {
_this.selectedIndex = properties.selectedIndex;
}
}
return _this;
}
/**
* Parse an existing list box field of the PDF document.
*
* @private
* @param {number} form maximum length.
* @param {_PdfDictionary} dictionary maximum length.
* @param {_PdfCrossReference} crossReference maximum length.
* @param {_PdfReference} reference maximum length.
* @returns {PdfListBoxField} List box field.
*/
PdfListBoxField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfListBoxField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
field._defaultIndex = 0;
field._parsedItems = new Map();
if (field._kidsCount > 0) {
field._retrieveOptionValue();
}
return field;
};
/**
* Reads the field's `Opt` array, prepares the internal `_listValues` cache, and
* assigns display text to each widget item’s `_text`. Also updates the current
* `_selectedIndex` from the `I` entry when available.
*
* @private
* @returns {void}
*/
PdfListBoxField.prototype._retrieveOptionValue = function () {
if (this._dictionary.has('Opt')) {
var options = this._dictionary.getArray('Opt');
var itemsCount = this._kidsCount;
var count = options.length <= itemsCount ? options.length : itemsCount;
this._listValues = new Array(count);
if (options && options.length > 0) {
var index = this._dictionary.get('I');
if (Array.isArray(index) && index.length > 0) {
index = index[0];
this._selectedIndex = index;
}
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item) {
if (_isNullOrUndefined(index) && this._listValues !== null && typeof this._listValues !== 'undefined') {
var value = options[i]; // eslint-disable-line
if (Array.isArray(value)) {
this._listValues[i] = value[1];
}
else {
this._listValues[i] = value;
}
if (i === index) {
item._text = this._listValues[i];
this._selectedIndex = i;
}
else {
item._text = this._listValues[i];
}
}
else {
item._text = '';
}
}
}
}
}
};
/**
* Creates the appearance template for the list box field ,
* computing effective bounds , border, background,
* and text formatting, then renders the widget content.
*
* @private
* @param {PdfListFieldItem} [item] The specific widget item to render; when omitted, renders the field-level appearance.
* @returns {PdfTemplate} The generated appearance template for the current visual state.
*/
PdfListBoxField.prototype._createAppearance = function (item) {
var parameter = new _PaintParameter();
if (item) {
var bounds = item.bounds;
var page = item._getPage();
if (item._isLoaded && page && typeof page.rotation !== 'undefined' && page.rotation !== PdfRotationAngle.angle0) {
parameter.bounds = this._rotateTextBox(bounds, page.size, page.rotation);
}
else {
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
}
var backcolor = item.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(item.color ? item.color : this.color);
var border = item.border;
if (item.borderColor) {
parameter.borderPen = new PdfPen(item.borderColor, border.width);
_updateDashedBorderStyle(border, parameter);
}
parameter.borderStyle = border.style;
parameter.borderWidth = border.width;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
if (item._enableGrouping && typeof item.rotate === 'undefined') {
parameter.rotationAngle = 0;
}
else {
parameter.rotationAngle = item.rotate;
}
var alignment = typeof item.textAlignment !== 'undefined' ? item.textAlignment : PdfTextAlignment.left;
var verticalAlignment = this.multiSelect ? PdfVerticalAlignment.top : PdfVerticalAlignment.middle;
parameter.stringFormat = new PdfStringFormat(alignment, verticalAlignment);
}
else {
var bounds = this.bounds;
if (this._isLoaded &&
this.page &&
typeof this.page.rotation !== 'undefined' &&
this.page.rotation !== PdfRotationAngle.angle0) {
parameter.bounds = this._rotateTextBox(bounds, this.page.size, this.page.rotation);
}
else {
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
}
var backcolor = this.backColor;
if (backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(this.color);
var border = this.border;
if (this.borderColor) {
parameter.borderPen = new PdfPen(this.borderColor, border.width);
_updateDashedBorderStyle(border, parameter);
}
parameter.borderStyle = border.style;
parameter.borderWidth = border.width;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
parameter.rotationAngle = this.rotationAngle;
if (this.rotate !== null && typeof this.rotate !== 'undefined') {
parameter.rotationAngle = this.rotate;
}
var alignment = typeof this.textAlignment !== 'undefined' ? this.textAlignment : PdfTextAlignment.left;
var verticalAlignment = this.multiSelect ? PdfVerticalAlignment.top : PdfVerticalAlignment.middle;
parameter.stringFormat = new PdfStringFormat(alignment, verticalAlignment);
}
parameter.required = this.required;
var template = new PdfTemplate(parameter.bounds, this._crossReference);
var graphics = template.graphics;
graphics._sw._clear();
if (!this.required) {
graphics._sw._beginMarkupSequence('Tx');
graphics._initializeCoordinates();
}
if (this._isLoaded) {
var font = this._obtainFont(item);
if ((typeof font === 'undefined' || font === null) || (!this._isLoaded && font.size === 1)) {
font = this._appearanceFont;
}
this._drawListBox(graphics, parameter, font, parameter.stringFormat);
}
else {
if (!this._font) {
this._font = this._defaultItemFont;
}
this._drawListBox(graphics, parameter, this._font, parameter.stringFormat);
}
if (!this.required) {
graphics._sw._endMarkupSequence();
}
return template;
};
/**
* Draws the list box control including background, border, clipping, selection highlight,
* and each options text, honoring padding and rotation settings.
*
* @private
* @param {PdfGraphics} graphics The graphics context used for rendering.
* @param {_PaintParameter} [parameter] The paint parameters including bounds, colors, border, rotation, and layout.
* @param {PdfFont} [font] The font to use for rendering list items.
* @param {PdfStringFormat} [stringFormat] The text layout configuration (alignment and line alignment).
* @returns {void}
*/
PdfListBoxField.prototype._drawListBox = function (graphics, parameter, font, stringFormat) {
if (graphics._isTemplateGraphics && parameter.required) {
graphics.save();
graphics._initializeCoordinates();
}
this._drawRectangularControl(graphics, parameter);
if (graphics._isTemplateGraphics && parameter.required) {
graphics.restore();
graphics.save();
graphics._sw._beginMarkupSequence('Tx');
graphics._initializeCoordinates();
}
var options = this._options;
var _loop_1 = function (index) {
var item = options[index];
var location_1 = [];
var borderWidth = parameter.borderWidth;
var doubleBorderWidth = 2 * borderWidth;
var defaultPadding = 2;
var padding = (parameter.borderStyle === PdfBorderStyle.inset || parameter.borderStyle === PdfBorderStyle.beveled);
if (padding) {
location_1.push(2 * doubleBorderWidth);
location_1.push((index + 2) * borderWidth + font._getHeight() * index);
}
else {
location_1.push(doubleBorderWidth + defaultPadding);
location_1.push((index + 1) * borderWidth + font._getHeight() * index + (defaultPadding - 1));
}
var brush = parameter.foreBrush;
var rect = parameter.bounds;
var width = rect.width - doubleBorderWidth;
var rectangle = rect;
if (padding) {
rectangle.height -= doubleBorderWidth;
}
else {
rectangle.height -= borderWidth;
}
graphics.setClip(rectangle, PdfFillMode.winding);
var selected = false;
var selectedIndexes = this_1._dictionary.get('I');
if (selectedIndexes !== null && typeof selectedIndexes !== 'undefined' && selectedIndexes.length > 0) {
selectedIndexes.forEach(function (selectedIndex) {
selected = selected || (selectedIndex === index);
});
}
if (parameter.rotationAngle === 0) {
if (selected) {
var x = rect.x + borderWidth;
if (padding) {
x += borderWidth;
width -= doubleBorderWidth;
}
brush = new PdfBrush({ r: 153, g: 193, b: 218 });
graphics.drawRectangle({ x: x, y: location_1[1], width: width, height: font._getHeight() }, brush);
brush = new PdfBrush(this_1.color ? this_1.color : { r: 0, g: 0, b: 0 });
}
}
var value = void 0;
if (item && typeof item === 'string') {
value = item;
}
else {
value = item[1] ? item[1] : item[0];
}
var itemTextBound = { x: location_1[0], y: location_1[1], width: width - location_1[0], height: font._getHeight() };
if (parameter.rotationAngle > 0) {
var state = graphics.save();
if (parameter.rotationAngle === 90) {
graphics.translateTransform({ x: 0, y: graphics._size.height });
graphics.rotateTransform(-90);
var x = graphics._size.height - (rectangle.y + rectangle.height);
var y = rectangle.x;
rectangle = { x: x, y: y, width: rectangle.height + rectangle.width, height: rectangle.width };
}
else if (parameter.rotationAngle === 270) {
graphics.translateTransform({ x: graphics._size.width, y: 0 });
graphics.rotateTransform(-270);
var x = rectangle.y;
var y = graphics._size.width - (rectangle.x + rectangle.width);
rectangle = { x: x, y: y, width: rectangle.height + rectangle.width, height: rectangle.width };
}
else if (parameter.rotationAngle === 180) {
graphics.translateTransform({ x: graphics._size.width, y: graphics._size.height });
graphics.rotateTransform(-180);
var x = graphics._size.width - (rectangle.x + rectangle.width);
var y = graphics._size.height - (rectangle.y + rectangle.height);
rectangle = { x: x, y: y, width: rectangle.width, height: rectangle.height };
}
if (selected) {
var x = rect.x + borderWidth;
if (padding) {
x += borderWidth;
width -= doubleBorderWidth;
}
brush = new PdfBrush({ r: 153, g: 193, b: 218 });
graphics.drawRectangle({ x: x, y: location_1[1], width: width, height: font._getHeight() }, brush);
brush = new PdfBrush(this_1.color ? this_1.color : { r: 0, g: 0, b: 0 });
}
graphics.drawString(value, font, itemTextBound, null, brush, stringFormat);
graphics.restore(state);
}
else {
graphics.drawString(value, font, itemTextBound, null, brush, stringFormat);
}
};
var this_1 = this;
for (var index = 0; index < options.length; ++index) {
_loop_1(index);
}
if (graphics._isTemplateGraphics && parameter.required) {
graphics._sw._endMarkupSequence();
graphics.restore();
}
};
/**
* Computes an appropriate font size (height) for rendering list items so that text
* fits within the field's width accounting for border padding based on measured
* widths of `_listValues`.
*
* @private
* @param {PdfFontFamily} fontFamily The font family used for measurement and scaling.
* @returns {number} The resolved font size in points (capped to a maximum of 12).
*/
PdfListBoxField.prototype._getFontHeight = function (fontFamily) {
var itemFont = new PdfStandardFont(fontFamily, 12, PdfFontStyle.regular);
var format = new PdfStringFormat(PdfTextAlignment.left, PdfVerticalAlignment.middle);
var s = 0;
if (_isNullOrUndefined(this._listValues) && this._listValues.length > 0) {
var max = itemFont.measureString(this._listValues[0], { width: 0, height: 0 }, format, 0, 0).width;
for (var i = 1; i < this._listValues.length; ++i) {
var value = itemFont.measureString(this._listValues[i], { width: 0, height: 0 }, format, 0, 0).width;
max = (max > value) ? max : value;
}
s = ((12 * (this.bounds.width - 4 * this.border.width)) / max);
s = (s > 12) ? 12 : s;
}
return s;
};
return PdfListBoxField;
}(PdfListField));
export { PdfListBoxField };
/**
* `PdfSignatureField` class represents the signature field objects.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new signature field
* let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
var PdfSignatureField = /** @class */ (function (_super) {
__extends(PdfSignatureField, _super);
function PdfSignatureField(page, name, bounds, properties) {
var _this = _super.call(this) || this;
_this._rotateAngle = 0;
/**
* Indicates whether the field already contains a signature.
*
* @private
*/
_this._isSigned = false;
_this._revision = -1;
if (page && name && bounds) {
_this._initialize(page, name, bounds);
}
if (properties) {
if ('toolTip' in properties && _isNullOrUndefined(properties.toolTip)) {
_this.toolTip = properties.toolTip;
}
if ('color' in properties && _isNullOrUndefined(properties.color)) {
_this.color = properties.color;
}
if ('border' in properties && _isNullOrUndefined(properties.border)) {
_this.border = properties.border;
}
if ('backColor' in properties && _isNullOrUndefined(properties.backColor)) {
_this.backColor = properties.backColor;
}
if ('borderColor' in properties && _isNullOrUndefined(properties.borderColor)) {
_this.borderColor = properties.borderColor;
}
}
return _this;
}
Object.defineProperty(PdfSignatureField.prototype, "isSigned", {
/**
* Gets the flag to indicate whether the field is signed or not.
*
* @returns {boolean} Returns true if the field is signed; otherwise, false.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the loaded signature field
* let field: PdfSignatureField = document.form.fieldAt(0) as PdfSignatureField;
* // Get the signed status of the field
* let isSigned: boolean = field.isSigned;
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
if (!this._isSigned) {
this._checkSigned();
}
return this._isSigned;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfSignatureField.prototype, "backColor", {
/**
* Gets the background color of the field.
*
* @returns {PdfColor} R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the form field at index 0
* let field: PdfField = document.form.fieldAt(0);
* // Gets the background color of the field.
* let backColor: PdfColor = field.backColor;
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
get: function () {
return this._parseBackColor(true);
},
/**
* Sets the background color of the field.
*
* @param {PdfColor} value Array with R, G, B color values in between 0 to 255.
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data, password);
* // Access the signature field at index 0
* let field1: PdfField = document.form.fieldAt(0);
* // Sets the background color of the field.
* field1.backColor = {r: 255, g: 0, b: 0};
* // Access the signature field at index 1
* let field2: PdfField = document.form.fieldAt(1);
* // Sets the background color of the field to transparent.
* field2.backColor = {r: 0, g: 0, b: 0, isTransparent: true};
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*/
set: function (value) {
this._updateBackColor(value, true);
},
enumerable: true,
configurable: true
});
/**
* Loads a signature field from the specified dictionary and reference, binding it
* to the owning form and cross-reference, and initializes its kids and caches.
*
* @private
* @param {PdfForm} form The parent form that owns this signature field.
* @param {_PdfDictionary} dictionary The field dictionary from which to load properties.
* @param {_PdfCrossReference} crossReference The cross-reference table for object resolution.
* @param {_PdfReference} reference The indirect reference identifying this field.
* @returns {PdfSignatureField} The initialized signature field instance in a loaded state.
*/
PdfSignatureField._load = function (form, dictionary, crossReference, reference) {
var field = new PdfSignatureField();
field._isLoaded = true;
field._form = form;
field._dictionary = dictionary;
field._crossReference = crossReference;
field._ref = reference;
if (field._dictionary.has('Kids')) {
field._kids = field._dictionary.get('Kids');
}
field._defaultIndex = 0;
field._parsedItems = new Map();
return field;
};
/**
* Gets the signature associated with the PDF signature field.
*
* ```typescript
* // Load the document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Gets the signature field
* let field: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
* // Gets the PDF signature
* let signature: PdfSignature = field.getSignature();
* // Gets the signature options
* let options: PdfSignatureOptions = signature.getSignatureOptions();
* // Gets the cryptographic standard of the signature
* let cryptographicStandard: CryptographicStandard = options.cryptographicStandard;
* // Destroy the document
* document.destroy();
* ```
*
* @returns {PdfSignature} - The signature instance.
*/
PdfSignatureField.prototype.getSignature = function () {
if (!this._signature && this._dictionary.has('V')) {
this._signature = this._crossReference._document._getSignature(this._dictionary.get('V'), this);
}
return this._signature;
};
/**
* Sets the signature for the PDF signature field.
*
* ```typescript
* // Load the document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new signature field
* let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
* // Create a new signature using PFX data and private key
* const sign: PdfSignature = PdfSignature.create(certData, password, { cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 });
* // Sets the signature to the field
* field.setSignature(sign);
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @param {PdfSignature} signature - The signature to assign to the field.
* @returns {void} Nothing.
*/
PdfSignatureField.prototype.setSignature = function (signature) {
if (this._crossReference) {
var form = this._crossReference._document.form;
form._signatureFlag = _SignatureFlag.signatureExists | _SignatureFlag.appendOnly;
signature._signatureField = this;
signature._signatureDictionary = signature._createDictionary(this._crossReference._document, signature);
this._signature = signature;
var ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(ref, signature._signatureDictionary._dictionary);
signature._reference = ref;
signature._signatureDictionary._dictionary._isSignature = true;
this._crossReference._signature = signature._signatureDictionary;
this._dictionary.update('V', ref);
this._dictionary.update('Ff', 0);
this._crossReference._signatureCollection.push(signature);
}
};
/**
* Gets the appearance of the PDF signature field.
*
* ```typescript
* // Load the document
* let document: PdfDocument = new PdfDocument(data);
* // Gets the first page of the document
* let page: PdfPage = document.getPage(0);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Create a new signature field
* let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
* // Create a new signature using PFX data and private key
* const sign: PdfSignature = PdfSignature.create(certData, password, { cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 });
* // Sets the signature to the field
* field.setSignature(sign);
* // Gets the field Appearance
* let appearance = field.getAppearance();
* // Add the field into PDF form
* form.add(field);
* // Save the document
* document.save('output.pdf');
* // Destroy the document
* document.destroy();
* ```
*
* @returns {PdfAppearance} - The appearance of the PDF signature field.
*/
PdfSignatureField.prototype.getAppearance = function () {
if (this._isLoaded) {
return null;
}
if (!this._appearance) {
var nativeRectangle = { x: 0, y: 0, width: this.bounds.width, height: this.bounds.height };
this._appearance = new PdfAppearance(nativeRectangle, this._widgetAnnot);
this._appearance.normal = new PdfTemplate(nativeRectangle, this._crossReference);
}
return this._appearance;
};
/**
* Gets the revision index of the PDF signature field.
*
* ```typescript
* // Load an existing PDF document
* let document: PdfDocument = new PdfDocument(data);
* // Access the PDF form
* let form: PdfForm = document.form;
* // Gets the signature field
* let signature: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
* // Gets the revision number associated with the signature field
* let revision: number = signature.getRevision();
* // Destroy the document
* document.destroy();
* ```
*
* @returns {number} - The revision index of the signature.
*/
PdfSignatureField.prototype.getRevision = function () {
if (this.isSigned && this._revision === -1) {
this._revision = this._getSignedRevision();
}
return this._revision;
};
/* eslint-disable */
/**
* Initializes the signature field on the specified page with the given name and bounds,
* creating its dictionary, reference, default font, and the initial widget item.
*
* @private
* @param {PdfPage} page The page on which the signature field is created.
* @param {string} name The field name to assign to the signature field.
* @param {{x: number, y: number, width: number, height: number}} bounds The field bounds in page coordinates. // eslint-disable-line
* @returns {void}
*/
PdfSignatureField.prototype._initialize = function (page, name, bounds) {
this._crossReference = page._crossReference;
this._page = page;
this._name = name;
this._dictionary = new _PdfDictionary(this._crossReference);
this._ref = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(this._ref, this._dictionary);
this._dictionary.objId = this._ref.toString();
this._dictionary.update('FT', _PdfName.get('Sig'));
this._dictionary.update('T', name);
this._defaultIndex = 0;
this._initializeFont(this._defaultFont);
this._createItem(bounds);
};
/**
* Creates and adds the signature field’s primary widget annotation using the provided bounds,
* sets up appearance-related entries (`MK`, `BC`, `BG`, `DA`), and adds it to the Kids array.
*
* @private
* @param {{x: number, y: number, width: number, height: number}} bounds The widget bounds in page coordinates.
* @returns {void}
*/
PdfSignatureField.prototype._createItem = function (bounds) {
this._widgetAnnot = new PdfWidgetAnnotation();
this._widgetAnnot._create(this._page, bounds, this);
this._widgetAnnot._dictionary.update('MK', new _PdfDictionary(this._crossReference));
this._widgetAnnot._mkDictionary.update('BC', [0, 0, 0]);
this._widgetAnnot._mkDictionary.update('BG', [1, 1, 1]);
this._widgetAnnot._dictionary.update('DA', this._fontName + " 8 Tf 0 0 0 rg");
this._addToKid(this._widgetAnnot);
};
/* eslint-enable */
/**
* Finalizes the signature field after updates by generating or attaching appearances,
* handling signature locking if required, and optionally flattening the field to static content.
*
* @private
* @param {boolean} [isFlatten=false] When `true`, flattens appearances into the page content.
* @returns {void}
*/
PdfSignatureField.prototype._doPostProcess = function (isFlatten) {
if (isFlatten === void 0) { isFlatten = false; }
if (this._signature && this._signature._isLocked &&
!this._signature._certify && !this._signature._signed) {
this._signature._lockSignature();
}
var needAppearance = this._setAppearance || this._form._setAppearance;
if (isFlatten || needAppearance || this._appearance) {
var count = this._kidsCount;
if (count > 0) {
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (this._appearance) {
var template = this._appearance.normal;
this._rotateAngle = this._widgetAnnot._getRotationAngle();
_setMatrix(template, this.rotate);
var dictionary = new _PdfDictionary();
template._content.dictionary._updated = true;
var reference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(reference, template._content);
template._content.reference = reference;
dictionary.set('N', reference);
dictionary._updated = true;
this._widgetAnnot._dictionary.set('AP', dictionary);
}
else if (item && item._dictionary && (needAppearance || (isFlatten && !item._dictionary.has('AP')))) {
var template = this._createAppearance(item, isFlatten);
this._addAppearance(item._dictionary, template, 'N');
}
}
}
}
if (isFlatten) {
var count = this._kidsCount;
if (count > 0) {
var firstItemTemplate = void 0;
for (var i = 0; i < count; i++) {
var item = this.itemAt(i);
if (item && item._dictionary) {
var page = item.page;
if (page) {
if (!firstItemTemplate && i === 0) {
firstItemTemplate = this._getItemTemplate(item._dictionary);
}
if (this._appearance) {
firstItemTemplate = this._appearance.normal;
}
this._flattenSignature(item._dictionary, page, item.bounds, firstItemTemplate);
}
}
}
}
else {
this._flattenSignature(this._dictionary, this.page, this.bounds);
}
}
};
/**
* Builds a visual appearance template for the signature widget, applying border, background,
* colors, and rotation. When flattening, fills background if specified.
*
* @private
* @param {PdfWidgetAnnotation} widget The signature widget for which to create the appearance.
* @param {boolean} isFlatten Indicates whether the appearance is being created for flattening.
* @returns {PdfTemplate} The generated appearance template for the widget.
*/
PdfSignatureField.prototype._createAppearance = function (widget, isFlatten) {
var bounds = widget.bounds;
var template = new PdfTemplate([0, 0, bounds.width, bounds.height], this._crossReference);
_setMatrix(template, null);
template._writeTransformation = false;
var graphics = template.graphics;
var parameter = new _PaintParameter();
parameter.bounds = { x: 0, y: 0, width: bounds.width, height: bounds.height };
var backcolor = widget.backColor;
if (isFlatten && backcolor && !backcolor.isTransparent) {
parameter.backBrush = new PdfBrush(backcolor);
}
parameter.foreBrush = new PdfBrush(widget.color);
var border = widget.border;
if (widget.borderColor) {
parameter.borderPen = new PdfPen(widget.borderColor, border.width);
}
parameter.borderWidth = border.width;
parameter.borderStyle = border.style;
if (backcolor) {
var shadowColor = [backcolor.r - 64, backcolor.g - 64, backcolor.b - 64];
var color = { r: shadowColor[0] >= 0 ? shadowColor[0] : 0,
g: shadowColor[1] >= 0 ? shadowColor[1] : 0,
b: shadowColor[2] >= 0 ? shadowColor[2] : 0 };
parameter.shadowBrush = new PdfBrush(color);
}
parameter.rotationAngle = widget.rotate;
graphics.save();
graphics._initializeCoordinates();
this._drawRectangularControl(graphics, parameter);
graphics.restore();
return template;
};
/* eslint-disable */
/**
* Draws the signature appearance onto the specified page at the given bounds, using the
* widget or field appearance stream (`AP`) if present, or a provided template.
*
* @private
* @param {_PdfDictionary} dictionary The widget or field dictionary containing appearance entries.
* @param {PdfPage} page The page on which to draw the signature appearance.
* @param {{x: number, y: number, width: number, height: number}} bounds The drawing bounds on the page.
* @param {PdfTemplate} [signatureTemplate] Optional predefined appearance template to use when `AP` is absent.
* @returns {void}
*/
PdfSignatureField.prototype._flattenSignature = function (dictionary, page, bounds, signatureTemplate) {
var template;
if (dictionary.has('AP')) {
var appearanceDictionary = dictionary.get('AP');
if (appearanceDictionary && appearanceDictionary.has('N')) {
var appearanceStream = appearanceDictionary.get('N');
var reference = appearanceDictionary.getRaw('N');
if (reference && appearanceStream) {
appearanceStream.reference = reference;
}
if (appearanceStream) {
if (signatureTemplate) {
template = signatureTemplate;
}
else {
template = new PdfTemplate(appearanceStream, this._crossReference);
}
if (template && page) {
var graphics = page.graphics;
var state = graphics.save();
if (this.isSigned) {
template._isSignature = true;
}
if (page.rotation !== PdfRotationAngle.angle0) {
var newGraphics = new PdfGraphics(graphics._size, graphics._sw._stream, graphics._crossReference, page);
newGraphics.drawTemplate(template, this._calculateTemplateBounds(bounds, page, template, newGraphics));
}
else {
graphics.drawTemplate(template, bounds);
}
graphics.restore(state);
}
}
}
}
else if (signatureTemplate && page) {
template = signatureTemplate;
var graphics = page.graphics;
var state = graphics.save();
if (page.rotation !== PdfRotationAngle.angle0) {
var newGraphics = new PdfGraphics(graphics._size, graphics._sw._stream, graphics._crossReference, page);
newGraphics.drawTemplate(template, this._calculateTemplateBounds(bounds, page, template, newGraphics));
}
else {
graphics.drawTemplate(template, bounds);
}
graphics.restore(state);
}
};
/**
* Computes transformed template bounds for the current page rotation, applying
* the appropriate translate/rotate transforms to the graphics context.
*
* @private
* @param {{x: number, y: number, width: number, height: number}} bounds The original widget bounds.
* @param {PdfPage} page The page whose rotation and size influence the transformed bounds.
* @param {PdfTemplate} template The template being drawn (used for size/transform).
* @param {PdfGraphics} graphics The graphics context to which transforms are applied.
* @returns {{x: number, y: number, width: number, height: number}} The adjusted bounds after transformation.
*/
PdfSignatureField.prototype._calculateTemplateBounds = function (bounds, page, template, graphics) {
var x = bounds.x;
var y = bounds.y;
if (page) {
var graphicsRotation = this._obtainGraphicsRotation(page.graphics._matrix);
if (graphicsRotation === 90) {
graphics.translateTransform({ x: template._size.height, y: 0 });
graphics.rotateTransform(90);
x = bounds.x;
y = -(page._size.height - bounds.y - bounds.height);
}
else if (graphicsRotation === 180) {
graphics.translateTransform({ x: template._size.width, y: template._size.height });
graphics.rotateTransform(180);
x = -(page._size.width - (bounds.x + template._size.width));
y = -(page._size.height - bounds.y - template._size.height);
}
else if (graphicsRotation === 270) {
graphics.translateTransform({ x: 0, y: template._size.width });
graphics.rotateTransform(270);
if (page._size.width > page._size.height && template._content && template._content.dictionary && template._content.dictionary.has('Matrix')) {
var matrix = template._content.dictionary.get('Matrix');
if (matrix[1] === -1 && matrix[2] === 1) {
x = -(page._size.width - bounds.x - template.size.width);
y = bounds.y - bounds.height;
}
else {
x = -(page._size.width - bounds.x - bounds.width);
y = bounds.y + bounds.x + template._size.width;
return { x: x, y: y, width: bounds.height, height: bounds.width };
}
}
else {
x = -(page._size.width - bounds.x - bounds.width);
y = bounds.y;
}
}
}
return { x: x, y: y, width: bounds.width, height: bounds.height };
};
/* eslint-enable */
/**
* Determines the effective rotation angle (0/90/180/270) of the graphics context
* based on the provided transformation matrix.
*
* @private
* @param {_PdfTransformationMatrix} matrix The transformation matrix of the graphics context.
* @returns {number} The normalized rotation angle in degrees.
*/
PdfSignatureField.prototype._obtainGraphicsRotation = function (matrix) {
var angle = Math.round(Math.atan2(matrix._matrix._elements[2], matrix._matrix._elements[0]) * 180 / Math.PI);
switch (angle) {
case -90:
angle = 90;
break;
case -180:
angle = 180;
break;
case 90:
angle = 270;
break;
}
return angle;
};
/**
* Retrieves the normal appearance template from the given dictionarys `AP` entry,
* if present, and binds its reference.
*
* @private
* @param {_PdfDictionary} dictionary The widget or field dictionary to read the appearance from.
* @returns {PdfTemplate} The extracted appearance template, or `undefined` if not available.
*/
PdfSignatureField.prototype._getItemTemplate = function (dictionary) {
var template;
if (dictionary && dictionary.has('AP')) {
var appearanceDictionary = dictionary.get('AP');
if (appearanceDictionary && appearanceDictionary.has('N')) {
var appearanceStream = appearanceDictionary.get('N');
var reference = appearanceDictionary.getRaw('N');
if (reference) {
appearanceStream.reference = reference;
}
if (appearanceStream) {
template = new PdfTemplate(appearanceStream, this._crossReference);
}
}
}
return template;
};
/**
* Checks the field dictionary for a populated `V` entry to determine if the signature
* has been applied and updates the internal signed state.
*
* @private
* @returns {void}
*/
PdfSignatureField.prototype._checkSigned = function () {
if (this._dictionary && this._dictionary.has('V')) {
var dictionary = this._dictionary.get('V');
if (dictionary !== null && typeof dictionary !== 'undefined' && dictionary.size > 0) {
this._isSigned = true;
}
}
};
PdfSignatureField.prototype._getSignedRevision = function () {
var signatureDictionary = this._dictionary.get('V');
var range = signatureDictionary.getArray('ByteRange');
var start = range[0] + range[1];
var end = range[2] + range[3];
var revision = -1;
if (this._crossReference && this._crossReference._document) {
var eofOffsets = this._crossReference._document.getRevisions();
if (eofOffsets && Array.isArray(eofOffsets) && eofOffsets.length > 0) {
for (var i = 0; i < eofOffsets.length; i++) {
var offset = eofOffsets[i];
if (offset > start && offset === end) {
revision = i + 1;
break;
}
}
}
}
return revision;
};
return PdfSignatureField;
}(PdfField));
export { PdfSignatureField };
/**
* Represents a parsed default appearance (`DA`) definition, capturing font name,
* font size, and RGB color to be used for text rendering.
*
* @private
*/
var _PdfDefaultAppearance = /** @class */ (function () {
function _PdfDefaultAppearance(da) {
var color;
var fontName = '';
var fontSize = 0;
if (da && typeof da === 'string' && da !== '') {
var sliced_1 = da.split(' ');
sliced_1.forEach(function (item, i) {
switch (item) {
case 'g':
color = [Number.parseFloat(sliced_1[i - 1])];
break;
case 'rg':
color = [Number.parseFloat(sliced_1[i - 3]), Number.parseFloat(sliced_1[i - 2]), Number.parseFloat(sliced_1[i - 1])];
break;
case 'k':
color = [Number.parseFloat(sliced_1[i - 4]), Number.parseFloat(sliced_1[i - 3]), Number.parseFloat(sliced_1[i - 2]),
Number.parseFloat(sliced_1[i - 1])];
break;
case 'Tf':
fontSize = Number.parseFloat(sliced_1[i - 1]);
fontName = sliced_1[i - 2].substring(1);
if (fontName.includes('#2C')) {
fontName.replace('#2C', ',');
}
break;
}
});
}
this.fontName = fontName;
this.fontSize = fontSize;
this.color = (typeof color !== 'undefined') ? _parseColor(color) : { r: 0, g: 0, b: 0 };
}
_PdfDefaultAppearance.prototype.toString = function () {
var color = [Number.parseFloat((this.color.r / 255).toFixed(3)),
Number.parseFloat((this.color.g / 255).toFixed(3)),
Number.parseFloat((this.color.b / 255).toFixed(3))];
return '/' +
this.fontName +
' ' +
this.fontSize +
' Tf ' +
color[0].toString() +
' ' +
color[1].toString() +
' ' +
color[2].toString() +
' rg';
};
return _PdfDefaultAppearance;
}());
export { _PdfDefaultAppearance };