$(document).one('documentLoaded', function() {
$('html').on('paste', function(e) {
getImageFromClipboard(e.originalEvent.clipboardData, addStamp);
});
});
function getImageFromClipboard(clipboardData, callback) {
var matchType = /image.*/;
var found = false;
return Array.prototype.forEach.call(clipboardData.types, function(type, i) {
var file, reader;
if (found) {
return;
}
if (type.match(matchType) || clipboardData.items[i].type.match(matchType)) {
file = clipboardData.items[i].getAsFile();
reader = new FileReader();
reader.onload = function(evt) {
callback(evt.target.result);
};
reader.readAsDataURL(file);
return found = true;
}
});
}
function getImageSize(imageData, callback) {
var image = document.createElement('img');
image.onload = function() {
callback({
width: image.width,
height: image.height
});
};
image.src = imageData;
}
function constrainSize(size, pageSize, rotation) {
var width = size.width;
var height = size.height;
if (rotation === 90 || rotation === 270) {
var temp = width;
width = height;
height = temp;
}
var aspectRatio = width / height;
var pageWidth = pageSize.width;
var pageHeight = pageSize.height;
width = Math.min(width, pageWidth);
height = Math.min(height, pageHeight);
// if the aspect ratio has changed we need to make sure we maintain it
if ((width / height) !== aspectRatio) {
if (height * aspectRatio < width) {
// the height is constraining the ratio
width = height * aspectRatio;
} else {
// the width is constraining the ratio
height = width / aspectRatio;
}
}
return {
width: width,
height: height
};
}
function addStamp(imageData) {
getImageSize(imageData, function(size) {
var docViewer = readerControl.docViewer;
var annotManager = docViewer.getAnnotationManager();
var currentPage = docViewer.getCurrentPage();
var pageSize = {
width: docViewer.getPageWidth(currentPage - 1),
height: docViewer.getPageHeight(currentPage - 1)
};
var rotation = docViewer.getCompleteRotation(currentPage) * 90;
var finalSize = constrainSize(size, pageSize, rotation);
var stampAnnot = new Annotations.StampAnnotation();
stampAnnot.PageNumber = currentPage;
stampAnnot.X = 0;
stampAnnot.Y = 0;
stampAnnot.Width = finalSize.width;
stampAnnot.Height = finalSize.height;
stampAnnot.Author = annotManager.getCurrentUser();
stampAnnot.Rotation = rotation;
stampAnnot.ImageData = imageData;
annotManager.addAnnotation(stampAnnot);
annotManager.drawAnnotations(stampAnnot.PageNumber);
});
}
Any idea?