@syncfusion/ej2-pdfviewer
Version:
Essential JS 2 PDF viewer Component
958 lines • 89 kB
JavaScript
import { PageRenderer, FormFieldsBase, AnnotationRenderer, SignatureBase, BookmarkStyles, BookmarkDestination, BookmarkBase, AnnotBounds } from './index';
import { isNullOrUndefined } from '@syncfusion/ej2-base';
import { DataFormat, PdfAnnotationExportSettings, PdfDocument, PdfRotationAngle, PdfTextStyle, PdfDocumentLinkAnnotation, PdfTextWebLinkAnnotation, PdfUriAnnotation, PdfPermissionFlag, PdfFormFieldExportSettings, PdfPageSettings, PdfSignatureField, PdfPageImportOptions } from '@syncfusion/ej2-pdf';
import { ExtractTextOption } from '../index';
import { Size } from '@syncfusion/ej2-drawings';
import { PdfViewerUtils, TaskPriorityLevel } from '../base/pdfviewer-utlis';
/**
* PdfRenderer
*
* @hidden
*/
var PdfRenderer = /** @class */ (function () {
/**
* @param {PdfViewer} pdfViewer - The PdfViewer.
* @param {PdfViewerBase} pdfViewerBase - The PdfViewerBase.
* @private
*/
function PdfRenderer(pdfViewer, pdfViewerBase) {
/**
* @private
*/
this.bookmarkStyles = [];
/**
* @private
*/
this.bookmarkCollection = [];
/**
* @private
*/
this.pageRotationCollection = [];
/**
* @private
*/
this.bookmarkDictionary = {};
this.annotationDetailCollection = {};
/**
* @private
*/
this.documentTextCollection = [];
this.pageSizes = {};
this.isCompletePageSizeNotReceieved = true;
this.x = 0;
this.y = 0;
this.zoom = 1;
this.id = 0;
this.pageIndex = 0;
this.textCollections = [];
this.scaleFactor = 1.5;
this.restrictionList = [];
this.securityList = ['Print', 'EditContent', 'CopyContent', 'EditAnnotations', 'FillFields', 'AccessibilityCopyContent', 'AssembleDocument', 'FullQualityPrint'];
this._fallbackFontCollection = {};
this.document = null;
/**
* @private
*/
this.searchResults = {};
this.isDummyInserted = false;
this.pdfViewer = pdfViewer;
this.pdfViewerBase = pdfViewerBase;
}
Object.defineProperty(PdfRenderer.prototype, "PageCount", {
/**
* @private
* @returns {void}
*/
get: function () {
return this.pageCount;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfRenderer.prototype, "ReferencePath", {
/**
* @private
* @returns {void}
*/
get: function () {
return this.mReferencePath;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfRenderer.prototype, "referencePath", {
/**
* @private
* @param {string} v - v
* @returns {void}
*/
set: function (v) {
this.mReferencePath = v;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfRenderer.prototype, "ScaleFactor", {
/**
* @private
* @returns {void}
*/
get: function () {
return this.scaleFactor;
},
/**
* @private
* @param {string} v - v
*/
set: function (v) {
this.scaleFactor = v;
if (this.scaleFactor <= 0) {
this.scaleFactor = 1;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfRenderer.prototype, "FallbackFontCollection", {
/**
* @private
* @returns {void}
*/
get: function () {
return this._fallbackFontCollection;
},
/**
* @private
* @param {string} v - v
*/
set: function (v) {
this._fallbackFontCollection = v;
},
enumerable: true,
configurable: true
});
/**
* @param {string} documentData - documentData
* @param {string} documentId - documentId
* @param {string} password - password
* @param {any} jsonObject - jsonObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.load = function (documentData, documentId, password, jsonObject) {
try {
if (jsonObject.action !== 'VirtualLoad') {
this.loadedDocument = new PdfDocument(documentData, password ? password : '');
this.loadedByteArray = documentData;
this.password = password;
this.isCompletePageSizeNotReceieved = true;
}
}
catch (error) {
if (error.message === 'Invalid PDF structure.') {
return '3';
}
else if (error.message === 'Cannot open an encrypted document. The password is invalid.') {
return '4';
}
else if (error.message === 'Invalid cross reference') {
return '4';
}
else {
return error.message;
}
}
var jsonResult = this.loadDocument(documentData, documentId, password, jsonObject);
this.bookmarkStyles = [];
this.bookmarkCollection = [];
this.bookmarkDictionary = {};
return JSON.stringify(jsonResult);
};
/**
* @param {string} documentData - documentData
* @param {string} documentId - documentId
* @param {string} password - password
* @param {any} jsonObject - jsonObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.loadImportDocument = function (documentData, documentId, password, jsonObject) {
try {
if (jsonObject.action !== 'VirtualLoad') {
this.loadImportedDocument = new PdfDocument(documentData, password ? password : '');
this.loadImportedBase64String = documentData;
this.importedDocpassword = password;
}
}
catch (error) {
if (error.message === 'Invalid PDF structure.') {
return '3';
}
else if (error.message === 'Cannot open an encrypted document. The password is invalid.') {
return '4';
}
else if (error.message === 'Invalid cross reference') {
return '4';
}
else {
return error.message;
}
}
var jsonResult = { uniqueId: documentId, password: this.importedDocpassword };
this.loadImportedDocument.destroy();
this.loadImportedDocument = null;
return JSON.stringify(jsonResult);
};
/**
* @param {string} documentData - documentData
* @param {string} documentId - documentId
* @param {string} password - password
* @param {any} jsonObject - jsonObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.loadDocument = function (documentData, documentId, password, jsonObject) {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'isCompletePageSizeNotReceived')) {
this.isCompletePageSizeNotReceieved = !jsonObject.isCompletePageSizeNotReceived;
}
this.pageCount = this.loadedDocument.pageCount;
this.pageSizes = this.getPageSizes(this.pageCount);
var pdfRenderedFormFields = [];
var isDigitalSignaturePresent = false;
var isTaggedPdf = false; //Need to check and set if loaddocument is tagged pdf
this.formFieldsBase = new FormFieldsBase(this.pdfViewer, this.pdfViewerBase, isDigitalSignaturePresent);
this.restrictionList = [];
if (!isNullOrUndefined(this.loadedDocument)) {
this.documentSecurity(password);
}
if (!isNullOrUndefined(this.loadedDocument)) {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'hideEmptyDigitalSignatureFields')) {
this.formFieldsBase.hideEmptyDigitalSignatureFields = jsonObject['hideEmptyDigitalSignatureFields'];
}
if (Object.prototype.hasOwnProperty.call(jsonObject, 'showDigitalSignatureAppearance')) {
this.formFieldsBase.showDigitalSignatureAppearance = jsonObject['showDigitalSignatureAppearance'];
}
}
if (!isNullOrUndefined(this.formFieldsBase) && this.pageSizes && Object.keys(this.pageSizes).length <= 100) {
this.formFieldsBase.GetFormFields();
pdfRenderedFormFields = this.formFieldsBase.PdfRenderedFormFields;
}
if (this.formFieldsBase.mIsDigitalSignaturePresent) {
var digitalSignatureDoc = new PdfDocument(documentData, '');
var loadedForm = digitalSignatureDoc.form;
if (!isNullOrUndefined(loadedForm) && !isNullOrUndefined(loadedForm._fields)) {
for (var i = 0; i < loadedForm.count; i++) {
if (loadedForm.fieldAt(i) instanceof PdfSignatureField) {
var signatureField = loadedForm.fieldAt(i);
if (signatureField.isSigned && this.formFieldsBase.showDigitalSignatureAppearance) {
signatureField.flatten = true;
}
}
}
}
this.digitialByteArray = digitalSignatureDoc.save();
digitalSignatureDoc.destroy();
}
return { pageCount: this.pageCount, pageSizes: this.pageSizes, uniqueId: documentId,
PdfRenderedFormFields: pdfRenderedFormFields, RestrictionSummary: this.restrictionList,
isDigitalSignaturePresent: this.formFieldsBase.mIsDigitalSignaturePresent,
digitialSignatureFile: this.digitialByteArray ? true : false,
isTaggedPdf: isTaggedPdf, pageRotation: this.pageRotationCollection };
};
PdfRenderer.prototype.documentSecurity = function (password) {
var isOwnerPassword = this.loadedDocument.isEncrypted &&
(!this.loadedDocument.isUserPassword || this.loadedDocument._hasUserPasswordOnly);
if (!isNullOrUndefined(password) && password !== '' && isOwnerPassword) {
this.restrictionSummary(password, true);
}
else {
this.restrictionSummary(password, false);
}
};
PdfRenderer.prototype.restrictionSummary = function (password, isOwner) {
var ownerPassword = password ? password : ''; //Need to set owner password from loaded document
var userPassword = password ? password : ''; //Need to set user password from loaded document
var permissionList = this.getPermissionArray(this.loadedDocument.permissions);
var isEncrypted = this.loadedDocument.isEncrypted;
if ((!(permissionList.length === 1 && permissionList[0] === 'Default')) || (isEncrypted && ownerPassword === '' && userPassword === '')) {
for (var i = 0; i < this.securityList.length; i++) {
var isExist = false;
for (var j = 0; j < permissionList.length; j++) {
if (permissionList[parseInt(j.toString(), 10)].trim() === this.securityList[parseInt(i.toString(), 10)].trim()) {
isExist = true;
break;
}
}
if (!isExist && !isOwner) {
this.restrictionList.push(this.securityList[parseInt(i.toString(), 10)].trim());
}
}
}
else if (permissionList.length === 1 && permissionList[0] === 'Default' && isEncrypted && !isOwner) {
for (var i = 0; i < this.securityList.length; i++) {
this.restrictionList.push(this.securityList[parseInt(i.toString(), 10)].trim());
}
}
};
PdfRenderer.prototype.getPermissionArray = function (permission) {
var permissionArray = [];
if (permission === PdfPermissionFlag.default) {
permissionArray.push('Default');
}
else {
if (permission & PdfPermissionFlag.fullQualityPrint) {
permissionArray.push('FullQualityPrint');
}
if (permission & PdfPermissionFlag.assembleDocument) {
permissionArray.push('AssembleDocument');
}
if (permission & PdfPermissionFlag.accessibilityCopyContent) {
permissionArray.push('AccessibilityCopyContent');
}
if (permission & PdfPermissionFlag.fillFields) {
permissionArray.push('FillFields');
}
if (permission & PdfPermissionFlag.editAnnotations) {
permissionArray.push('EditAnnotations');
}
if (permission & PdfPermissionFlag.copyContent) {
permissionArray.push('CopyContent');
}
if (permission & PdfPermissionFlag.editContent) {
permissionArray.push('EditContent');
}
if (permission & PdfPermissionFlag.print) {
permissionArray.push('Print');
}
}
return permissionArray;
};
PdfRenderer.prototype.getPageSizes = function (pageCount) {
var pageSizes = {};
var pageLimit = pageCount;
if (pageCount > 100) {
pageLimit = 100;
}
if (this.isCompletePageSizeNotReceieved) {
for (var i = 0; i < pageLimit; i++) {
pageSizes[i.toString()] = this.getPageSize(i);
var page = this.loadedDocument.getPage(i);
var rotation = page.rotation % 4;
this.pageRotationCollection.push(rotation);
}
}
else {
for (var i = pageLimit; i < pageCount; i++) {
pageSizes[i.toString()] = this.getPageSize(i);
var page = this.loadedDocument.getPage(i);
var rotation = page.rotation % 4;
this.pageRotationCollection.push(rotation);
}
}
return pageSizes;
};
/**
* @param {number} pageNumber - pageNumber
* @private
* @returns {void}
*/
PdfRenderer.prototype.getPageSize = function (pageNumber) {
var page = this.loadedDocument.getPage(pageNumber);
var size = page.size;
var rotation = page.rotation % 4;
if (rotation === PdfRotationAngle.angle0 || rotation === PdfRotationAngle.angle180) {
return new Size(this.convertPointToPixel(size[0]), this.convertPointToPixel(size[1]));
}
else {
return new Size(this.convertPointToPixel(size[1]), this.convertPointToPixel(size[0]));
}
};
PdfRenderer.prototype.convertPointToPixel = function (number) {
return number * 96 / 72;
};
PdfRenderer.prototype.convertPixelToPoint = function (value) {
return (value * (72 / 96));
};
/**
* @param {string} jsonObject - jsonObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.getDocumentAsBase64 = function (jsonObject) {
this.loadedDocument = new PdfDocument(this.loadedByteArray, this.password);
var clonedDocument = null;
if (Object.prototype.hasOwnProperty.call(jsonObject, 'digitalSignatureDocumentEdited') &&
!jsonObject.digitalSignatureDocumentEdited ||
(Object.prototype.hasOwnProperty.call(jsonObject, 'isPdfEdited') && !jsonObject.isPdfEdited)) {
return this.loadedByteArray;
}
else {
var annotationRenderer = new AnnotationRenderer(this.pdfViewer, this.pdfViewerBase);
var formfields = new FormFieldsBase(this.pdfViewer, this.pdfViewerBase);
// create object for form fields signature
annotationRenderer.removeSignatureTypeAnnot(jsonObject, this.loadedDocument);
this.orderAnnotations(jsonObject);
if (Object.prototype.hasOwnProperty.call(jsonObject, 'isFormFieldAnnotationsExist') && jsonObject.isFormFieldAnnotationsExist) {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'formDesigner') && !isNullOrUndefined(jsonObject['formDesigner'])) {
formfields.saveFormFieldsDesignerData(jsonObject);
}
else if (Object.prototype.hasOwnProperty.call(jsonObject, 'fieldsData')) {
formfields.saveFormFieldsData(jsonObject);
}
}
if (Object.prototype.hasOwnProperty.call(jsonObject, 'organizePages')) {
var savedBase64String = this.loadedDocument.save();
clonedDocument = new PdfDocument(savedBase64String, this.password);
var organizePages = JSON.parse(jsonObject.organizePages);
if (organizePages.length > 0) {
var reorderedPages = this.rearrangePages(organizePages);
this.deletePdfPages(organizePages, reorderedPages);
this.insertPdfPages(organizePages);
this.copyPages(organizePages, clonedDocument);
this.rotatePages(organizePages);
this.importPages(organizePages);
if (this.isDummyInserted) {
this.loadedDocument.removePage(this.loadedDocument.pageCount - 1);
this.isDummyInserted = false;
}
}
clonedDocument.destroy();
clonedDocument = null;
}
var documentSaved = this.loadedDocument.save();
if (this.document != null) {
this.document.destroy();
this.document = null;
}
return documentSaved;
}
};
PdfRenderer.prototype.rearrangePages = function (organizePages) {
var _this = this;
var reorderedPageIndices = [];
if (organizePages.length > 0) {
var clonedCollection = JSON.parse(JSON.stringify(organizePages));
var sortedCollection = clonedCollection.sort(function (a, b) { return _this.pdfViewer.pageOrganizer.sorting(a['currentPageIndex'], b['currentPageIndex']); });
for (var i = 0; i < sortedCollection.length; i++) {
var currentPageDetails = sortedCollection[parseInt(i.toString(), 10)];
if (!currentPageDetails.isInserted && !currentPageDetails.isCopied && !currentPageDetails.isImportedDoc && currentPageDetails['currentPageIndex'] !== null && currentPageDetails['pageIndex'] !== null && parseInt(currentPageDetails['pageIndex'].toString(), 10) >= 0) {
reorderedPageIndices.push(parseInt(currentPageDetails['pageIndex'].toString(), 10));
}
}
// eslint-disable-next-line
var deltetedPages = sortedCollection.filter(function (item) { return item.isDeleted && item['currentPageIndex'] === null; });
for (var i = 0; i < deltetedPages.length; i++) {
var deletedPage = deltetedPages[parseInt(i.toString(), 10)];
reorderedPageIndices = reorderedPageIndices.slice(0, deletedPage.pageIndex).concat([deletedPage.pageIndex], reorderedPageIndices.slice(deletedPage.pageIndex));
}
if (reorderedPageIndices.length > 0) {
this.loadedDocument.reorderPages(reorderedPageIndices);
}
}
return reorderedPageIndices;
};
PdfRenderer.prototype.rotatePages = function (organizePages) {
if (organizePages.length > 0) {
var importPageCount = 0;
for (var i = 0; i < organizePages.length; i++) {
var pageNumber = organizePages[parseInt(i.toString(), 10)].pageIndex;
var currentPageNumber = organizePages[parseInt(i.toString(), 10)].currentPageIndex;
var isDeleted = organizePages[parseInt(i.toString(), 10)].isDeleted;
var isInserted = organizePages[parseInt(i.toString(), 10)].isInserted;
var isCopied = organizePages[parseInt(i.toString(), 10)].isCopied;
var isImportedDoc = organizePages[parseInt(i.toString(), 10)].isImportedDoc;
var rotation = organizePages[parseInt(i.toString(), 10)].rotateAngle;
if (isImportedDoc) {
importPageCount++;
}
if (!isNullOrUndefined(currentPageNumber) && !isNullOrUndefined(rotation) &&
!isDeleted && !isInserted && !isCopied && !isImportedDoc && pageNumber !== -1) {
var page = this.loadedDocument.getPage(currentPageNumber - importPageCount);
page.rotation = this.getPdfRotationAngle(rotation);
}
}
}
};
PdfRenderer.prototype.insertPdfPages = function (organizePages) {
if (organizePages.length > 0) {
var copiedPageCount = 0;
for (var i = 0; i < organizePages.length; i++) {
var pageNumber = organizePages[parseInt(i.toString(), 10)].pageIndex;
var currentPageNumber = organizePages[parseInt(i.toString(), 10)].currentPageIndex;
var isDeleted = organizePages[parseInt(i.toString(), 10)].isDeleted;
var isInserted = organizePages[parseInt(i.toString(), 10)].isInserted;
var isCopied = organizePages[parseInt(i.toString(), 10)].isCopied;
var isImportedDoc = organizePages[parseInt(i.toString(), 10)].isImportedDoc;
var pageSize = void 0;
if (!isNullOrUndefined(organizePages[parseInt(i.toString(), 10)].pageSize)) {
pageSize = [this.convertPixelToPoint(organizePages[parseInt(i.toString(), 10)].pageSize.width),
this.convertPixelToPoint(organizePages[parseInt(i.toString(), 10)].pageSize.height)];
}
if (isCopied || isImportedDoc) {
copiedPageCount++;
}
if (!isNullOrUndefined(currentPageNumber) && !isDeleted && isInserted && pageNumber === -1) {
var rotation = organizePages[parseInt(i.toString(), 10)].rotateAngle;
var pageSettings = new PdfPageSettings();
pageSettings.rotation = this.getPdfRotationAngle(rotation);
if (!isNullOrUndefined(pageSize)) {
pageSettings.size = pageSize;
}
this.loadedDocument.addPage(currentPageNumber - copiedPageCount, pageSettings);
}
}
}
};
PdfRenderer.prototype.copyPages = function (organizePages, clonedDocument) {
if (organizePages.length > 0) {
var importPageCount = 0;
for (var i = 0; i < organizePages.length; i++) {
var pageNumber = organizePages[parseInt(i.toString(), 10)].pageIndex;
var currentPageNumber = organizePages[parseInt(i.toString(), 10)].currentPageIndex;
var copiedPageIndex = organizePages[parseInt(i.toString(), 10)].copiedPageIndex;
var isDeleted = organizePages[parseInt(i.toString(), 10)].isDeleted;
var isInserted = organizePages[parseInt(i.toString(), 10)].isInserted;
var isCopied = organizePages[parseInt(i.toString(), 10)].isCopied;
var isImportedDoc = organizePages[parseInt(i.toString(), 10)].isImportedDoc;
var rotation = organizePages[parseInt(i.toString(), 10)].rotateAngle;
if (isImportedDoc) {
importPageCount++;
}
if (!isNullOrUndefined(currentPageNumber) && !isDeleted && !isInserted && !isImportedDoc && isCopied && pageNumber === -1) {
var pageToImport = clonedDocument.getPage(copiedPageIndex);
pageToImport.rotation = this.getPdfRotationAngle(rotation);
var options = new PdfPageImportOptions();
options.targetIndex = currentPageNumber - importPageCount;
options.groupFormFields = true;
this.loadedDocument.importPage(pageToImport, clonedDocument, options);
}
}
}
};
PdfRenderer.prototype.importPages = function (organizePages) {
if (organizePages.length > 0) {
for (var i = organizePages.length - 1; i >= 0; i--) {
var pageNumber = organizePages[parseInt(i.toString(), 10)].pageIndex;
var currentPageNumber = organizePages[parseInt(i.toString(), 10)].currentPageIndex;
var isDeleted = organizePages[parseInt(i.toString(), 10)].isDeleted;
var isInserted = organizePages[parseInt(i.toString(), 10)].isInserted;
var isCopied = organizePages[parseInt(i.toString(), 10)].isCopied;
var isImportedDoc = organizePages[parseInt(i.toString(), 10)].isImportedDoc;
var documentData = organizePages[parseInt(i.toString(), 10)].documentData;
var password = organizePages[parseInt(i.toString(), 10)].password;
if (!isNullOrUndefined(currentPageNumber) && !isDeleted && !isInserted && !isCopied && isImportedDoc &&
pageNumber === -1) {
var importedDocCountBefore = 0;
for (var j = 0; j < i; j++) {
if (organizePages[parseInt(j.toString(), 10)].isImportedDoc) {
importedDocCountBefore++;
}
}
documentData = this.pdfViewerBase.convertBase64(documentData);
this.document = null;
this.document = new PdfDocument(documentData, password);
var options = new PdfPageImportOptions();
options.targetIndex = currentPageNumber - importedDocCountBefore;
options.groupFormFields = true;
this.loadedDocument.importPageRange(this.document, 0, this.document.pageCount - 1, options);
}
}
}
};
PdfRenderer.prototype.deletePdfPages = function (organizePages, reorderedPages) {
if (organizePages.length > 0) {
var clonedCollection = JSON.parse(JSON.stringify(organizePages));
var sortedCollection = [];
var deleteCount = 0;
var initialPageCount = this.loadedDocument.pageCount;
var _loop_1 = function (i) {
if (clonedCollection.
find(function (item) { return item.pageIndex === reorderedPages[parseInt(i.toString(), 10)]; })) {
sortedCollection[parseInt(i.toString(), 10)] = clonedCollection.
find(function (item) { return item.pageIndex === reorderedPages[parseInt(i.toString(), 10)]; });
}
};
for (var i = 0; i < clonedCollection.length; i++) {
_loop_1(i);
}
for (var i = sortedCollection.length - 1; i >= 0; i--) {
var count = 0;
var pageNumber = sortedCollection[parseInt(i.toString(), 10)].pageIndex;
var isDeleted = sortedCollection[parseInt(i.toString(), 10)].isDeleted;
if (!isNullOrUndefined(pageNumber) && isDeleted) {
if (deleteCount + 1 === initialPageCount) {
this.loadedDocument.addPage();
this.isDummyInserted = true;
}
for (var j = i - 1; j >= 0; j--) {
if (!(i === sortedCollection[parseInt(i.toString(), 10)].pageIndex)) {
count++;
}
}
if (count > sortedCollection[parseInt(i.toString(), 10)].pageIndex) {
count = count - sortedCollection[parseInt(i.toString(), 10)].pageIndex;
this.loadedDocument.removePage(pageNumber + count);
}
else {
this.loadedDocument.removePage(pageNumber);
}
deleteCount++;
}
}
}
};
PdfRenderer.prototype.getPdfRotationAngle = function (rotation) {
switch (rotation) {
case 0:
return PdfRotationAngle.angle0;
case 90:
return PdfRotationAngle.angle90;
case 180:
return PdfRotationAngle.angle180;
case 270:
return PdfRotationAngle.angle270;
default:
return PdfRotationAngle.angle0;
}
};
/**
* @param {any} jsonObject - jsonObject
* @private
* @returns {any} - any
*/
PdfRenderer.prototype.importAnnotations = function (jsonObject) {
try {
var annotationRenderer = new AnnotationRenderer(this.pdfViewer, this.pdfViewerBase);
var annotData = jsonObject['importedData'];
annotationRenderer.removeSignatureTypeAnnot(jsonObject, this.loadedDocument);
var annotationDataFormat = jsonObject['annotationDataFormat'];
if (typeof annotationDataFormat === 'string') {
this.loadedDocument._allowImportCustomData = true;
switch (annotationDataFormat.toLowerCase()) {
case 'json':
this.loadedDocument.importAnnotations(annotData, DataFormat.json);
break;
case 'xfdf':
this.loadedDocument.importAnnotations(annotData, DataFormat.xfdf);
break;
default:
break;
}
for (var i = 0; i < this.loadedDocument.pageCount; i++) {
var pageNumber = i;
var pageSize = this.getPageSize(pageNumber);
var renderer = new PageRenderer(this.pdfViewer, this.pdfViewerBase);
renderer.exportAnnotationComments(pageNumber, pageSize);
this.annotationDetailCollection[i.toString()] = new Annotations();
this.annotationDetailCollection[parseInt(i.toString(), 10)].textMarkupAnnotation = renderer.textMarkupAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].shapeAnnotation = renderer.shapeAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].measureShapeAnnotation = renderer.measureAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].stampAnnotations = renderer.rubberStampAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].stickyNotesAnnotation = renderer.stickyAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].freeTextAnnotation = renderer.freeTextAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].signatureAnnotation = renderer.signatureAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].signatureInkAnnotation =
renderer.signatureInkAnnotationList;
this.annotationDetailCollection[parseInt(i.toString(), 10)].annotationOrder = renderer.annotationOrder;
this.removeAnnotationsFromCollection(renderer);
}
var returnAnnot = JSON.parse(JSON.stringify(this.annotationDetailCollection));
if (Object.prototype.hasOwnProperty.call(jsonObject, 'uniqueId')) {
return { pdfAnnotation: returnAnnot, uniqueId: jsonObject['uniqueId'] };
}
else {
return { pdfAnnotation: returnAnnot };
}
}
}
catch (e) {
return e.message;
}
};
PdfRenderer.prototype.removeAnnotationsFromCollection = function (renderer) {
renderer.textMarkupAnnotationList = [];
renderer.shapeAnnotationList = [];
renderer.measureAnnotationList = [];
renderer.rubberStampAnnotationList = [];
renderer.stickyAnnotationList = [];
renderer.freeTextAnnotationList = [];
renderer.signatureAnnotationList = [];
renderer.signatureInkAnnotationList = [];
renderer.annotationOrder = [];
};
/**
* @param {string} jsonObject - jsonObject
* @param {boolean} isObject - isObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.exportAnnotation = function (jsonObject, isObject) {
var annotationRenderer = new AnnotationRenderer(this.pdfViewer, this.pdfViewerBase);
annotationRenderer.removeSignatureTypeAnnot(jsonObject, this.loadedDocument);
this.orderAnnotations(jsonObject);
var settings = new PdfAnnotationExportSettings();
var annotationDataFormat = jsonObject['annotationDataFormat'];
var fileName;
var exportObject;
if (typeof annotationDataFormat === 'string') {
switch (annotationDataFormat.toLowerCase()) {
case 'json':
settings.dataFormat = DataFormat.json;
if (isObject) {
settings.exportAppearance = isObject;
exportObject = this.loadedDocument.exportAnnotations(settings);
return exportObject;
}
else {
fileName = this.changeFileExtension(this.pdfViewer.fileName, 'json');
return this.loadedDocument.exportAnnotations(settings);
}
case 'xfdf':
settings.dataFormat = DataFormat.xfdf;
if (isObject) {
settings.exportAppearance = isObject;
exportObject = this.loadedDocument.exportAnnotations(settings);
return exportObject;
}
else {
fileName = this.changeFileExtension(this.pdfViewer.fileName, 'xfdf');
return this.loadedDocument.exportAnnotations(settings);
}
// Add more cases for other supported formats as needed
default:
break;
}
}
return null;
};
PdfRenderer.prototype.changeFileExtension = function (filename, newExtension) {
var lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex === -1) {
// No extension found in the filename
return filename + '.' + newExtension;
}
var nameWithoutExtension = filename.slice(0, lastDotIndex);
return nameWithoutExtension + '.' + newExtension;
};
PdfRenderer.prototype.orderAnnotations = function (jsonObject) {
var annotationRenderer = new AnnotationRenderer(this.pdfViewer, this.pdfViewerBase);
var signatureModule = new SignatureBase(this.pdfViewer, this.pdfViewerBase);
if (Object.prototype.hasOwnProperty.call(jsonObject, 'isAnnotationsExist') && jsonObject.isAnnotationsExist) {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'annotationCollection')) {
var annotationDetails = JSON.parse(jsonObject.annotationCollection);
var count = annotationDetails.length;
var _loop_2 = function (i) {
var annotationType = annotationDetails[parseInt(i.toString(), 10)].shapeAnnotationType;
var details = annotationDetails[parseInt(i.toString(), 10)];
if (Object.prototype.hasOwnProperty.call(details, 'calibrate') && (details['shapeAnnotationType'] === 'Circle' || details['shapeAnnotationType'] === 'Line' || details['shapeAnnotationType'] === 'Polygon' || details['shapeAnnotationType'] === 'Polyline')) {
annotationType = 'measureShapes';
}
else if (!(Object.prototype.hasOwnProperty.call(details, 'calibrate')) && (details['shapeAnnotationType'] === 'Line' || details['shapeAnnotationType'] === 'Circle' || details['shapeAnnotationType'] === 'Polygon' || details['shapeAnnotationType'] === 'Square' || details['shapeAnnotationType'] === 'Polyline')) {
annotationType = 'shapeAnnotation';
}
switch (annotationType) {
case 'textMarkup':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'textMarkupAnnotations')) {
var textMarkupDetails = JSON.parse(jsonObject.textMarkupAnnotations);
var pageNumber = details['pageNumber'].toString();
var annotationCount = textMarkupDetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var textMarkup = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (textMarkup) {
details = textMarkup;
annotationRenderer.addTextMarkup(details, this_1.loadedDocument);
}
}
break;
case 'shapeAnnotation':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'shapeAnnotations')) {
var shapeDetails = JSON.parse(jsonObject.shapeAnnotations);
var pageNumber = details['pageNumber'].toString();
var annotationCount = shapeDetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var page = this_1.loadedDocument.getPage(parseInt(pageNumber, 10));
var shape = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (shape) {
details = shape;
annotationRenderer.addShape(details, page);
}
}
break;
case 'stamp':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'stampAnnotations')) {
var stampdetails = JSON.parse(jsonObject.stampAnnotations);
var pageNumber = details['pageNumber'].toString();
var annotationCount = stampdetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var page = this_1.loadedDocument.getPage(parseInt(pageNumber, 10));
var stamp = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (stamp) {
details = stamp;
annotationRenderer.addCustomStampAnnotation(details, page);
}
}
break;
case 'measureShapes':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'measureShapeAnnotations')) {
var shapeDetails = JSON.parse(jsonObject.measureShapeAnnotations);
var pageNumber = details['pageNumber'].toString();
var annotationCount = shapeDetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var page = this_1.loadedDocument.getPage(parseInt(pageNumber, 10));
var shape = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (shape) {
details = shape;
annotationRenderer.addMeasure(details, page);
}
}
break;
case 'sticky':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'stickyNotesAnnotation')) {
var shapeDetails = JSON.parse(jsonObject.stickyNotesAnnotation);
var pageNumber = details['pageNumber'].toString();
var annotationCount = shapeDetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var page = this_1.loadedDocument.getPage(parseInt(pageNumber, 10));
var shape = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (shape) {
details = shape;
annotationRenderer.addStickyNotes(details, page);
}
}
break;
case 'Ink':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'inkSignatureData')) {
var shapeDetails = JSON.parse(jsonObject.inkSignatureData);
var pageNumber = details['pageNumber'].toString();
var annotationCount = shapeDetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var page = this_1.loadedDocument.getPage(parseInt(pageNumber, 10));
var shape = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (shape) {
details = shape;
annotationRenderer.saveInkSignature(details, page);
}
}
break;
case 'FreeText':
if (Object.prototype.hasOwnProperty.call(jsonObject, 'freeTextAnnotation')) {
var freeTextDetails = JSON.parse(jsonObject.freeTextAnnotation);
var pageNumber = details['pageNumber'].toString();
var annotationCount = freeTextDetails[parseInt(pageNumber, 10)];
var pageAnnotations = annotationCount;
var page = this_1.loadedDocument.getPage(parseInt(pageNumber, 10));
var freeText = pageAnnotations.find(function (obj) { return obj['annotName'].toString() === details['annotationId'].toString(); });
if (!isNullOrUndefined(freeText)) {
details = freeText;
if (!isNullOrUndefined(this_1.FallbackFontCollection) &&
Object.keys(this_1.FallbackFontCollection).length !== 0) {
annotationRenderer.addFreeText(details, page, this_1.FallbackFontCollection);
}
else {
annotationRenderer.addFreeText(details, page);
}
}
}
break;
default:
break;
}
};
var this_1 = this;
for (var i = 0; i < count; i++) {
_loop_2(i);
}
if (jsonObject.signatureData) {
if (jsonObject.isSignatureEdited) {
signatureModule.saveSignatureAsAnnotatation(jsonObject, this.loadedDocument);
}
else {
signatureModule.saveSignatureData(jsonObject, this.loadedDocument);
}
}
}
}
};
/**
* @param {any} jsonObject - jsonObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.getAnnotationComments = function (jsonObject) {
try {
if (Object.prototype.hasOwnProperty.call(jsonObject, 'pageStartIndex') && !isNullOrUndefined(jsonObject.pageStartIndex)) {
var pageStartIndex = parseInt(jsonObject.pageStartIndex, 10);
var pageEndIndex = parseInt(jsonObject.pageEndIndex, 10);
var annotationDetails = {};
for (var i = pageStartIndex; i < pageEndIndex; i++) {
var pageNumber = i;
var pageSize = this.getPageSize(pageNumber);
this.renderer = new PageRenderer(this.pdfViewer, this.pdfViewerBase);
annotationDetails[pageNumber.toString()] = this.renderer.exportAnnotationComments(pageNumber, pageSize);
}
return { uniqueId: jsonObject.uniqueId, annotationDetails: annotationDetails,
startPageIndex: pageStartIndex, endPageIndex: pageEndIndex, isAnnotationPresent: this.renderer.isAnnotationPresent };
}
return '';
}
catch (error) {
return error.message;
}
};
/**
* @param {any} jsonObject - jsonObject
* @private
* @returns {void}
*/
PdfRenderer.prototype.getBookmarks = function (jsonObject) {
try {
var bookmark = this.loadedDocument.bookmarks;
if (!isNullOrUndefined(bookmark) && Object.prototype.hasOwnProperty.call(jsonObject, 'bookmarkStyles')) {
for (var i = 0; i < bookmark.count; i++) {
this.retrieveFontStyles(bookmark.at(i), false);
}
}
if (isNullOrUndefined(bookmark) || isNullOrUndefined(bookmark.count) || bookmark.count === 0) {
return null;
}
else {
for (var i = 0; i < bookmark.count; i++) {
var pdfLoadedBookmark = bookmark.at(i);
var parentBookmarkDestination = new BookmarkDestination();
var bookmarkDestination = pdfLoadedBookmark.destination ?
pdfLoadedBookmark.destination : pdfLoadedBookmark.namedDestination ?
pdfLoadedBookmark.namedDestination.destination ? pdfLoadedBookmark.namedDestination.destination : null : null;
parentBookmarkDestination.X = !isNullOrUndefined(bookmarkDestination) ? bookmarkDestination.location[0] : 0;
parentBookmarkDestination.PageIndex = !isNullOrUndefined(bookmarkDestination) ? bookmarkDestination.pageIndex : 0;
parentBookmarkDestination.Zoom = !isNullOrUndefined(bookmarkDestination) ? bookmarkDestination.zoom : 0;
var parentBookmark = new BookmarkBase();
parentBookmark.Id = ++this.id;
parentBookmark.Title = pdfLoadedBookmark.title;
parentBookmark.Child = [];
parentBookmark.FileName = !isNullOrUndefined(pdfLoadedBookmark.action) ? pdfLoadedBookmark.action.toString() : '';
if (!isNullOrUndefined(bookmarkDestination)) {
if (bookmarkDestination.page.rotation === PdfRotationAngle.angle90) {
parentBookmarkDestination.Y = this.convertPointToPixel(bookmarkDestination.page.size[0]) -
this.convertPointToPixel(Math.abs(bookmarkDestination.location[1]));
}
else if (bookmarkDestination.page.rotation === PdfRotationAngle.angle270) {
parentBookmarkDestination.Y = this.convertPointToPixel(Math.abs(bookmarkDestination.location[1]));
}
else {
parentBookmarkDestination.Y = this.convertPointToPixel(bookmarkDestination.page.size[1]) -
this.convertPointToPixel(Math.abs(bookmarkDestination.location[1]));
}
}
else {
parentBookmarkDestination.Y = 0;
}
this.bookmarkDictionary[this.id.toString()] = parentBookmarkDestination;
parentBookmark.Child = this.getChildrenBookmark(pdfLoadedBookmark);
if (parentBookmark.Child.length > 0) {
parentBookmark.HasChild = true;
}
this.bookmarkCollection.push(parentBookmark);
}
}
if (Object.prototype.hasOwnProperty.call(jsonObject, 'uniqueId')) {
return { Bookmarks: JSON.parse(JSON.stringify(this.bookmarkCollection)), BookmarksDestination: JSO