|
| 1 | +var pdfviewer = new ej.pdfviewer.PdfViewer({ |
| 2 | + documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf', |
| 3 | + serviceUrl: 'https://services.syncfusion.com/js/production/api/pdfviewer' |
| 4 | +}); |
| 5 | +ej.pdfviewer.PdfViewer.Inject(ej.pdfviewer.TextSelection, ej.pdfviewer.TextSearch, ej.pdfviewer.Print, ej.pdfviewer.Navigation, ej.pdfviewer.Toolbar, |
| 6 | + ej.pdfviewer.Magnification, ej.pdfviewer.Annotation, ej.pdfviewer.FormDesigner, ej.pdfviewer.FormFields, ej.pdfviewer.PageOrganizer); |
| 7 | +pdfviewer.appendTo('#PdfViewer'); |
| 8 | +var pageSizes = []; |
| 9 | +pdfviewer.ajaxRequestSuccess = function (args) { |
| 10 | + if (args.action === 'Load') { |
| 11 | + let objLength = Object.keys(args.data.pageSizes).length; |
| 12 | + for (var x = 0; x < objLength; x++) { |
| 13 | + var pageSize = args.data.pageSizes[x]; |
| 14 | + pageSizes.push(pageSize); |
| 15 | + } |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | + |
| 20 | +pdfviewer.exportSuccess = function (args) { |
| 21 | + console.log(args.exportData); |
| 22 | + const blobURL = args.exportData; |
| 23 | + |
| 24 | + // Converting the exported blob into object |
| 25 | + convertBlobURLToObject(blobURL) |
| 26 | + .then((objectData) => { |
| 27 | + console.log(objectData); |
| 28 | + var datas = objectData; |
| 29 | + var shapeAnnotationData = datas['pdfAnnotation'][0]['shapeAnnotation']; |
| 30 | + shapeAnnotationData.map(data => { |
| 31 | + if (data && data.rect && parseInt(data.rect.width)) { |
| 32 | + |
| 33 | + //var pageHeight=parseInt(data.rect.height); |
| 34 | + var pageHeight = pageSizes[parseInt(data.page)].Height |
| 35 | + |
| 36 | + // Converting PDF Library values into PDF Viewer values. |
| 37 | + var rect = { |
| 38 | + x: (parseInt(data.rect.x) * 96) / 72, |
| 39 | + y: (parseInt(pageHeight) * 72 / 96 - parseInt(data.rect.height)) * 96 / 72, |
| 40 | + width: (parseInt(data.rect.width) - parseInt(data.rect.x)) * 96 / 72, |
| 41 | + height: (parseInt(data.rect.height) - parseInt(data.rect.y)) * 96 / 72, |
| 42 | + }; |
| 43 | + } |
| 44 | + if ((data.type == 'Line' || data.type == 'Arrow') && data.start && data.end) { |
| 45 | + // Split and parse the start and end points |
| 46 | + const [startX, startY] = data.start.split(',').map(Number); |
| 47 | + const [endX, endY] = data.end.split(',').map(Number); |
| 48 | + |
| 49 | + // Convert to PDF Viewer coordinates |
| 50 | + const pageHeight = pageSizes[parseInt(data.page)].Height; |
| 51 | + const pdfStartX = (startX * 96) / 72; |
| 52 | + const pdfStartY = (parseInt(pageHeight) * 72 / 96 - startY) * 96 / 72; |
| 53 | + const pdfEndX = (endX * 96) / 72; |
| 54 | + const pdfEndY = (parseInt(pageHeight) * 72 / 96 - endY) * 96 / 72; |
| 55 | + |
| 56 | + rect = { |
| 57 | + x: Math.min(pdfStartX, pdfEndX), |
| 58 | + y: Math.min(pdfStartY, pdfEndY), |
| 59 | + width: Math.abs(pdfEndX - pdfStartX), |
| 60 | + height: Math.abs(pdfEndY - pdfStartY), |
| 61 | + }; |
| 62 | + } |
| 63 | + if (rect != null && data.type!='Text') { |
| 64 | + console.log(data.name); |
| 65 | + console.log(rect); |
| 66 | + console.log("-------------------------"); |
| 67 | + } |
| 68 | + }); |
| 69 | + }) |
| 70 | + .catch((error) => { |
| 71 | + console.error('Error converting Blob URL to object:', error); |
| 72 | + }); |
| 73 | +}; |
| 74 | + |
| 75 | +function convertBlobURLToObject(blobURL) { |
| 76 | + return fetch(blobURL) |
| 77 | + .then((response) => response.blob()) |
| 78 | + .then((blobData) => { |
| 79 | + return new Promise((resolve, reject) => { |
| 80 | + const reader = new FileReader(); |
| 81 | + reader.onloadend = () => { |
| 82 | + resolve(JSON.parse(reader.result)); |
| 83 | + }; |
| 84 | + reader.onerror = reject; |
| 85 | + reader.readAsText(blobData); |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
0 commit comments