Skip to content

834573: Library Bounds to Viewer bounds Sample #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions How to/Library Bounds to Viewer bounds/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 PDF Viewer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript PDF Viewer Control">
<meta name="author" content="Syncfusion">
<!-- <link href="index.css" rel="stylesheet"> -->
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-pdfviewer/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-notifications/styles/material.css" rel="stylesheet">

<!-- Essential JS 2 PDF Viewer's script -->
<script src="https://cdn.syncfusion.com/ej2/27.1.48/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>

<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="PdfViewer" style="height:500px;width:100%;"></div>
</div>


<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
92 changes: 92 additions & 0 deletions How to/Library Bounds to Viewer bounds/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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,

// 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,
};
}
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);
});
});
}
Loading