@syncfusion/ej2-pdfviewer
Version:
Essential JS 2 PDF viewer Component
812 lines (811 loc) • 85.7 kB
JavaScript
import { FontStyle } from './../base/types';
import { PdfViewerBase } from '../index';
import { isBlazor, isNullOrUndefined, SanitizeHtmlHelper } from '@syncfusion/ej2-base';
/**
* @hidden
*/
var FreeTextAnnotation = /** @class */ (function () {
function FreeTextAnnotation(pdfviewer, pdfViewerBase) {
/**
* @private
*/
this.inputBoxCount = 0;
/**
* @private
*/
this.isFreeTextValueChange = false;
/**
* @private
*/
this.isAddAnnotationProgramatically = false;
/**
* @private
*/
this.isInuptBoxInFocus = false;
/**
* @private
*/
this.freeTextPageNumbers = [];
/**
* @private
*/
this.selectedText = '';
/**
* @private
*/
this.isTextSelected = false;
this.selectionStart = 0;
this.selectionEnd = 0;
/**
* @private
*/
this.isBold = false;
/**
* @private
*/
this.isItalic = false;
/**
* @private
*/
this.isUnderline = false;
/**
* @private
*/
this.isStrikethrough = false;
this.isReadonly = false;
this.isMaximumWidthReached = false;
this.freeTextPaddingLeft = 4;
this.freeTextPaddingTop = 5;
this.defaultFontSize = 16;
this.lineGap = 1.5;
/**
* @private
*/
this.previousText = 'Type Here';
/**
* @private
*/
this.currentPosition = [];
this.pdfViewer = pdfviewer;
this.pdfViewerBase = pdfViewerBase;
this.updateTextProperties();
this.inputBoxElement = document.createElement('textarea');
this.inputBoxElement.style.position = 'absolute';
this.inputBoxElement.style.Width = this.defautWidth;
this.inputBoxElement.style.Height = this.defaultHeight;
this.inputBoxElement.style.zIndex = '5';
this.inputBoxElement.style.fontSize = this.fontSize + 'px';
this.inputBoxElement.className = 'free-text-input';
this.inputBoxElement.style.resize = 'none';
this.inputBoxElement.style.borderColor = this.borderColor;
this.inputBoxElement.style.background = this.fillColor;
this.inputBoxElement.style.borderStyle = this.borderStyle;
this.inputBoxElement.style.borderWidth = this.borderWidth + 'px';
this.inputBoxElement.style.padding = this.padding;
this.inputBoxElement.style.paddingLeft = this.freeTextPaddingLeft + 'px';
this.inputBoxElement.style.paddingTop = this.freeTextPaddingTop * (parseFloat(this.inputBoxElement.style.fontSize) / this.defaultFontSize) + 'px';
this.inputBoxElement.style.borderRadius = '2px';
this.inputBoxElement.style.verticalAlign = 'middle';
this.inputBoxElement.style.fontFamily = this.fontFamily;
this.inputBoxElement.style.color = this.pdfViewer.freeTextSettings.fontColor ?
this.pdfViewer.freeTextSettings.fontColor : '#000';
this.inputBoxElement.style.overflow = 'hidden';
this.inputBoxElement.style.wordBreak = this.wordBreak;
this.inputBoxElement.readOnly = this.isReadonly;
this.inputBoxElement.addEventListener('focusout', this.onFocusOutInputBox.bind(this));
this.inputBoxElement.addEventListener('keydown', this.onKeyDownInputBox.bind(this));
this.inputBoxElement.addEventListener('mouseup', this.onMouseUpInputBox.bind(this));
this.freeTextPageNumbers = [];
}
/**
* @private
* @returns {void}
*/
FreeTextAnnotation.prototype.updateTextProperties = function () {
this.defautWidth = this.pdfViewer.freeTextSettings.width ? this.pdfViewer.freeTextSettings.width : 151;
this.defaultHeight = this.pdfViewer.freeTextSettings.height ? this.pdfViewer.freeTextSettings.height : 24.6;
this.borderColor = this.pdfViewer.freeTextSettings.borderColor ? this.pdfViewer.freeTextSettings.borderColor : '#ffffff00';
this.fillColor = this.pdfViewer.freeTextSettings.fillColor ? this.pdfViewer.freeTextSettings.fillColor : '#fff';
this.borderStyle = this.pdfViewer.freeTextSettings.borderStyle ? this.pdfViewer.freeTextSettings.borderStyle : 'solid';
this.borderWidth = !isNullOrUndefined(this.pdfViewer.freeTextSettings.borderWidth) ?
this.pdfViewer.freeTextSettings.borderWidth : 1;
this.fontSize = this.pdfViewer.freeTextSettings.fontSize ? this.pdfViewer.freeTextSettings.fontSize : 16;
this.opacity = this.pdfViewer.freeTextSettings.opacity ? this.pdfViewer.freeTextSettings.opacity : 1;
this.fontColor = this.pdfViewer.freeTextSettings.fontColor ? this.pdfViewer.freeTextSettings.fontColor : '#000';
this.author = (this.pdfViewer.freeTextSettings.author && this.pdfViewer.freeTextSettings.author !== 'Guest') ? this.pdfViewer.freeTextSettings.author : this.pdfViewer.annotationSettings.author ? this.pdfViewer.annotationSettings.author : 'Guest';
if (!isNullOrUndefined(this.pdfViewer.annotationModule)) {
if (this.getRgbCode(this.borderColor).a === 0) {
this.borderWidth = 0;
}
}
if (this.pdfViewer.freeTextSettings.fontFamily) {
var fontName = this.pdfViewer.freeTextSettings.fontFamily;
if (fontName === 'Helvetica' || fontName === 'Times New Roman' || fontName === 'Courier' || fontName === 'Symbol' || fontName === 'ZapfDingbats') {
this.fontFamily = fontName;
}
else {
this.fontFamily = 'Helvetica';
}
}
else {
this.fontFamily = 'Helvetica';
}
this.textAlign = this.pdfViewer.freeTextSettings.textAlignment ? this.pdfViewer.freeTextSettings.textAlignment : 'Left';
this.defaultText = this.pdfViewer.freeTextSettings.defaultText ? this.pdfViewer.freeTextSettings.defaultText : 'Type here';
this.isReadonly = false;
if (this.pdfViewer.freeTextSettings.enableAutoFit) {
this.wordBreak = 'break-all';
this.padding = '2px';
}
else {
this.padding = '0px';
this.wordBreak = 'break-word';
}
if (this.pdfViewer.freeTextSettings.isLock || this.pdfViewer.annotationSettings.isLock
|| this.pdfViewer.freeTextSettings.isReadonly) {
this.isReadonly = true;
}
this.isBold = (this.pdfViewer.freeTextSettings.fontStyle & FontStyle.Bold) === FontStyle.Bold;
this.isItalic = (this.pdfViewer.freeTextSettings.fontStyle & FontStyle.Italic) === FontStyle.Italic;
this.isUnderline = (this.pdfViewer.freeTextSettings.fontStyle & FontStyle.Underline) === FontStyle.Underline;
this.isStrikethrough = (this.pdfViewer.freeTextSettings.fontStyle & FontStyle.Strikethrough) === FontStyle.Strikethrough;
};
/**
* @param {any} shapeAnnotations - It describes about the shape annotations
* @param {number} pageNumber - It describes about the page number value
* @param {boolean} isImportAction - It ensures whether the isImportAction is true or not
* @param {boolean} isAnnotOrderAction - It ensures whether the isAnnotOrderAction is true or not
* @private
* @returns {void}
*/
FreeTextAnnotation.prototype.renderFreeTextAnnotations = function (shapeAnnotations, pageNumber, isImportAction, isAnnotOrderAction) {
var isFreeTextAdded = false;
if (!isImportAction) {
for (var p = 0; p < this.freeTextPageNumbers.length; p++) {
if (this.freeTextPageNumbers[parseInt(p.toString(), 10)] === pageNumber) {
isFreeTextAdded = true;
break;
}
}
}
if (shapeAnnotations && (!isFreeTextAdded || isAnnotOrderAction)) {
if (shapeAnnotations.length >= 1) {
this.freeTextPageNumbers.push(pageNumber);
for (var i = 0; i < shapeAnnotations.length; i++) {
var annotation = shapeAnnotations[parseInt(i.toString(), 10)];
annotation.annotationAddMode = this.pdfViewer.annotationModule.
findAnnotationMode(annotation, pageNumber, annotation.AnnotType);
if (annotation.AnnotType) {
var vertexPoints = null;
if (annotation.VertexPoints) {
vertexPoints = [];
for (var j = 0; j < annotation.VertexPoints.length; j++) {
var point = { x: annotation.VertexPoints[parseInt(j.toString(), 10)].X,
y: annotation.VertexPoints[parseInt(j.toString(), 10)].Y };
vertexPoints.push(point);
}
}
annotation.AnnotationSettings = annotation.AnnotationSettings ?
annotation.AnnotationSettings : this.pdfViewer.annotationModule.updateSettings(this.pdfViewer.freeTextSettings);
if (annotation.IsLocked) {
annotation.AnnotationSettings.isLock = annotation.IsLocked;
}
var paddingValue = 0.5;
var annotationBoundsX = !isNullOrUndefined(annotation.Bounds.X) ?
annotation.Bounds.X - paddingValue : annotation.Bounds.x;
var annotationBoundsY = !isNullOrUndefined(annotation.Bounds.Y) ?
annotation.Bounds.Y - paddingValue : annotation.Bounds.y;
var width = annotation.Bounds.Width ?
annotation.Bounds.Width : annotation.Bounds.width;
var height = annotation.Bounds.Height ?
annotation.Bounds.Height : annotation.Bounds.height;
var isAddedProgramatically = annotation.isAddAnnotationProgramatically ?
annotation.isAddAnnotationProgramatically : false;
var rotateValue = this.getRotationValue(pageNumber, isAddedProgramatically);
var pageRotate = annotation.PageRotation;
if (Math.sign(annotation.Rotate) === 1) {
annotation.Rotate = -annotation.Rotate + rotateValue;
}
else {
annotation.Rotate = annotation.Rotate + rotateValue;
}
var rotateAngle = Math.abs(annotation.Rotate);
if (isImportAction && rotateValue !== pageRotate) {
if (this.pdfViewerBase.isJsonImported) {
var pageDetails = this.pdfViewerBase.pageSize[parseInt(pageNumber.toString(), 10)];
var boundsX = annotation.Bounds.X;
var boundsY = annotation.Bounds.Y;
var annotationWidth = width;
var annotationHeight = height;
if (pageRotate > 0) {
var rotatation = pageRotate / 90;
if (rotatation === 1) {
height = width;
width = annotation.Bounds.Height;
annotationBoundsX = annotation.Bounds.Y;
if (rotateValue !== 270) {
annotationBoundsY = pageDetails.height - annotation.Bounds.X -
annotation.Bounds.Width;
}
else {
annotationBoundsY = pageDetails.width - annotation.Bounds.X -
annotation.Bounds.Width;
}
}
else if (rotatation === 2) {
if (rotateValue !== 270 && rotateValue !== 90) {
annotationBoundsX = pageDetails.width - annotation.Bounds.X -
annotation.Bounds.Width;
annotationBoundsY = pageDetails.height - annotation.Bounds.Y -
annotation.Bounds.Height;
}
else {
annotationBoundsX = pageDetails.height - annotation.Bounds.X -
annotation.Bounds.Width;
annotationBoundsY = pageDetails.width - annotation.Bounds.Y -
annotation.Bounds.Height;
}
}
else if (rotatation === 3) {
height = width;
width = annotation.Bounds.Height;
if (rotateValue !== 90) {
annotationBoundsX = pageDetails.width - annotation.Bounds.Y - width;
}
else {
annotationBoundsX = pageDetails.height - annotation.Bounds.Y - width;
}
annotationBoundsY = annotation.Bounds.X;
}
boundsX = annotationBoundsX;
boundsY = annotationBoundsY;
annotationWidth = width;
annotationHeight = height;
}
rotateAngle = (rotateValue / 90) % 4;
if (rotateAngle === 1) {
height = width;
width = annotationHeight;
annotationBoundsX = pageDetails.width - boundsY - annotationHeight - paddingValue;
annotationBoundsY = boundsX - paddingValue;
rotateAngle = 90;
}
else if (rotateAngle === 2) {
annotationBoundsX = pageDetails.width - boundsX - annotationWidth - paddingValue;
annotationBoundsY = pageDetails.height - boundsY - annotationHeight - paddingValue;
rotateAngle = 180;
}
else if (rotateAngle === 3) {
height = width;
width = annotationHeight;
annotationBoundsX = boundsY - paddingValue;
annotationBoundsY = pageDetails.height - boundsX - height - paddingValue;
rotateAngle = 270;
}
else if (rotateAngle === 0) {
annotationBoundsX = boundsX - paddingValue;
annotationBoundsY = boundsY - paddingValue;
}
}
}
if (rotateAngle === 90 || rotateAngle === 270) {
var rotationHeight = height;
var rotationWidth = width;
height = rotationWidth;
width = rotationHeight;
annotationBoundsX = (annotationBoundsX - (width / 2)) + (height / 2);
annotationBoundsY = (annotationBoundsY) + (width / 2 - height / 2);
}
annotation.allowedInteractions = annotation.AllowedInteractions ? annotation.AllowedInteractions :
this.pdfViewer.annotationModule.updateAnnotationAllowedInteractions(annotation);
if (!isNullOrUndefined(annotation) && annotation.MarkupText && annotation.MarkupText.includes('\n')) {
var noOfLines = annotation.MarkupText.split('\n').length;
var newHeight = noOfLines * annotation.FontSize * this.lineGap;
var pageHeight = this.pdfViewerBase.pageSize[parseInt(pageNumber.toString(), 10)].height
- annotation.Bounds.Y;
if (height < newHeight) {
height = newHeight;
}
if (height > pageHeight) {
height = pageHeight;
}
}
var annot = {
author: annotation.Author, modifiedDate: annotation.ModifiedDate, subject: annotation.Subject, id: 'freetext' + this.inputBoxCount,
rotateAngle: annotation.Rotate, dynamicText: annotation.MarkupText, strokeColor: annotation.StrokeColor,
thickness: annotation.Thickness, fillColor: annotation.FillColor,
bounds: {
x: annotationBoundsX, y: annotationBoundsY, left: annotationBoundsX, top: annotationBoundsY,
width: width, height: height, right: annotation.Bounds.Right,
bottom: annotation.Bounds.Bottom
}, annotName: annotation.AnnotName, shapeAnnotationType: 'FreeText',
pageIndex: pageNumber, opacity: annotation.Opacity, fontColor: annotation.FontColor,
fontSize: annotation.FontSize, pageRotation: rotateValue,
fontFamily: annotation.FontFamily, notes: annotation.MarkupText, textAlign: annotation.TextAlign,
comments: this.pdfViewer.annotationModule.getAnnotationComments(annotation.Comments, annotation, annotation.Author),
review: { state: annotation.State, stateModel: annotation.StateModel,
modifiedDate: annotation.ModifiedDate, author: annotation.Author },
font: { isBold: annotation.Font.Bold, isItalic: annotation.Font.Italic,
isStrikeout: annotation.Font.Strikeout, isUnderline: annotation.Font.Underline },
annotationSelectorSettings: this.getSettings(annotation), annotationSettings: annotation.AnnotationSettings,
customData: this.pdfViewer.annotation.getCustomData(annotation),
annotationAddMode: annotation.annotationAddMode, allowedInteractions: annotation.allowedInteractions,
isPrint: annotation.IsPrint, isCommentLock: annotation.IsCommentLock, isReadonly: annotation.IsReadonly,
isAddAnnotationProgrammatically: isAddedProgramatically, isTransparentSet: annotation.IsTransparentSet
};
if (isImportAction) {
annot.id = annotation.AnnotName;
annot.previousFontSize = annotation.FontSize ? annotation.FontSize : this.fontSize;
}
var addedAnnot = this.pdfViewer.add(annot);
this.pdfViewer.annotationModule.storeAnnotations(pageNumber, annot, '_annotations_freetext');
if (this.isAddAnnotationProgramatically) {
var settings = {
opacity: annot.opacity, borderColor: annot.strokeColor, borderWidth: annot.thickness,
author: annotation.author, subject: annotation.subject, modifiedDate: annotation.modifiedDate,
fillColor: annot.fillColor, fontSize: annot.fontSize, width: annot.bounds.width,
height: annot.bounds.height, fontColor: annot.fontColor, fontFamily: annot.fontFamily,
defaultText: annot.dynamicText, fontStyle: annot.font, textAlignment: annot.textAlign
};
this.pdfViewer.fireAnnotationAdd(annot.pageIndex, annot.annotName, 'FreeText', annot.bounds, settings);
}
this.inputBoxCount += 1;
this.pdfViewer.annotation.freeTextAnnotationModule.isFreeTextValueChange = true;
this.pdfViewer.nodePropertyChange(addedAnnot, {});
this.pdfViewer.annotation.freeTextAnnotationModule.isFreeTextValueChange = false;
}
}
}
}
};
/**
* @param {any} annotation - It describes about the annotation
* @private
* @returns {AnnotationSelectorSettingsModel} - AnnotationSelectorSettingsModel
*/
FreeTextAnnotation.prototype.getSettings = function (annotation) {
var selector = this.pdfViewer.annotationSelectorSettings;
if (annotation.AnnotationSelectorSettings) {
selector = typeof (annotation.AnnotationSelectorSettings) === 'string' ? JSON.parse(annotation.AnnotationSelectorSettings) : annotation.AnnotationSelectorSettings;
}
else if (this.pdfViewer.freeTextSettings.annotationSelectorSettings) {
selector = this.pdfViewer.freeTextSettings.annotationSelectorSettings;
this.pdfViewerBase.updateSelectorSettings(selector);
}
return selector;
};
/**
* @param {AnnotType} type - Annotation type
* @private
* @returns {void}
*/
FreeTextAnnotation.prototype.setAnnotationType = function (type) {
this.pdfViewerBase.disableTextSelectionMode();
this.pdfViewer.annotationModule.isFormFieldShape = false;
switch (type) {
case 'FreeText': {
this.currentAnnotationMode = 'FreeText';
this.updateTextProperties();
var modifiedDateRect = this.pdfViewer.annotation.stickyNotesAnnotationModule.getDateAndTime();
this.pdfViewer.drawingObject = {
shapeAnnotationType: 'FreeText', strokeColor: this.borderColor,
fillColor: this.fillColor, opacity: this.opacity, notes: '', isCommentLock: false,
thickness: this.borderWidth, borderDashArray: '0', modifiedDate: modifiedDateRect,
author: this.author, subject: this.pdfViewer.freeTextSettings.subject,
font: {
isBold: this.isBold, isItalic: this.isItalic, isStrikeout: this.isStrikethrough,
isUnderline: this.isUnderline
}, textAlign: this.textAlign
};
this.pdfViewer.tool = 'Select';
break;
}
}
};
/**
* @param {string} property - It describes about the property name
* @param {number} pageNumber - It describes about the page number value
* @param {any} annotationBase - It describes about the annotation base
* @param {boolean} isNewAdded - It describes about whether the isNewAdded is true or not
* @private
* @returns {IFreeTextAnnotation} - Ifreetextannotation
*/
FreeTextAnnotation.prototype.modifyInCollection = function (property, pageNumber, annotationBase, isNewAdded) {
if (!isNullOrUndefined(annotationBase.formFieldAnnotationType) && annotationBase.formFieldAnnotationType !== '') {
this.pdfViewer.annotationModule.isFormFieldShape = true;
}
else {
this.pdfViewer.annotationModule.isFormFieldShape = false;
}
var currentAnnotObject = null;
var isEdited = false;
var pageAnnotations = this.getAnnotations(pageNumber, null);
if (pageAnnotations !== null && annotationBase) {
for (var i = 0; i < pageAnnotations.length; i++) {
if (annotationBase.id === pageAnnotations[parseInt(i.toString(), 10)].id) {
if (property === 'bounds') {
this.pdfViewerBase.isBounds =
this.pdfViewerBase.boundsCalculation(pageAnnotations[parseInt(i.toString(), 10)].bounds, annotationBase.wrapper.bounds);
if (this.pdfViewerBase.isBounds) {
pageAnnotations[parseInt(i.toString(), 10)].bounds = {
left: annotationBase.bounds.x, top: annotationBase.bounds.y,
width: annotationBase.bounds.width, height: annotationBase.bounds.height,
right: annotationBase.bounds.right,
bottom: annotationBase.bounds.bottom
};
}
}
else if (property === 'fill') {
pageAnnotations[parseInt(i.toString(), 10)].fillColor = annotationBase.wrapper.children[0].style.fill;
}
else if (property === 'stroke') {
pageAnnotations[parseInt(i.toString(), 10)].strokeColor = annotationBase.wrapper.children[0].style.strokeColor;
}
else if (property === 'opacity') {
pageAnnotations[parseInt(i.toString(), 10)].opacity = annotationBase.wrapper.children[0].style.opacity;
}
else if (property === 'thickness') {
pageAnnotations[parseInt(i.toString(), 10)].thickness = annotationBase.wrapper.children[0].style.strokeWidth;
}
else if (property === 'notes') {
pageAnnotations[parseInt(i.toString(), 10)].note = annotationBase.notes;
}
else if (property === 'delete') {
currentAnnotObject = pageAnnotations.splice(i, 1)[0];
break;
}
else if (property === 'dynamicText') {
if (pageAnnotations[parseInt(i.toString(), 10)].dynamicText !== annotationBase.dynamicText) {
isEdited = true;
this.pdfViewer.fireCommentEdit(pageAnnotations[parseInt(i.toString(), 10)].annotName, annotationBase.dynamicText, pageAnnotations[parseInt(i.toString(), 10)]);
}
pageAnnotations[parseInt(i.toString(), 10)].dynamicText = annotationBase.dynamicText;
}
else if (property === 'fontColor') {
pageAnnotations[parseInt(i.toString(), 10)].fontColor = annotationBase.fontColor;
}
else if (property === 'fontSize') {
pageAnnotations[parseInt(i.toString(), 10)].fontSize = annotationBase.fontSize;
}
else if (property === 'fontFamily') {
pageAnnotations[parseInt(i.toString(), 10)].fontFamily = annotationBase.fontFamily;
}
else if (property === 'textPropertiesChange') {
pageAnnotations[parseInt(i.toString(), 10)].font = { isBold: annotationBase.font.isBold,
isItalic: annotationBase.font.isItalic, isStrikeout: annotationBase.font.isStrikeout,
isUnderline: annotationBase.font.isUnderline };
}
else if (property === 'textAlign') {
pageAnnotations[parseInt(i.toString(), 10)].textAlign = annotationBase.textAlign;
}
if (this.pdfViewerBase.isBounds) {
pageAnnotations[parseInt(i.toString(), 10)].modifiedDate =
this.pdfViewer.annotation.stickyNotesAnnotationModule.getDateAndTime();
}
this.pdfViewer.annotationModule.storeAnnotationCollections(pageAnnotations[parseInt(i.toString(), 10)], pageNumber);
}
}
this.manageAnnotations(pageAnnotations, pageNumber);
}
if (!isNewAdded && isEdited) {
this.pdfViewerBase.updateDocumentEditedProperty(true);
}
return currentAnnotObject;
};
/**
* @param {number} pageNumber - This is pageNumber
* @param {IFreeTextAnnotation} annotationBase - This is annotationBase
* @private
* @returns {void}
*/
FreeTextAnnotation.prototype.addInCollection = function (pageNumber, annotationBase) {
if (annotationBase) {
var pageAnnotations = this.getAnnotations(pageNumber, null);
if (pageAnnotations) {
pageAnnotations.push(annotationBase);
}
this.manageAnnotations(pageAnnotations, pageNumber);
}
};
/**
* @private
* @returns {string} - string
*/
FreeTextAnnotation.prototype.saveFreeTextAnnotations = function () {
var storeObject = PdfViewerBase.sessionStorageManager.getItem(this.pdfViewerBase.documentId + '_annotations_freetext');
if (this.pdfViewerBase.isStorageExceed) {
storeObject = this.pdfViewerBase.annotationStorage[this.pdfViewerBase.documentId + '_annotations_freetext'];
}
var annotations = [];
for (var j = 0; j < this.pdfViewerBase.pageCount; j++) {
annotations[parseInt(j.toString(), 10)] = [];
}
if (storeObject && !this.pdfViewer.annotationSettings.skipDownload) {
var annotationCollection = JSON.parse(storeObject);
for (var i = 0; i < annotationCollection.length; i++) {
var newArray = [];
var pageAnnotationObject = annotationCollection[parseInt(i.toString(), 10)];
if (pageAnnotationObject) {
for (var z = 0; pageAnnotationObject.annotations.length > z; z++) {
this.pdfViewer.annotationModule.updateModifiedDate(pageAnnotationObject.annotations[parseInt(z.toString(), 10)]);
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].bounds =
this.getBoundsBasedOnRotation(pageAnnotationObject.annotations[parseInt(z.toString(), 10)].bounds, pageAnnotationObject.annotations[parseInt(z.toString(), 10)].rotateAngle, pageAnnotationObject.pageIndex, pageAnnotationObject.annotations[parseInt(z.toString(), 10)]);
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].bounds =
JSON.stringify(this.pdfViewer.annotation.getBounds(pageAnnotationObject.
annotations[parseInt(z.toString(), 10)].bounds, pageAnnotationObject.pageIndex));
var strokeColorString = pageAnnotationObject.annotations[parseInt(z.toString(), 10)].strokeColor;
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].strokeColor =
JSON.stringify(this.getRgbCode(strokeColorString));
var fillColorString = pageAnnotationObject.annotations[parseInt(z.toString(), 10)].fillColor;
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].fillColor =
JSON.stringify(this.getRgbCode(fillColorString));
var fontColorString = pageAnnotationObject.annotations[parseInt(z.toString(), 10)].fontColor;
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].fontColor =
JSON.stringify(this.getRgbCode(fontColorString));
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].vertexPoints =
JSON.stringify(pageAnnotationObject.annotations[parseInt(z.toString(), 10)].vertexPoints);
if (pageAnnotationObject.annotations[parseInt(z.toString(), 10)].rectangleDifference !== null) {
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].rectangleDifference =
JSON.stringify(pageAnnotationObject.annotations[parseInt(z.toString(), 10)].rectangleDifference);
}
pageAnnotationObject.annotations[parseInt(z.toString(), 10)].padding = this.getPaddingValues(this.fontSize);
}
newArray = pageAnnotationObject.annotations;
}
annotations[pageAnnotationObject.pageIndex] = newArray;
}
}
return JSON.stringify(annotations);
};
FreeTextAnnotation.prototype.getRotationValue = function (pageIndex, isAddedProgrammatically) {
var pageDetails = this.pdfViewerBase.pageSize[parseInt(pageIndex.toString(), 10)];
if (!isNullOrUndefined(isAddedProgrammatically) && isAddedProgrammatically) {
return 0;
}
else {
if (pageDetails.rotation === 0) {
return 0;
}
else if (pageDetails.rotation === 1) {
return 90;
}
else if (pageDetails.rotation === 2) {
return 180;
}
else if (pageDetails.rotation === 3) {
return 270;
}
return 0;
}
};
FreeTextAnnotation.prototype.getBoundsBasedOnRotation = function (bounds, rotateAngle, pageIndex, annotation, isAddedProgrammatically) {
var rotateValue = this.getRotationValue(pageIndex, isAddedProgrammatically);
var paddingValue = 0.5;
annotation.rotateAngle = rotateAngle - rotateValue;
annotation.pageRotation = rotateValue;
if (rotateAngle === 90 || rotateAngle === -90 || rotateAngle === 270 || rotateAngle === -270) {
var x = bounds.left + (bounds.width / 2) - (bounds.height / 2);
var y = bounds.top - (bounds.width / 2 - bounds.height / 2);
return { x: x + paddingValue, y: y + paddingValue, left: x + paddingValue,
top: y + paddingValue, width: bounds.height, height: bounds.width };
}
else {
return { x: bounds.left + paddingValue, y: bounds.top + paddingValue,
left: bounds.left + paddingValue, top: bounds.top + paddingValue, width: bounds.width, height: bounds.height };
}
};
FreeTextAnnotation.prototype.manageAnnotations = function (pageAnnotations, pageNumber) {
var storeObject = PdfViewerBase.sessionStorageManager.getItem(this.pdfViewerBase.documentId + '_annotations_freetext');
if (this.pdfViewerBase.isStorageExceed) {
storeObject = this.pdfViewerBase.annotationStorage[this.pdfViewerBase.documentId + '_annotations_freetext'];
}
if (storeObject) {
var annotObject = JSON.parse(storeObject);
if (!this.pdfViewerBase.isStorageExceed) {
PdfViewerBase.sessionStorageManager.removeItem(this.pdfViewerBase.documentId + '_annotations_freetext');
}
var index = this.pdfViewer.annotationModule.getPageCollection(annotObject, pageNumber);
if (index != null && annotObject[parseInt(index.toString(), 10)]) {
annotObject[parseInt(index.toString(), 10)].annotations = pageAnnotations;
}
var annotationStringified = JSON.stringify(annotObject);
if (this.pdfViewerBase.isStorageExceed) {
this.pdfViewerBase.annotationStorage[this.pdfViewerBase.documentId + '_annotations_freetext'] = annotationStringified;
}
else {
PdfViewerBase.sessionStorageManager.setItem(this.pdfViewerBase.documentId + '_annotations_freetext', annotationStringified);
}
}
};
FreeTextAnnotation.prototype.getAnnotations = function (pageIndex, shapeAnnotations) {
var annotationCollection;
var storeObject = PdfViewerBase.sessionStorageManager.getItem(this.pdfViewerBase.documentId + '_annotations_freetext');
if (this.pdfViewerBase.isStorageExceed) {
storeObject = this.pdfViewerBase.annotationStorage[this.pdfViewerBase.documentId + '_annotations_freetext'];
}
if (storeObject) {
var annotObject = JSON.parse(storeObject);
var index = this.pdfViewer.annotationModule.getPageCollection(annotObject, pageIndex);
if (index != null && annotObject[parseInt(index.toString(), 10)]) {
annotationCollection = annotObject[parseInt(index.toString(), 10)].annotations;
}
else {
annotationCollection = shapeAnnotations;
}
}
else {
annotationCollection = shapeAnnotations;
}
return annotationCollection;
};
FreeTextAnnotation.prototype.getRgbCode = function (colorString) {
// eslint-disable-next-line
if (!colorString.match(/#([a-z0-9]+)/gi) && !colorString.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/)) {
colorString = this.pdfViewer.annotationModule.nameToHash(colorString);
}
var stringArray = colorString.split(',');
if (isNullOrUndefined(stringArray[1])) {
colorString = this.pdfViewer.annotationModule.getValue(colorString, 'rgba');
stringArray = colorString.split(',');
}
var r = parseFloat(stringArray[0].split('(')[1]);
var g = parseFloat(stringArray[1]);
var b = parseFloat(stringArray[2]);
var a = parseFloat(stringArray[3]);
return { r: r, g: g, b: b, a: a };
};
/**
* @private
* @returns {void}
*/
FreeTextAnnotation.prototype.onFocusOutInputBox = function () {
var allowServerDataBind = this.pdfViewer.allowServerDataBinding;
this.pdfViewer.enableServerDataBinding(false);
if (!this.pdfViewerBase.isFreeTextContextMenu) {
this.pdfViewer.fireBeforeAddFreeTextAnnotation(this.inputBoxElement.value);
if (this.pdfViewer.enableHtmlSanitizer && this.inputBoxElement) {
this.inputBoxElement.value = SanitizeHtmlHelper.sanitize(this.inputBoxElement.value);
}
var pageIndex = this.inputBoxElement.id && this.inputBoxElement.id.split('_freeText_')[1] && this.inputBoxElement.id.split('_freeText_')[1].split('_')[0] ? parseFloat(this.inputBoxElement.id.split('_freeText_')[1].split('_')[0]) : this.pdfViewerBase.currentPageNumber - 1;
var pageDiv = this.pdfViewerBase.getElement('_pageDiv_' + (pageIndex));
var width = parseFloat(this.inputBoxElement.style.width);
// Removed the line since when we click on the freetext the size gets changed. Task Id: 847135
if (this.pdfViewer.freeTextSettings.enableAutoFit && !this.isMaximumWidthReached && this.isNewFreeTextAnnot) {
width = parseFloat(this.inputBoxElement.style.width);
var characterLength = 8;
this.inputBoxElement.style.width = (width - characterLength) + 'px';
}
var inputEleHeight = parseFloat(this.inputBoxElement.style.height);
var inputEleWidth = parseFloat(this.inputBoxElement.style.width);
var inputEleLeft = parseFloat(this.inputBoxElement.style.left);
if (this.pdfViewerBase.isMixedSizeDocument) {
var canvas = this.pdfViewerBase.getAnnotationCanvas('_annotationCanvas_', pageIndex);
inputEleLeft = inputEleLeft - canvas.offsetLeft;
}
var inputEleTop = parseFloat(this.inputBoxElement.style.top);
var zoomFactor = this.pdfViewerBase.getZoomFactor();
if (this.pdfViewer.isValidFreeText) {
this.inputBoxElement.value = 'Type Here';
this.pdfViewer.isValidFreeText = false;
}
var inputValue = this.inputBoxElement.value;
var isNewlyAdded = false;
if (this.isNewFreeTextAnnot === true) {
var currentDateString = this.pdfViewer.annotation.stickyNotesAnnotationModule.getDateAndTime();
var annotationName = this.pdfViewer.annotation.createGUID();
this.isNewFreeTextAnnot = false;
isNewlyAdded = true;
var commentsDivid = this.pdfViewer.annotation.stickyNotesAnnotationModule.addComments('freeText', pageIndex + 1);
if (commentsDivid) {
document.getElementById(commentsDivid).id = annotationName;
}
var annotationSelectorSettings = this.pdfViewer.freeTextSettings.annotationSelectorSettings;
this.pdfViewerBase.updateSelectorSettings(annotationSelectorSettings);
var annotationSettings = this.pdfViewer.annotationModule.updateSettings(this.pdfViewer.freeTextSettings);
this.author = this.author ? this.author : this.pdfViewer.freeTextSettings.author ? this.pdfViewer.freeTextSettings.author : 'Guest';
this.subject = this.subject ? this.subject : this.pdfViewer.freeTextSettings.subject ? this.pdfViewer.freeTextSettings.subject : 'Text Box';
var allowedInteractions = this.pdfViewer.freeTextSettings.allowedInteractions ?
this.pdfViewer.freeTextSettings.allowedInteractions : this.pdfViewer.annotationSettings.allowedInteractions;
var annot = {
author: this.author, modifiedDate: currentDateString, subject: this.subject, id: 'free_text' + this.inputBoxCount,
rotateAngle: 0, dynamicText: inputValue, strokeColor: this.borderColor,
thickness: this.borderWidth, fillColor: this.fillColor,
bounds: {
left: inputEleLeft / zoomFactor, top: inputEleTop / zoomFactor, x: inputEleLeft / zoomFactor,
y: inputEleTop / zoomFactor, width: inputEleWidth / zoomFactor, height: inputEleHeight / zoomFactor
}, annotName: annotationName,
shapeAnnotationType: 'FreeText', pageIndex: pageIndex, fontColor: this.fontColor, fontSize: this.fontSize,
fontFamily: this.fontFamily, opacity: this.opacity, comments: [], textAlign: this.textAlign,
font: { isBold: this.isBold, isItalic: this.isItalic, isStrikeout: this.isStrikethrough,
isUnderline: this.isUnderline },
review: { state: 'Unmarked', stateModel: 'None', modifiedDate: currentDateString, author: this.author },
annotationSelectorSettings: annotationSelectorSettings, annotationSettings: annotationSettings,
customData: this.pdfViewer.annotationModule.getData('FreeText'), isPrint: (this.pdfViewer.freeTextSettings && !isNullOrUndefined(this.pdfViewer.freeTextSettings.isPrint)) ? this.pdfViewer.freeTextSettings.isPrint : true,
allowedInteractions: allowedInteractions, isReadonly: this.isReadonly
};
if (this.pdfViewer.enableRtl) {
annot.textAlign = 'Right';
}
var annotation = this.pdfViewer.add(annot);
var bounds = { left: annot.bounds.x, top: annot.bounds.y, width: annot.bounds.width,
height: annot.bounds.height };
var settings = {
opacity: annot.opacity, borderColor: annot.strokeColor, borderWidth: annot.thickness,
author: annotation.author, subject: annotation.subject, modifiedDate: annotation.modifiedDate,
fillColor: annot.fillColor, fontSize: annot.fontSize, width: annot.bounds.width,
height: annot.bounds.height, fontColor: annot.fontColor, fontFamily: annot.fontFamily,
defaultText: annot.dynamicText, fontStyle: annot.font, textAlignment: annot.textAlign
};
this.pdfViewer.annotation.storeAnnotations(pageIndex, annot, '_annotations_freetext');
this.pdfViewer.fireAnnotationAdd(annot.pageIndex, annot.annotName, 'FreeText', bounds, settings);
this.pdfViewer.fireCommentAdd(annot.annotName, annot.dynamicText, annot);
this.pdfViewer.annotation.addAction(pageIndex, null, annotation, 'Addition', '', annotation, annotation);
this.pdfViewer.renderSelector(annot.pageIndex);
this.pdfViewer.clearSelection(annot.pageIndex);
this.pdfViewerBase.updateDocumentEditedProperty(true);
this.selectedAnnotation = annotation;
}
this.isInuptBoxInFocus = false;
if (this.selectedAnnotation && this.pdfViewer.selectedItems.annotations) {
var isRotated = this.selectedAnnotation.pageRotation === 90 || this.selectedAnnotation.pageRotation === 270;
inputEleHeight = parseFloat(isRotated ? this.inputBoxElement.style.width : this.inputBoxElement.style.height) / zoomFactor;
inputEleWidth = parseFloat(isRotated ? this.inputBoxElement.style.height : this.inputBoxElement.style.width) / zoomFactor;
var heightDiff = (inputEleHeight - this.selectedAnnotation.bounds.height);
var y = undefined;
if (heightDiff > 0) {
y = this.selectedAnnotation.wrapper.offsetY + (heightDiff / 2);
y = y > 0 ? y : undefined;
}
var widthDiff = (inputEleWidth - this.selectedAnnotation.bounds.width);
var x = undefined;
if (widthDiff > 0) {
x = this.selectedAnnotation.wrapper.offsetX + (widthDiff / 2);
x = x > 0 ? x : undefined;
}
else {
widthDiff = Math.abs(widthDiff);
x = this.selectedAnnotation.wrapper.offsetX - (widthDiff / 2);
}
this.selectedAnnotation.bounds.width = inputEleWidth;
this.selectedAnnotation.bounds.height = inputEleHeight;
var lineSpace = 0;
lineSpace = ((parseFloat(this.inputBoxElement.style.fontSize) / zoomFactor) / (this.defaultFontSize / 2));
this.selectedAnnotation.wrapper.children[1].margin.left = this.freeTextPaddingLeft;
this.selectedAnnotation.wrapper.children[1].margin.top =
((parseFloat(this.inputBoxElement.style.paddingTop) / zoomFactor)) + lineSpace;
this.pdfViewer.annotation.modifyDynamicTextValue(inputValue, this.selectedAnnotation.annotName);
this.selectedAnnotation.dynamicText = inputValue;
this.modifyInCollection('dynamicText', pageIndex, this.selectedAnnotation, isNewlyAdded);
this.modifyInCollection('bounds', pageIndex, this.selectedAnnotation, isNewlyAdded);
this.pdfViewer.nodePropertyChange(this.selectedAnnotation, { bounds: { width: this.selectedAnnotation.bounds.width,
height: this.selectedAnnotation.bounds.height, y: y, x: x } });
var commentsDiv = document.getElementById(this.selectedAnnotation.annotName);
if (commentsDiv && commentsDiv.childNodes) {
if (commentsDiv.childNodes[0].ej2_instances) {
commentsDiv.childNodes[0].ej2_instances[0].value = inputValue;
}
else if (commentsDiv.childNodes[0].childNodes && commentsDiv.childNodes[0].childNodes[1].ej2_instances) {
commentsDiv.childNodes[0].childNodes[1].ej2_instances[0].value = inputValue;
}
}
this.pdfViewer.renderSelector(this.selectedAnnotation.pageIndex, this.selectedAnnotation.annotationSelectorSettings);
}
if (this.inputBoxElement.parentElement) {
if (pageDiv && (pageDiv.id === this.inputBoxElement.parentElement.id)) {
pageDiv.removeChild(this.inputBoxElement);
}
else {
this.inputBoxElement.parentElement.removeChild(this.inputBoxElement);
}
}
var canvass = this.pdfViewerBase.getAnnotationCanvas('_annotationCanvas_', pageIndex);
this.pdfViewer.renderDrawing(canvass, pageIndex);
this.inputBoxCount += 1;
}
else {
this.inputBoxElement.focus();
if (!this.isTextSelected) {
window.getSelection().removeAllRanges();
}
}
this.pdfViewer.enableServerDataBinding(allowServerDataBind, true);