@syncfusion/ej2-pdfviewer
Version:
Essential JS 2 PDF viewer Component
827 lines • 122 kB
JavaScript
import { isNullOrUndefined } from '@syncfusion/ej2-base';
import { PdfTextBoxField, PdfFormFieldVisibility, PdfTextAlignment, PdfSignatureField, PdfFreeTextAnnotation, PdfFontFamily, PdfStandardFont, PdfAnnotationFlag, PdfRubberStampAnnotation, PdfBitmap, PdfFontStyle as FontStyle, PdfCheckBoxField, PdfComboBoxField, PdfListBoxField, PdfListFieldItem, PdfRadioButtonListField, PdfRadioButtonListItem, PdfRotationAngle, PdfFontStyle, PdfInkAnnotation, PdfTrueTypeFont, PdfPath } from '@syncfusion/ej2-pdf';
import { PageRenderer } from '../index';
import { getArialFontData } from '../pdf-base/fontData';
import { PdfViewerUtils } from '../base/pdfviewer-utlis';
/**
* FormFieldsBase
*
* @hidden
*/
var FormFieldsBase = /** @class */ (function () {
/**
* @param {PdfViewer} pdfViewer - The PdfViewer.
* @param {PdfViewerBase} pdfViewerBase - The PdfViewerBase.
* @param {boolean} digitalSignatruePresent - The digitalSignatruePresent
* @private
* @returns {void}
*/
function FormFieldsBase(pdfViewer, pdfViewerBase, digitalSignatruePresent) {
/**
* @private
*/
this.PdfRenderedFormFields = [];
this.pdfViewer = pdfViewer;
this.pdfViewerBase = pdfViewerBase;
this.formFieldLoadedDocument = this.pdfViewer.pdfRendererModule.loadedDocument;
this.mIsDigitalSignaturePresent = digitalSignatruePresent;
}
/**
* @private
* @param {any} textSignature - This is textSignature
* @param {any} loadedDocument - loadedDocument
* @param {boolean} isAnnotationFlattern - isAnnotationFlattern
* @returns {void}
*/
FormFieldsBase.prototype.drawFreeTextAnnotations = function (textSignature, loadedDocument, isAnnotationFlattern) {
var stampObjects = textSignature.data;
var textData = stampObjects.replace(/"/g, '');
var boundsObject = JSON.parse(textSignature.bounds);
var page = loadedDocument.getPage(textSignature.pageIndex);
if (stampObjects !== '') {
var left = this.convertPixelToPoint(boundsObject.left);
var top_1 = this.convertPixelToPoint(boundsObject.top);
var width = this.convertPixelToPoint(boundsObject.width);
var height = this.convertPixelToPoint(boundsObject.height);
var annotation = new PdfFreeTextAnnotation(left, top_1, width, height);
annotation._dictionary.set('NM', textSignature.signatureName.toString());
var fontSize = textSignature.fontSize;
annotation.border.width = 0;
var fontFamilyEnum = PdfFontFamily.helvetica;
var fontName = textSignature.fontFamily.toString();
if (!isNullOrUndefined(fontName)) {
var family = fontName.toString();
if (family.includes('Times New Roman')) {
fontFamilyEnum = PdfFontFamily.timesRoman;
}
else if (family.includes('Courier')) {
fontFamilyEnum = PdfFontFamily.courier;
}
else if (family.includes('Symbol')) {
fontFamilyEnum = PdfFontFamily.symbol;
}
else if (family.includes('ZapfDingbats')) {
fontFamilyEnum = PdfFontFamily.zapfDingbats;
}
}
fontSize = Math.floor(this.convertPixelToPoint(fontSize));
var fontStyle = FontStyle.regular;
annotation.font = new PdfStandardFont(fontFamilyEnum, fontSize, fontStyle);
annotation.text = textData;
annotation.borderColor = [0, 0, 0];
annotation.textAlignment = PdfTextAlignment.center;
annotation._annotFlags = PdfAnnotationFlag.print;
if (isAnnotationFlattern) {
var rotateAngle = this.getRotateAngle(page.rotation);
annotation.rotationAngle = Math.abs(rotateAngle);
}
annotation.setValues('AnnotationType', 'Signature');
if (isAnnotationFlattern) {
annotation.flatten = true;
}
annotation.setAppearance(true);
page.annotations.add(annotation);
}
};
FormFieldsBase.prototype.getRotateAngle = function (angleString) {
var angle = 0;
switch (angleString) {
case PdfRotationAngle.angle0:
angle = 0;
break;
case PdfRotationAngle.angle180:
angle = 2;
break;
case PdfRotationAngle.angle270:
angle = 3;
break;
case PdfRotationAngle.angle90:
angle = 1;
break;
}
return angle;
};
/**
* @private
* @param {any} signatureImage - signatureImage
* @param {any} loadedDocument - loadedDocument
* @param {boolean} isAnnotationFlattern - isAnnotationFlattern
* @returns {void}
*/
FormFieldsBase.prototype.drawImage = function (signatureImage, loadedDocument, isAnnotationFlattern) {
var _a;
var stampObjects = signatureImage.data;
var boundsObject = JSON.parse(signatureImage.bounds);
var page = loadedDocument.getPage(signatureImage.pageIndex);
if (stampObjects !== '') {
var imageUrl = (stampObjects.toString()).split(',')[1];
var left = this.convertPixelToPoint(boundsObject.left);
var top_2 = this.convertPixelToPoint(boundsObject.top);
var width = this.convertPixelToPoint(boundsObject.width);
var height = this.convertPixelToPoint(boundsObject.height);
if (page.rotation === PdfRotationAngle.angle90 || page.rotation === PdfRotationAngle.angle270) {
_a = [height, width], width = _a[0], height = _a[1];
}
var rubberStampAnnotation = new PdfRubberStampAnnotation(left, top_2, width, height);
var bitmap = new PdfBitmap(imageUrl);
var graphics = page.graphics;
var appearance = rubberStampAnnotation.appearance.normal;
rubberStampAnnotation._dictionary.set('NM', signatureImage.signatureName.toString());
var rotationAngle = this.getRotateAngle(page.rotation);
rubberStampAnnotation.rotationAngle = Math.abs(rotationAngle);
if (isAnnotationFlattern) {
rubberStampAnnotation.flatten = true;
}
if (!isAnnotationFlattern) {
var state = graphics.save();
appearance.graphics.drawImage(bitmap, 0, 0, width, height);
appearance.graphics.restore(state);
}
else {
appearance.graphics.drawImage(bitmap, 0, 0, width, height);
}
page.annotations.add(rubberStampAnnotation);
}
};
/**
* @private
* @param {any} jsonObject - jsonObject
* @returns {void}
*/
FormFieldsBase.prototype.saveFormFieldsDesignerData = function (jsonObject) {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'formDesigner')) {
var formFields = jsonObject['formDesigner'];
if (!isNullOrUndefined(formFields)) {
var data = JSON.parse(formFields);
var myList = [];
var formFieldsPageList = Object.prototype.hasOwnProperty.call(jsonObject, 'formFieldsPageList') ? JSON.parse(jsonObject['formFieldsPageList']) : myList;
//Removing form fields from the page.
if (!isNullOrUndefined(this.formFieldLoadedDocument.form)) {
var initialCount = this.formFieldLoadedDocument.form._fields.length;
//Get the loaded form.
var loadedForm = this.formFieldLoadedDocument.form;
for (var k = initialCount - 1; k >= 0; k--) {
var formFieldPage = loadedForm.fieldAt(k);
var signField = null;
if (formFieldPage instanceof PdfSignatureField) {
signField = formFieldPage;
}
var signed = !isNullOrUndefined(signField) ? signField.isSigned : true;
//Removing the formfields from a page
// if (formFieldsPageList.includes(pageNumber + 1) && (signField === null || !signed)) {
// formFieldsPageList is did not removed when delete non rendered pages form fields.
if (signField == null || !signed) {
loadedForm.removeField(loadedForm.fieldAt(k));
}
}
}
for (var i = 0; i < data.length; i++) {
this.addFormFieldsToDocument(data[parseInt(i.toString(), 10)].FormField);
}
}
if (!isNullOrUndefined(this.formFieldLoadedDocument.form)) {
if (!isNullOrUndefined(this.defaultAppearanceFields)) {
for (var i = 0; i < this.formFieldLoadedDocument.form.count; i++) {
var field = this.formFieldLoadedDocument.form.fieldAt(i);
if (this.defaultAppearanceFields.indexOf(field.name) === -1) {
field.setAppearance(true);
}
}
this.defaultAppearanceFields = null;
}
else {
this.formFieldLoadedDocument.form.setDefaultAppearance(false);
}
}
}
};
FormFieldsBase.prototype.setFont = function (field, currentField) {
var pdfFontStyle = this.getFontStyle(field);
currentField._dictionary.set('FontStyle', pdfFontStyle);
// eslint-disable-next-line
var hasUnicode = /[^\u0000-\u007F]/.test(currentField.text);
if (hasUnicode) {
currentField.font = this.getTrueFont(field.fontSize, pdfFontStyle);
}
else {
currentField.font = new PdfStandardFont(this.getFontFamily(field.FontFamily), this.convertPixelToPoint(field.fontSize), pdfFontStyle);
}
};
/**
* @private
* @param {any} jsonObject - jsonObject
* @returns {void}
*/
FormFieldsBase.prototype.saveFormFieldsData = function (jsonObject) {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'fieldsData')) {
var formFields = jsonObject['fieldsData'];
var data = JSON.parse(formFields);
if (!isNullOrUndefined(data) && Object.keys(data).length > 0 && !isNullOrUndefined(this.formFieldLoadedDocument.form)) {
if (this.formFieldLoadedDocument.form._fields.length > 0) {
this.formFieldLoadedDocument.form.setDefaultAppearance(false);
}
for (var i = 0; i < this.formFieldLoadedDocument.form._fields.length; i++) {
var currentField = this.formFieldLoadedDocument.form.fieldAt(i);
var currentFieldName = '';
var actualFieldName = '';
if (!isNullOrUndefined(currentField.name)) {
currentFieldName = currentField.name.replace(/[^0-9a-zA-Z]+/g, '').replace(/\s+/g, '');
actualFieldName = currentField.name;
}
if (currentField instanceof PdfTextBoxField) {
if (!currentField.password) {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var field = data["" + currentFieldName];
if (!isNullOrUndefined(field) && Object.prototype.hasOwnProperty.call(field, 'isReadOnly')) {
currentField.text = field['fieldValue'];
currentField.readOnly = field['isReadOnly'] === 'true' ? true : false;
}
this.setFont(field, currentField);
}
}
else {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var field = data["" + currentFieldName];
if (!isNullOrUndefined(field) && Object.prototype.hasOwnProperty.call(field, 'isReadOnly')) {
currentField.text = field['fieldValue'];
currentField.readOnly = field['isReadOnly'] === 'true' ? true : false;
}
this.setFont(field, currentField);
}
}
}
else if (currentField instanceof PdfComboBoxField) {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var field = data["" + currentFieldName];
var count = currentField.itemsCount;
var fieldName = '';
if (!isNullOrUndefined(field)) {
if (Object.prototype.hasOwnProperty.call(field, 'isReadOnly')) {
currentField.readOnly = field['isReadOnly'] === 'true' ? true : false;
}
if (!isNullOrUndefined(field['fieldValue'])) {
fieldName = field['fieldValue'];
}
}
var isExists = false;
for (var j = 0; j < count; j++) {
var optionArray = void 0;
var text = void 0;
if (currentField._dictionary.has('Opt')) {
optionArray = currentField._dictionary.get('Opt');
text = optionArray[parseInt(j.toString(), 10)];
}
else if (!isNullOrUndefined(currentField.itemAt(j))) {
text = currentField.itemAt(j).text;
}
if (text === fieldName || (text && text.length > 0 && text[1] === fieldName)) {
currentField.selectedIndex = j;
isExists = true;
}
}
if (currentField.editable && !isExists) {
currentField.selectedValue = fieldName;
}
this.setFont(field, currentField);
}
}
else if (currentField instanceof PdfCheckBoxField) {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var field = data["" + currentFieldName];
var fields = field['isSelected'];
var fieldValueString = fields.toString();
var fieldValue = field['fieldValue'];
if (!isNullOrUndefined(fieldValue)) {
currentField._dictionary.set('ExportValue', fieldValue);
}
if (fieldValueString.toLowerCase() === 'true' || fieldValueString.toLowerCase() === 'false') {
currentField.checked = fields;
if (!isNullOrUndefined(field) && Object.prototype.hasOwnProperty.call(field, 'isReadOnly')) {
currentField.readOnly = field.readonly;
}
if (fieldValueString.toLowerCase() === 'false') {
var checkBoxField = currentField;
for (var k = 0; k < checkBoxField.itemsCount; k++) {
checkBoxField.itemAt(k).checked = false;
}
currentField.checked = false;
}
}
else {
var integerValue = isNullOrUndefined(fieldValueString) ? -1 : parseInt(fieldValueString, 10);
var checkBoxField = currentField;
if (checkBoxField.itemsCount > 0) {
if (integerValue === -1) {
for (var n = 0; n < checkBoxField.itemsCount; n++) {
checkBoxField.itemAt(n).checked = false;
}
currentField.checked = false;
}
else if (!isNullOrUndefined(checkBoxField.itemAt(integerValue))) {
checkBoxField.itemAt(integerValue).checked = true;
}
}
}
}
}
else if (currentField instanceof PdfListBoxField) {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var table = data["" + currentFieldName];
var count = currentField.itemsCount;
var fieldName = '';
if (!isNullOrUndefined(table)) {
if (Object.prototype.hasOwnProperty.call(table, 'fieldValue') && !isNullOrUndefined(table['fieldValue'])) {
fieldName = table['fieldValue'];
}
if (Object.prototype.hasOwnProperty.call(table, 'isReadOnly')) {
currentField.readOnly = table['isReadOnly'] === 'true' ? true : false;
}
}
fieldName = JSON.parse(fieldName)[0].replace(/[^0-9a-zA-Z]+/g, '');
var selectedIndexes = [];
for (var k = 0; k < count; k++) {
var text = currentField.itemAt(k).text;
if (text === fieldName) {
selectedIndexes.push(k);
}
}
currentField.selectedIndex = selectedIndexes;
this.setFont(table, currentField);
}
}
else if (currentField instanceof PdfRadioButtonListField) {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var field = data["" + currentFieldName];
if (!isNullOrUndefined(field) && Object.prototype.hasOwnProperty.call(field, 'isReadOnly')) {
var selectedValue = field['fieldValue'];
if (selectedValue) {
for (var i_1 = 0; i_1 < currentField.itemsCount; i_1++) {
var item = currentField.itemAt(i_1);
if (item && (item.value === selectedValue || item._optionValue === selectedValue)) {
currentField.selectedIndex = i_1;
break;
}
}
}
currentField.readOnly = field['isReadOnly'] === 'true' ? true : false;
}
}
}
else if (currentField instanceof PdfSignatureField) {
if ((Object.prototype.hasOwnProperty.call(data, currentFieldName) && !isNullOrUndefined(data["" + currentFieldName])) || (Object.prototype.hasOwnProperty.call(data, actualFieldName) && !isNullOrUndefined(data["" + actualFieldName]))) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName)) {
currentFieldName = actualFieldName;
}
var signatureFields = currentField;
if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'fontName')) {
this.drawFieldFreeTextAnnotations(data["" + currentFieldName], signatureFields, currentFieldName, data[currentFieldName + 'bounds'], data[currentFieldName + 'fontName'], data[currentFieldName + 'fontSize']);
}
else if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'ImageData')) {
this.drawFieldImage(data["" + currentFieldName], signatureFields, currentFieldName, data[currentFieldName + 'bounds']);
}
else if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'bounds')) {
this.drawFieldPath(data["" + currentFieldName], signatureFields, currentFieldName, data[currentFieldName + 'bounds']);
}
var signatureFieldListCount = signatureFields.itemsCount;
if (signatureFieldListCount > 0) {
for (var k = 0; k < signatureFieldListCount; k++) {
if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'fontName' + '_' + k)) {
this.drawFieldFreeTextAnnotations(data["" + currentFieldName], signatureFields, currentFieldName, data[currentFieldName + 'bounds' + '_' + k], data[currentFieldName + 'fontName' + '_' + k], data[currentFieldName + 'fontSize' + '_' + k]);
}
else if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'ImageData' + '_' + k)) {
this.drawFieldImage(data["" + currentFieldName], signatureFields, currentFieldName, data[currentFieldName + 'bounds' + '_' + k]);
}
else if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'bounds' + '_' + k)) {
this.drawFieldPath(data["" + currentFieldName], signatureFields, currentFieldName, data[currentFieldName + 'bounds' + '_' + k]);
}
}
}
}
if (Object.prototype.hasOwnProperty.call(data, currentFieldName + 'isReadOnly') || Object.prototype.hasOwnProperty.call(data, actualFieldName + 'isReadOnly')) {
if (Object.prototype.hasOwnProperty.call(data, actualFieldName + 'isReadOnly')) {
currentFieldName = actualFieldName;
}
currentField.readOnly = data['isReadOnly'] === 'true' ? true : false;
}
}
}
}
}
};
FormFieldsBase.prototype.addFormFieldsToDocument = function (formFieldAttributes) {
var loadedPage = this.formFieldLoadedDocument.getPage(formFieldAttributes.pageNumber - 1);
var field;
switch (formFieldAttributes.formFieldAnnotationType) {
case 'Textbox':
case 'PasswordField':
//Create a password and text box field for name
field = this.saveTextBoxField(loadedPage, formFieldAttributes);
break;
case 'Checkbox':
// Create Check Box field.
field = this.SaveCheckBoxField(loadedPage, formFieldAttributes);
break;
case 'RadioButton':
field = this.saveRadioButtonField(formFieldAttributes);
break;
case 'DropdownList':
// Create Drop Down field.
field = this.saveDropDownField(loadedPage, formFieldAttributes);
break;
case 'ListBox':
field = this.saveListBoxField(loadedPage, formFieldAttributes);
break;
case 'SignatureField':
case 'InitialField':
//Create PDF Signature and Initial field.
field = this.saveSignatureField(loadedPage, formFieldAttributes);
break;
}
if (field) {
this.formFieldLoadedDocument.form.add(field);
}
};
FormFieldsBase.prototype.setFontFromKeys = function (text, field, textFont, fontSize, hasUnicode, fontStyle) {
var font = PdfViewerUtils.tryGetFontFromKeys(textFont, text.toString(), fontSize, fontStyle);
if (!isNullOrUndefined(font)) {
field.font = font;
field.setAppearance(true);
}
else {
if (hasUnicode) {
var trueTypeFont = this.getTrueFont(fontSize, fontStyle);
var isGlyphPresent = PdfViewerUtils.isSupportedFont(text, font);
if (isGlyphPresent) {
field.font = trueTypeFont;
}
else {
this.disableFieldAppearance(field);
}
}
else {
this.disableFieldAppearance(field);
}
}
};
FormFieldsBase.prototype.setFontAppearance = function (text, fontFamily, fontSize, field, textFont, hasUnicode, fontStyle) {
if (!isNullOrUndefined(textFont) && Object.keys(textFont).length > 0) {
var fontKey = PdfViewerUtils.getFontKey(textFont, fontFamily.toLowerCase());
if (!isNullOrUndefined(fontKey)) {
var fontStream = textFont["" + fontKey];
fontStream = PdfViewerUtils.processFontStream(fontStream);
var font = new PdfTrueTypeFont(fontStream, this.convertPixelToPoint(fontSize), fontStyle);
var glyphPresent = PdfViewerUtils.isSupportedFont(text, font);
field.setAppearance(glyphPresent);
if (glyphPresent) {
field.font = font;
}
else {
this.setFontFromKeys(text, field, textFont, fontSize, hasUnicode, fontStyle);
}
}
else {
this.setFontFromKeys(text, field, textFont, fontSize, hasUnicode, fontStyle);
}
}
else {
try {
field.font.measureString(text.toString());
}
catch (e) {
if (hasUnicode) {
var trueTypeFont = this.getTrueFont(fontSize, fontStyle);
var isGlyphPresent = PdfViewerUtils.isSupportedFont(text, trueTypeFont);
if (isGlyphPresent) {
field.font = trueTypeFont;
}
else {
this.disableFieldAppearance(field);
}
}
else {
this.disableFieldAppearance(field);
}
}
}
};
FormFieldsBase.prototype.disableFieldAppearance = function (field) {
field.setAppearance(false);
if (isNullOrUndefined(this.defaultAppearanceFields)) {
this.defaultAppearanceFields = [];
}
this.defaultAppearanceFields.push(field.name);
};
FormFieldsBase.prototype.saveTextBoxField = function (loadedPage, formFieldAttributes) {
var textboxName = isNullOrUndefined(formFieldAttributes.name) ? formFieldAttributes.type === 'Password' ? 'passwordTextbox' : 'textbox' : formFieldAttributes.name;
var textBounds = this.convertFieldBounds(formFieldAttributes);
var rotationAngle = loadedPage.rotation;
var isFieldRotated = false;
if (formFieldAttributes.rotation !== 0) {
isFieldRotated = true;
}
var fieldBounds = this.getBounds(textBounds, loadedPage.size[1], loadedPage.size[0], rotationAngle, isFieldRotated);
var bound = { x: fieldBounds.X, y: fieldBounds.Y, width: fieldBounds.Width, height: fieldBounds.Height };
//Create a new text box field
var textbox = new PdfTextBoxField(loadedPage, textboxName, bound);
textbox.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b];
if (formFieldAttributes.backgroundColor.r === 0 && formFieldAttributes.backgroundColor.g === 0 &&
formFieldAttributes.backgroundColor.b === 0 && formFieldAttributes.backgroundColor.a === 0) {
textbox.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b, formFieldAttributes.backgroundColor.a];
}
textbox.maxLength = formFieldAttributes.maxLength;
textbox.insertSpaces = formFieldAttributes.insertSpaces;
textbox.readOnly = formFieldAttributes.isReadonly;
textbox.required = formFieldAttributes.isRequired;
textbox.textAlignment = this.getTextAlignment(formFieldAttributes.textAlign);
textbox.visibility = this.getFormFieldsVisibility(formFieldAttributes.visibility);
textbox.text = isNullOrUndefined(formFieldAttributes.value) ? '' : formFieldAttributes.value;
textbox.toolTip = isNullOrUndefined(formFieldAttributes.tooltip) ? '' : formFieldAttributes.tooltip;
textbox.color = [formFieldAttributes.fontColor.r, formFieldAttributes.fontColor.g, formFieldAttributes.fontColor.b];
textbox.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g, formFieldAttributes.borderColor.b];
// eslint-disable-next-line
if (formFieldAttributes.borderColor.r == 0 && formFieldAttributes.borderColor.g == 0 &&
// eslint-disable-next-line
formFieldAttributes.borderColor.b == 0 && formFieldAttributes.borderColor.a == 0) {
textbox.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g,
formFieldAttributes.borderColor.b, formFieldAttributes.borderColor.a];
}
textbox.border.width = formFieldAttributes.thickness;
textbox.multiLine = formFieldAttributes.Multiline;
var pdfFontStyle = this.getFontStyle(formFieldAttributes);
textbox._dictionary.set('FontStyle', pdfFontStyle);
// eslint-disable-next-line
var hasUnicode = /[^\u0000-\u007F]/.test(textbox.text);
var fontFamily = formFieldAttributes.FontFamily ? formFieldAttributes.FontFamily : formFieldAttributes.fontFamily;
textbox.font = new PdfStandardFont(this.getFontFamily(fontFamily), this.convertPixelToPoint(formFieldAttributes.fontSize), pdfFontStyle);
if (!isNullOrUndefined(textbox.text.toString())) {
var textFont = this.pdfViewer.pdfRenderer.FallbackFontCollection;
this.setFontAppearance(textbox.text.toString(), formFieldAttributes.fontFamily, formFieldAttributes.fontSize, textbox, textFont, hasUnicode, pdfFontStyle);
}
if (formFieldAttributes.formFieldAnnotationType === 'PasswordField') {
textbox.password = true;
}
if (!isFieldRotated) {
textbox.rotate = this.getFormfieldRotation(loadedPage.rotation);
}
if (!isNullOrUndefined(formFieldAttributes.customData)) {
var customData = JSON.stringify(formFieldAttributes.customData);
textbox._dictionary.set('CustomData', customData);
}
return textbox;
};
FormFieldsBase.prototype.saveDropDownField = function (loadedPage, formFieldAttributes) {
var dropdownListName = isNullOrUndefined(formFieldAttributes.name) ? 'dropdownList' : formFieldAttributes.name;
var dropDownListbounds = this.convertFieldBounds(formFieldAttributes);
var rotationAngle = loadedPage.rotation;
var isFieldRotated = false;
if (formFieldAttributes.rotation !== 0) {
isFieldRotated = true;
}
var fieldBounds = this.getBounds(dropDownListbounds, loadedPage.size[1], loadedPage.size[0], rotationAngle, isFieldRotated);
var bound = { x: fieldBounds.X, y: fieldBounds.Y, width: fieldBounds.Width, height: fieldBounds.Height };
var comboBox = new PdfComboBoxField(loadedPage, dropdownListName, bound);
var hasUnicode = false;
for (var i = 0; i < formFieldAttributes.option.length; i++) {
var item = new PdfListFieldItem(formFieldAttributes.option[parseInt(i.toString(), 10)].itemName, formFieldAttributes.option[parseInt(i.toString(), 10)].itemValue);
comboBox.addItem(item);
// eslint-disable-next-line
var flag = /[^\u0000-\u007F]/.test(formFieldAttributes.option[parseInt(i.toString(), 10)].itemName);
if (flag) {
hasUnicode = true;
}
}
comboBox.textAlignment = this.getTextAlignment(formFieldAttributes.textAlign);
var pdfFontStyle = this.getFontStyle(formFieldAttributes);
comboBox._dictionary.set('FontStyle', pdfFontStyle);
var fontFamily = formFieldAttributes.FontFamily ? formFieldAttributes.FontFamily : formFieldAttributes.fontFamily;
comboBox.font = new PdfStandardFont(this.getFontFamily(fontFamily), this.convertPixelToPoint(formFieldAttributes.fontSize), pdfFontStyle);
for (var i = 0; i < formFieldAttributes.option.length; i++) {
var comboBoxText = formFieldAttributes.option[parseInt(i.toString(), 10)].itemName.toString();
if (!isNullOrUndefined(comboBoxText)) {
var textFont = this.pdfViewer.pdfRenderer.FallbackFontCollection;
this.setFontAppearance(comboBoxText, formFieldAttributes.fontFamily, formFieldAttributes.fontSize, comboBox, textFont, hasUnicode, pdfFontStyle);
break;
}
}
if (comboBox.itemsCount > 0) {
if (formFieldAttributes.selectedIndex.length > 0) {
comboBox.selectedIndex = formFieldAttributes.selectedIndex[0];
}
else {
comboBox.selectedIndex = 0;
}
}
comboBox.required = formFieldAttributes.isRequired;
comboBox.readOnly = formFieldAttributes.isReadonly;
comboBox.visibility = this.getFormFieldsVisibility(formFieldAttributes.visibility);
comboBox.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b];
if (formFieldAttributes.backgroundColor.r === 0 && formFieldAttributes.backgroundColor.g === 0 &&
formFieldAttributes.backgroundColor.b === 0 && formFieldAttributes.backgroundColor.a === 0) {
comboBox.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b, formFieldAttributes.backgroundColor.a];
}
comboBox.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g, formFieldAttributes.borderColor.b];
// eslint-disable-next-line
if (formFieldAttributes.borderColor.r == 0 && formFieldAttributes.borderColor.g == 0 &&
// eslint-disable-next-line
formFieldAttributes.borderColor.b == 0 && formFieldAttributes.borderColor.a == 0) {
comboBox.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g,
formFieldAttributes.borderColor.b, formFieldAttributes.borderColor.a];
}
comboBox.border.width = formFieldAttributes.thickness;
comboBox.color = [formFieldAttributes.fontColor.r, formFieldAttributes.fontColor.g, formFieldAttributes.fontColor.b];
if (!isFieldRotated) {
comboBox.rotate = this.getFormfieldRotation(loadedPage.rotation);
}
comboBox.toolTip = isNullOrUndefined(formFieldAttributes.tooltip) ? '' : formFieldAttributes.tooltip;
if (!isNullOrUndefined(formFieldAttributes.customData)) {
var customData = JSON.stringify(formFieldAttributes.customData);
comboBox._dictionary.set('CustomData', customData);
}
return comboBox;
};
FormFieldsBase.prototype.SaveCheckBoxField = function (loadedPage, formFieldAttributes) {
var checkboxFieldName = isNullOrUndefined(formFieldAttributes.name) && formFieldAttributes.name === '' ? 'checkboxField' : formFieldAttributes.name;
var checkBounds = this.convertFieldBounds(formFieldAttributes);
var rotationAngle = loadedPage.rotation;
var isFieldRotated = false;
if (formFieldAttributes.rotation !== 0) {
isFieldRotated = true;
}
var fieldBounds = this.getBounds(checkBounds, loadedPage.size[1], loadedPage.size[0], rotationAngle, isFieldRotated);
var bound = { x: fieldBounds.X, y: fieldBounds.Y, width: fieldBounds.Width, height: fieldBounds.Height };
//Create a new Check box field
var checkBoxField = new PdfCheckBoxField(checkboxFieldName, bound, loadedPage);
checkBoxField.readOnly = formFieldAttributes.isReadonly;
checkBoxField.required = formFieldAttributes.isRequired;
checkBoxField.checked = formFieldAttributes.isChecked;
checkBoxField.visibility = this.getFormFieldsVisibility(formFieldAttributes.visibility);
checkBoxField._dictionary.set('ExportValue', formFieldAttributes.value);
checkBoxField.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b];
if (formFieldAttributes.backgroundColor.r === 0 && formFieldAttributes.backgroundColor.g === 0 &&
formFieldAttributes.backgroundColor.b === 0 && formFieldAttributes.backgroundColor.a === 0) {
checkBoxField.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b, formFieldAttributes.backgroundColor.a];
}
checkBoxField.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g,
formFieldAttributes.borderColor.b];
if (formFieldAttributes.borderColor.r === 0 && formFieldAttributes.borderColor.g === 0 && formFieldAttributes.borderColor.b === 0
&& formFieldAttributes.borderColor.a === 0) {
checkBoxField.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g,
formFieldAttributes.borderColor.b, formFieldAttributes.borderColor.a];
}
checkBoxField.border.width = formFieldAttributes.thickness;
checkBoxField.toolTip = isNullOrUndefined(formFieldAttributes.tooltip) ? '' : formFieldAttributes.tooltip;
if (!isFieldRotated) {
checkBoxField.rotate = this.getFormfieldRotation(loadedPage.rotation);
}
if (!isNullOrUndefined(formFieldAttributes.customData)) {
var customData = JSON.stringify(formFieldAttributes.customData);
checkBoxField._dictionary.set('CustomData', customData);
}
return checkBoxField;
};
FormFieldsBase.prototype.saveListBoxField = function (loadedPage, formFieldAttributes) {
var listBoxName = isNullOrUndefined(formFieldAttributes.name) ? 'listBox' : formFieldAttributes.name;
var listBounds = this.convertFieldBounds(formFieldAttributes);
var rotationAngle = loadedPage.rotation;
var isFieldRotated = false;
if (formFieldAttributes.rotation !== 0) {
isFieldRotated = true;
}
var fieldBounds = this.getBounds(listBounds, loadedPage.size[1], loadedPage.size[0], rotationAngle, isFieldRotated);
var bound = { x: fieldBounds.X, y: fieldBounds.Y, width: fieldBounds.Width, height: fieldBounds.Height };
var listBox = new PdfListBoxField(loadedPage, listBoxName, bound);
var flag = false;
var hasUnicode = false;
for (var i = 0; i < formFieldAttributes.option.length; i++) {
var item = new PdfListFieldItem(formFieldAttributes.option[parseInt(i.toString(), 10)].itemName, formFieldAttributes.option[parseInt(i.toString(), 10)].itemValue);
listBox.addItem(item);
// eslint-disable-next-line
var unicode = /[^\u0000-\u007F]/.test(formFieldAttributes.option[parseInt(i.toString(), 10)].itemName);
if (unicode) {
hasUnicode = true;
}
if (!isNullOrUndefined(item && item._dictionary && !flag)) {
item.textAlignment = this.getTextAlignment(formFieldAttributes.textAlign);
flag = true;
}
}
if (listBox.itemsCount > 0) {
var count = formFieldAttributes.selectedIndex.length;
if (Array.isArray(formFieldAttributes.selectedIndex) && count > 0) {
if (count === 1) {
listBox.selectedIndex = formFieldAttributes.selectedIndex[0];
}
else {
var selectedIndexes = [];
for (var j = 0; j < count; j++) {
selectedIndexes.push(formFieldAttributes.selectedIndex[parseInt(j.toString(), 10)]);
}
listBox.selectedIndex = selectedIndexes;
}
}
else {
listBox.selectedIndex = 0;
}
}
listBox.textAlignment = this.getTextAlignment(formFieldAttributes.textAlign);
listBox.multiSelect = true;
listBox.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b];
if (formFieldAttributes.backgroundColor.r === 0 && formFieldAttributes.backgroundColor.g === 0 &&
formFieldAttributes.backgroundColor.b === 0 && formFieldAttributes.backgroundColor.a === 0) {
listBox.backColor = [formFieldAttributes.backgroundColor.r, formFieldAttributes.backgroundColor.g,
formFieldAttributes.backgroundColor.b, formFieldAttributes.backgroundColor.a];
}
listBox.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g, formFieldAttributes.borderColor.b];
// eslint-disable-next-line
if (formFieldAttributes.borderColor.r == 0 && formFieldAttributes.borderColor.g == 0 &&
// eslint-disable-next-line
formFieldAttributes.borderColor.b == 0 && formFieldAttributes.borderColor.a == 0) {
listBox.borderColor = [formFieldAttributes.borderColor.r, formFieldAttributes.borderColor.g,
formFieldAttributes.borderColor.b, formFieldAttributes.borderColor.a];
}
listBox.border.width = formFieldAttributes.thickness;
var pdfFontStyle = this.getFontStyle(formFieldAttributes);
listBox._dictionary.set('FontStyle', pdfFontStyle);
var fontFamily = formFieldAttributes.FontFamily ? formFieldAttributes.FontFamily : formFieldAttributes.fontFamily;
listBox.font = new PdfStandardFont(this.getFontFamily(fontFamily), this.convertPixelToPoint(formFieldAttributes.fontSize), pdfFontStyle);
for (var i = 0; i < formFieldAttributes.option.length; i++) {
var listBoxText = formFieldAttributes.option[parseInt(i.toString(), 10)].itemName.toString();
if (!isNullOrUndefined(listBoxText)) {
var textFont = this.pdfViewer.pdfRenderer.FallbackFontCollection;
this.setFontAppearance(listBoxText, formFieldAttributes.fontFamily, formFieldAttributes.fontSize, listBox, textFont, hasUnicode, pdfFontStyle);
break;
}
}
listBox.readOnly = formFieldAttributes.isReadonly;
listBox.required = formFieldAttributes.isRequired;
listBox.visibility = this.getFormFieldsVisibility(formFieldAttributes.visibility);
listBox.toolTip = isNullOrUndefined(formFieldAttributes.tooltip) ? '' : formFieldAttributes.tooltip;
if (!isFieldRotated) {
listBox.rotate = this.getFormfieldRotation(loadedPage.rotation);
}
if (!isNullOrUndefined(formFieldAttributes.customData)) {
var customData = JSON.stringify(formFieldAttributes.customData);
listBox._dictionary.set('CustomData', customData);
}
return listBox;
};
FormFieldsBase.prototype.saveRadioButtonField = function (formFieldAttributes) {
var loadedPage = this.formFieldLoadedDocument.getPage(formFieldAttributes.pageNumber - 1);
var fieldName = isNullOrUndefined(formFieldAttributes.radiobuttonItem[0].name) ? 'radiobuttonField' : formFieldAttributes.radiobuttonItem[0].name;
var field = new PdfRadioButtonListField(loadedPage, fieldName);
var selectedIndex = 0;
var isSelectedItem = false;
var isReadOnly = false;
var isRequired = false;
for (var i = 0; i < formFieldAttributes.radiobuttonItem.length; i++) {
var radiobuttonItem = formFieldAttributes.radiobuttonItem[parseInt(i.toString(), 10)];
var page = this.formFieldLoadedDocument.getPage(radiobuttonItem.pageNumber - 1);
var radioButtonName = !(isNullOrUndefined(radiobuttonItem.value) || radiobuttonItem.value === '') ? radiobuttonItem.value : fieldName;
var rotationAngle = this.getRotateAngle(page.rotation);
var bounds = this.convertFieldBounds(radiobuttonItem);
var isFieldRotated = false;
if (formFieldAttributes.rotation !== 0) {
isFieldRotated = true;
}
var fieldBounds = this.getBounds(bounds, page.size[1], page.size[0], rotationAngle, isFieldRotated);
var bound = { x: fieldBounds.X, y: fieldBounds.Y, width: fieldBounds.Width, height: fieldBounds.Height };
var radioButtonItem = new PdfRadioButtonListItem(radioButtonName, bound, page);
if (isFieldRotated) {
radioButtonItem.rotationAngle = this.GetRotateAngle(page.rotation);
}
if (radiobuttonItem.isReadonly) {
isReadOnly = true;
}
if (radiobuttonItem.isRequired) {
isRequired = true;
}
radioButtonItem.borderColor = [radiobuttonItem.borderColor.r, radiobuttonItem.borderColor.g, radiobuttonItem.borderColor.b];
// eslint-disable-next-line
if (radiobuttonItem.borderColor.r == 0 && radiobuttonItem.borderColor.g == 0 &&
// eslint-disable-next-line
radiobuttonItem.borderCol