Skip to content

Commit 247d978

Browse files
834573: optimized code using getPageInfo
1 parent 21631fb commit 247d978

File tree

1 file changed

+11
-21
lines changed
  • How to/Library Bounds to Viewer Bounds/src/app

1 file changed

+11
-21
lines changed

How to/Library Bounds to Viewer Bounds/src/app/app.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pdf } from '@syncfusion/ej2';
12
import { PdfViewer, TextSelection, TextSearch, Print, Navigation, Toolbar, Magnification, Annotation, FormDesigner, FormFields } from '@syncfusion/ej2-pdfviewer';
23

34
// Inject required modules
@@ -11,19 +12,6 @@ const pdfviewer: PdfViewer = new PdfViewer({
1112
// Append the PdfViewer to the DOM
1213
pdfviewer.appendTo('#PdfViewer');
1314

14-
const pageSizes: Array<{ Height: number }> = [];
15-
16-
// Event for AJAX request success
17-
pdfviewer.ajaxRequestSuccess = (args: { action: string; data: { pageSizes: Array<{ Height: number }> } }) => {
18-
if (args.action === 'Load') {
19-
const objLength = Object.keys(args.data.pageSizes).length;
20-
for (let x = 0; x < objLength; x++) {
21-
const pageSize = args.data.pageSizes[x];
22-
pageSizes.push(pageSize);
23-
}
24-
}
25-
};
26-
2715
// Event for export success
2816
pdfviewer.exportSuccess = (args: { exportData: string }) => {
2917
console.log(args.exportData);
@@ -40,15 +28,14 @@ pdfviewer.exportSuccess = (args: { exportData: string }) => {
4028
let rect: { x: number; y: number; width: number; height: number } | null = null;
4129

4230
if (data && data.rect && parseInt(data.rect.width)) {
43-
const pageHeight = pageSizes[parseInt(data.page)].Height;
4431

32+
//Get PageSize using getPageInfo API
33+
const pageInfo = pdfviewer.getPageInfo(parseInt(data.page));
34+
const pageHeight: number = pageInfo?.height ?? 0;
4535
// Converting PDF Library values into PDF Viewer values.
4636
rect = {
4737
x: (parseInt(data.rect.x) * 96) / 72,
48-
49-
// Converting pageHeight from pixels(PDF Viewer) to points(PDF Library) for accurate positioning
50-
// The conversion factor of 72/96 is used to change pixel values to points
51-
y: (pageHeight * 72 / 96 - parseInt(data.rect.height)) * 96 / 72,
38+
y: (pageHeight - parseInt(data.rect.height)) * 96 / 72,
5239
width: (parseInt(data.rect.width) - parseInt(data.rect.x)) * 96 / 72,
5340
height: (parseInt(data.rect.height) - parseInt(data.rect.y)) * 96 / 72,
5441
};
@@ -58,11 +45,14 @@ pdfviewer.exportSuccess = (args: { exportData: string }) => {
5845
const [startX, startY] = data.start.split(',').map(Number);
5946
const [endX, endY] = data.end.split(',').map(Number);
6047

61-
const pageHeight = pageSizes[parseInt(data.page)].Height;
48+
//Get PageSize using getPageInfo API
49+
const pageInfo = pdfviewer.getPageInfo(parseInt(data.page));
50+
const pageHeight: number = pageInfo?.height ?? 0;
51+
6252
const pdfStartX = (startX * 96) / 72;
63-
const pdfStartY = (pageHeight * 72 / 96 - startY) * 96 / 72;
53+
const pdfStartY = (pageHeight - startY) * 96 / 72;
6454
const pdfEndX = (endX * 96) / 72;
65-
const pdfEndY = (pageHeight * 72 / 96 - endY) * 96 / 72;
55+
const pdfEndY = (pageHeight - endY) * 96 / 72;
6656

6757
rect = {
6858
x: Math.min(pdfStartX, pdfEndX),

0 commit comments

Comments
 (0)