Skip to content

Commit

Permalink
[pdf] Work with stamp annot preview
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita committed Dec 11, 2024
1 parent 464db15 commit f963b29
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions configs/word.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
"pdf/src/viewer.js",
"pdf/src/file.js",
"pdf/src/pdfSearch.js",
"pdf/src/previews.js",
"pdf/src/forms/formActions.js",
"pdf/src/forms/base/base.js",
"pdf/src/forms/apiFunctions.js",
Expand Down
9 changes: 9 additions & 0 deletions pdf/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,14 @@
oDoc.BlurActiveObject();
AscCommon.DocumentEditorApi.prototype.asc_Save.call(this, isAutoSave, isIdle);
};
PDFEditorApi.prototype._onEndLoadSdk = function() {
AscCommon.DocumentEditorApi.prototype._onEndLoadSdk.call(this);

this.stampAnnotPreviewManager = new AscPDF.StampAnnotPreviewManager();
};
PDFEditorApi.prototype.asc_getPropertyEditorStamps = function() {
return this.stampAnnotPreviewManager.getStampPreviews();
};
PDFEditorApi.prototype._coAuthoringInitEnd = function() {
AscCommon.DocumentEditorApi.prototype._coAuthoringInitEnd.call(this);

Expand Down Expand Up @@ -2864,6 +2872,7 @@

// stamp
PDFEditorApi.prototype['AddStampAnnot'] = PDFEditorApi.prototype.AddStampAnnot;
PDFEditorApi.prototype['asc_getPropertyEditorStamps'] = PDFEditorApi.prototype.asc_getPropertyEditorStamps;

// freetext
PDFEditorApi.prototype['AddFreeTextAnnot'] = PDFEditorApi.prototype.AddFreeTextAnnot;
Expand Down
112 changes: 112 additions & 0 deletions pdf/src/previews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* (c) Copyright Ascensio System SIA 2010-2024
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/

"use strict";

(
/**
* @param {Window} window
* @param {undefined} undefined
*/
function(window, undefined) {

function StampAnnotPreviewManager() {
}

StampAnnotPreviewManager.prototype.clear = function() {
this.StampStyles.length = 0;
};

StampAnnotPreviewManager.prototype.getStampStyles = function() {
if (this.StampStyles.length === 0) {
this.generateStampStyles();
}
return this.StampStyles;
};

StampAnnotPreviewManager.prototype.createCanvas = function(width, height) {
var oCanvas = document.createElement('canvas');
oCanvas.width = AscCommon.AscBrowser.convertToRetinaValue(width, true);
oCanvas.height = AscCommon.AscBrowser.convertToRetinaValue(height, true);
return oCanvas;
};

StampAnnotPreviewManager.prototype.getStampRender = function(sType) {
let oDoc = Asc.editor.getPDFDoc();
let oTextDrawer = oDoc.CreateStampRender(sType);

return oTextDrawer;
};

StampAnnotPreviewManager.prototype.getStampPreview = function(type) {
return this.getStampPreviewCanvas(type).toDataURL("image/png");
};
StampAnnotPreviewManager.prototype.getStampPreviews = function() {
let aRet = [];

let _t = this;
Object.entries(AscPDF.STAMP_XML).forEach(function(entry) {
let type = entry[0];

let oPreview = {};
oPreview["Type"] = type;
oPreview["Image"] = _t.getStampPreview(type);
aRet.push(oPreview);
});

return aRet;
};
StampAnnotPreviewManager.prototype.getStampPreviewCanvas = function(type) {
let oTextDrawer = this.getStampRender(type);
if (!oTextDrawer) {
return "";
}

let pxWidth = oTextDrawer.Width * g_dKoef_mm_to_pix;
let pxHeight = oTextDrawer.Height * g_dKoef_mm_to_pix;

let canvas = this.createCanvas(pxWidth, pxHeight);
let ctx = canvas.getContext('2d');
let graphics = new AscCommon.CGraphics();

graphics.init(ctx, canvas.width, canvas.height, oTextDrawer.Width, oTextDrawer.Height);
graphics.m_oFontManager = AscCommon.g_fontManager;
graphics.transform(1, 0, 0, 1, 0, 0);

oTextDrawer.m_aStack[0].draw(graphics);
return canvas;
};

//----------------------------------------------------------export----------------------------------------------------
window['AscPDF'] = window['AscPDF'] || {};
window['AscPDF'].StampAnnotPreviewManager = StampAnnotPreviewManager;
})(window);
112 changes: 112 additions & 0 deletions previews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* (c) Copyright Ascensio System SIA 2010-2024
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/

"use strict";

(
/**
* @param {Window} window
* @param {undefined} undefined
*/
function(window, undefined) {

function StampAnnotPreviewManager() {
}

StampAnnotPreviewManager.prototype.clear = function() {
this.StampStyles.length = 0;
};

StampAnnotPreviewManager.prototype.getStampStyles = function() {
if (this.StampStyles.length === 0) {
this.generateStampStyles();
}
return this.StampStyles;
};

StampAnnotPreviewManager.prototype.createCanvas = function(width, height) {
var oCanvas = document.createElement('canvas');
oCanvas.width = AscCommon.AscBrowser.convertToRetinaValue(width, true);
oCanvas.height = AscCommon.AscBrowser.convertToRetinaValue(height, true);
return oCanvas;
};

StampAnnotPreviewManager.prototype.getStampRender = function(sType) {
let oDoc = Asc.editor.getPDFDoc();
let oTextDrawer = oDoc.CreateStampRender(sType);

return oTextDrawer;
};

StampAnnotPreviewManager.prototype.getStampPreview = function(type) {
return this.getStampPreviewCanvas(type).toDataURL("image/png");
};
StampAnnotPreviewManager.prototype.getStampPreviews = function() {
let aRet = [];

let _t = this;
Object.entries(AscPDF.STAMP_XML).forEach(function(entry) {
let type = entry[0];

let oPreview = {};
oPreview["Type"] = type;
oPreview["Image"] = _t.getStampPreview(type);
aRet.push(oPreview);
});

return aRet;
};
StampAnnotPreviewManager.prototype.getStampPreviewCanvas = function(type) {
let oTextDrawer = this.getStampRender(type);
if (!oTextDrawer) {
return "";
}

let pxWidth = oTextDrawer.Width * g_dKoef_mm_to_pix;
let pxHeight = oTextDrawer.Height * g_dKoef_mm_to_pix;

let canvas = this.createCanvas(pxWidth, pxHeight);
let ctx = canvas.getContext('2d');
let graphics = new AscCommon.CGraphics();

graphics.init(ctx, canvas.width, canvas.height, oTextDrawer.Width, oTextDrawer.Height);
graphics.m_oFontManager = AscCommon.g_fontManager;
graphics.transform(1, 0, 0, 1, 0, 0);

oTextDrawer.m_aStack[0].draw(graphics);
return canvas;
};

//----------------------------------------------------------export----------------------------------------------------
window['AscPDF'] = window['AscPDF'] || {};
window['AscPDF'].StampAnnotPreviewManager = StampAnnotPreviewManager;
})(window);

0 comments on commit f963b29

Please sign in to comment.