From 56af2238d5029a743909f5293a29b8a2398b4578 Mon Sep 17 00:00:00 2001 From: SF4524LogeshKumar Date: Tue, 29 Oct 2024 15:48:25 +0530 Subject: [PATCH 1/5] 834573: Library Bounds to Viewer bounds Sample --- .../index.html | 38 ++++++++ .../Library Bounds to Viewer bounds/index.js | 88 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 How to/Library Bounds to Viewer bounds/index.html create mode 100644 How to/Library Bounds to Viewer bounds/index.js diff --git a/How to/Library Bounds to Viewer bounds/index.html b/How to/Library Bounds to Viewer bounds/index.html new file mode 100644 index 0000000..269b2bb --- /dev/null +++ b/How to/Library Bounds to Viewer bounds/index.html @@ -0,0 +1,38 @@ + + EJ2 PDF Viewer + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + + + + \ No newline at end of file diff --git a/How to/Library Bounds to Viewer bounds/index.js b/How to/Library Bounds to Viewer bounds/index.js new file mode 100644 index 0000000..bec7f58 --- /dev/null +++ b/How to/Library Bounds to Viewer bounds/index.js @@ -0,0 +1,88 @@ +var pdfviewer = new ej.pdfviewer.PdfViewer({ + documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf', + serviceUrl: 'https://services.syncfusion.com/js/production/api/pdfviewer' +}); +ej.pdfviewer.PdfViewer.Inject(ej.pdfviewer.TextSelection, ej.pdfviewer.TextSearch, ej.pdfviewer.Print, ej.pdfviewer.Navigation, ej.pdfviewer.Toolbar, + ej.pdfviewer.Magnification, ej.pdfviewer.Annotation, ej.pdfviewer.FormDesigner, ej.pdfviewer.FormFields, ej.pdfviewer.PageOrganizer); +pdfviewer.appendTo('#PdfViewer'); +var pageSizes = []; +pdfviewer.ajaxRequestSuccess = function (args) { + if (args.action === 'Load') { + let objLength = Object.keys(args.data.pageSizes).length; + for (var x = 0; x < objLength; x++) { + var pageSize = args.data.pageSizes[x]; + pageSizes.push(pageSize); + } + } +}; + + +pdfviewer.exportSuccess = function (args) { + console.log(args.exportData); + const blobURL = args.exportData; + + // Converting the exported blob into object + convertBlobURLToObject(blobURL) + .then((objectData) => { + console.log(objectData); + var datas = objectData; + var shapeAnnotationData = datas['pdfAnnotation'][0]['shapeAnnotation']; + shapeAnnotationData.map(data => { + if (data && data.rect && parseInt(data.rect.width)) { + + //var pageHeight=parseInt(data.rect.height); + var pageHeight = pageSizes[parseInt(data.page)].Height + + // Converting PDF Library values into PDF Viewer values. + var rect = { + x: (parseInt(data.rect.x) * 96) / 72, + y: (parseInt(pageHeight) * 72 / 96 - parseInt(data.rect.height)) * 96 / 72, + width: (parseInt(data.rect.width) - parseInt(data.rect.x)) * 96 / 72, + height: (parseInt(data.rect.height) - parseInt(data.rect.y)) * 96 / 72, + }; + } + if ((data.type == 'Line' || data.type == 'Arrow') && data.start && data.end) { + // Split and parse the start and end points + const [startX, startY] = data.start.split(',').map(Number); + const [endX, endY] = data.end.split(',').map(Number); + + // Convert to PDF Viewer coordinates + const pageHeight = pageSizes[parseInt(data.page)].Height; + const pdfStartX = (startX * 96) / 72; + const pdfStartY = (parseInt(pageHeight) * 72 / 96 - startY) * 96 / 72; + const pdfEndX = (endX * 96) / 72; + const pdfEndY = (parseInt(pageHeight) * 72 / 96 - endY) * 96 / 72; + + rect = { + x: Math.min(pdfStartX, pdfEndX), + y: Math.min(pdfStartY, pdfEndY), + width: Math.abs(pdfEndX - pdfStartX), + height: Math.abs(pdfEndY - pdfStartY), + }; + } + if (rect != null && data.type!='Text') { + console.log(data.name); + console.log(rect); + console.log("-------------------------"); + } + }); + }) + .catch((error) => { + console.error('Error converting Blob URL to object:', error); + }); +}; + +function convertBlobURLToObject(blobURL) { + return fetch(blobURL) + .then((response) => response.blob()) + .then((blobData) => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onloadend = () => { + resolve(JSON.parse(reader.result)); + }; + reader.onerror = reject; + reader.readAsText(blobData); + }); + }); +} From e77b06e398c545b3236ba5ac06d7213871481ce1 Mon Sep 17 00:00:00 2001 From: SF4524LogeshKumar Date: Wed, 30 Oct 2024 16:53:51 +0530 Subject: [PATCH 2/5] 834573: comments update. --- How to/Library Bounds to Viewer bounds/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/How to/Library Bounds to Viewer bounds/index.js b/How to/Library Bounds to Viewer bounds/index.js index bec7f58..b3be77a 100644 --- a/How to/Library Bounds to Viewer bounds/index.js +++ b/How to/Library Bounds to Viewer bounds/index.js @@ -36,7 +36,11 @@ pdfviewer.exportSuccess = function (args) { // Converting PDF Library values into PDF Viewer values. var rect = { x: (parseInt(data.rect.x) * 96) / 72, + + // Converting pageHeight from pixels(PDF Viewer) to points(PDF Library) for accurate positioning + // The conversion factor of 72/96 is used to change pixel values to points y: (parseInt(pageHeight) * 72 / 96 - parseInt(data.rect.height)) * 96 / 72, + width: (parseInt(data.rect.width) - parseInt(data.rect.x)) * 96 / 72, height: (parseInt(data.rect.height) - parseInt(data.rect.y)) * 96 / 72, }; From a3137fd85d7bb49cb1fbaddfbcb826400c5a0dcb Mon Sep 17 00:00:00 2001 From: SF4524LogeshKumar Date: Thu, 7 Nov 2024 11:49:06 +0530 Subject: [PATCH 3/5] 834573: optimized code --- .../index.html | 22 ++++++------ .../Library Bounds to Viewer bounds/index.js | 34 ++++++------------- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/How to/Library Bounds to Viewer bounds/index.html b/How to/Library Bounds to Viewer bounds/index.html index 269b2bb..e99157a 100644 --- a/How to/Library Bounds to Viewer bounds/index.html +++ b/How to/Library Bounds to Viewer bounds/index.html @@ -5,19 +5,19 @@ - - - - - - - - - - + + + + + + + + + + - + diff --git a/How to/Library Bounds to Viewer bounds/index.js b/How to/Library Bounds to Viewer bounds/index.js index b3be77a..84b0cdb 100644 --- a/How to/Library Bounds to Viewer bounds/index.js +++ b/How to/Library Bounds to Viewer bounds/index.js @@ -3,19 +3,8 @@ var pdfviewer = new ej.pdfviewer.PdfViewer({ serviceUrl: 'https://services.syncfusion.com/js/production/api/pdfviewer' }); ej.pdfviewer.PdfViewer.Inject(ej.pdfviewer.TextSelection, ej.pdfviewer.TextSearch, ej.pdfviewer.Print, ej.pdfviewer.Navigation, ej.pdfviewer.Toolbar, - ej.pdfviewer.Magnification, ej.pdfviewer.Annotation, ej.pdfviewer.FormDesigner, ej.pdfviewer.FormFields, ej.pdfviewer.PageOrganizer); -pdfviewer.appendTo('#PdfViewer'); -var pageSizes = []; -pdfviewer.ajaxRequestSuccess = function (args) { - if (args.action === 'Load') { - let objLength = Object.keys(args.data.pageSizes).length; - for (var x = 0; x < objLength; x++) { - var pageSize = args.data.pageSizes[x]; - pageSizes.push(pageSize); - } - } -}; - + ej.pdfviewer.Magnification, ej.pdfviewer.Annotation, ej.pdfviewer.FormDesigner, ej.pdfviewer.FormFields, ej.pdfviewer.PageOrganizer); +pdfviewer.appendTo('#PdfViewer'); pdfviewer.exportSuccess = function (args) { console.log(args.exportData); @@ -30,17 +19,12 @@ pdfviewer.exportSuccess = function (args) { shapeAnnotationData.map(data => { if (data && data.rect && parseInt(data.rect.width)) { - //var pageHeight=parseInt(data.rect.height); - var pageHeight = pageSizes[parseInt(data.page)].Height + var pageHeight = pdfviewer.getPageInfo(pdfviewer.currentPageNumber).height; // Converting PDF Library values into PDF Viewer values. var rect = { x: (parseInt(data.rect.x) * 96) / 72, - - // Converting pageHeight from pixels(PDF Viewer) to points(PDF Library) for accurate positioning - // The conversion factor of 72/96 is used to change pixel values to points - y: (parseInt(pageHeight) * 72 / 96 - parseInt(data.rect.height)) * 96 / 72, - + y: (parseInt(pageHeight) - parseInt(data.rect.height)) * 96 / 72, width: (parseInt(data.rect.width) - parseInt(data.rect.x)) * 96 / 72, height: (parseInt(data.rect.height) - parseInt(data.rect.y)) * 96 / 72, }; @@ -51,11 +35,11 @@ pdfviewer.exportSuccess = function (args) { const [endX, endY] = data.end.split(',').map(Number); // Convert to PDF Viewer coordinates - const pageHeight = pageSizes[parseInt(data.page)].Height; + var pageHeight = pdfviewer.getPageInfo(pdfviewer.currentPageNumber).height; const pdfStartX = (startX * 96) / 72; - const pdfStartY = (parseInt(pageHeight) * 72 / 96 - startY) * 96 / 72; + const pdfStartY = (parseInt(pageHeight) - startY) * 96 / 72; const pdfEndX = (endX * 96) / 72; - const pdfEndY = (parseInt(pageHeight) * 72 / 96 - endY) * 96 / 72; + const pdfEndY = (parseInt(pageHeight) - endY) * 96 / 72; rect = { x: Math.min(pdfStartX, pdfEndX), @@ -64,7 +48,8 @@ pdfviewer.exportSuccess = function (args) { height: Math.abs(pdfEndY - pdfStartY), }; } - if (rect != null && data.type!='Text') { + + if (rect != null && data.type != 'Text') { console.log(data.name); console.log(rect); console.log("-------------------------"); @@ -76,6 +61,7 @@ pdfviewer.exportSuccess = function (args) { }); }; +// Function to convert Blob URL to object function convertBlobURLToObject(blobURL) { return fetch(blobURL) .then((response) => response.blob()) From 0b94df049d7982c8ac7fc25bbe71a80594eb09b8 Mon Sep 17 00:00:00 2001 From: SF4524LogeshKumar Date: Thu, 7 Nov 2024 12:05:12 +0530 Subject: [PATCH 4/5] 834573: updated argument for getPageInfo --- How to/Library Bounds to Viewer bounds/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/How to/Library Bounds to Viewer bounds/index.js b/How to/Library Bounds to Viewer bounds/index.js index 84b0cdb..53aead7 100644 --- a/How to/Library Bounds to Viewer bounds/index.js +++ b/How to/Library Bounds to Viewer bounds/index.js @@ -19,7 +19,7 @@ pdfviewer.exportSuccess = function (args) { shapeAnnotationData.map(data => { if (data && data.rect && parseInt(data.rect.width)) { - var pageHeight = pdfviewer.getPageInfo(pdfviewer.currentPageNumber).height; + var pageHeight = pdfviewer.getPageInfo(parseInt(data.page)).height; // Converting PDF Library values into PDF Viewer values. var rect = { @@ -35,7 +35,7 @@ pdfviewer.exportSuccess = function (args) { const [endX, endY] = data.end.split(',').map(Number); // Convert to PDF Viewer coordinates - var pageHeight = pdfviewer.getPageInfo(pdfviewer.currentPageNumber).height; + var pageHeight = pdfviewer.getPageInfo(parseInt(data.page)).height; const pdfStartX = (startX * 96) / 72; const pdfStartY = (parseInt(pageHeight) - startY) * 96 / 72; const pdfEndX = (endX * 96) / 72; From 216b6718cd932ea33699b52f67bfb41d0b71a083 Mon Sep 17 00:00:00 2001 From: SF4524LogeshKumar Date: Thu, 7 Nov 2024 12:27:08 +0530 Subject: [PATCH 5/5] 834573: cleaned code --- How to/Library Bounds to Viewer bounds/index.html | 1 - 1 file changed, 1 deletion(-) diff --git a/How to/Library Bounds to Viewer bounds/index.html b/How to/Library Bounds to Viewer bounds/index.html index e99157a..91f355d 100644 --- a/How to/Library Bounds to Viewer bounds/index.html +++ b/How to/Library Bounds to Viewer bounds/index.html @@ -4,7 +4,6 @@ -