Skip to content

Commit afd5088

Browse files
834573: Optimized Code using getPageInfo API
1 parent d9c6d8f commit afd5088

File tree

1 file changed

+19
-35
lines changed
  • How to/Library Bounds to Viewer Bounds/src

1 file changed

+19
-35
lines changed

How to/Library Bounds to Viewer Bounds/src/index.js

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,9 @@ import {
1818
class App extends React.Component {
1919
constructor(props) {
2020
super(props);
21-
this.pageSizes = [];
2221
this.viewerRef = React.createRef();
2322
}
2423

25-
// Event for AJAX request success
26-
handleAjaxRequestSuccess = (args) => {
27-
if (args.action === 'Load') {
28-
const objLength = Object.keys(args.data.pageSizes).length;
29-
for (let x = 0; x < objLength; x++) {
30-
const pageSize = args.data.pageSizes[x];
31-
this.pageSizes.push(pageSize);
32-
}
33-
}
34-
};
35-
3624
// Event for export success
3725
handleExportSuccess = (args) => {
3826
console.log(args.exportData);
@@ -43,38 +31,34 @@ class App extends React.Component {
4331
.then((objectData) => {
4432
console.log(objectData);
4533
const shapeAnnotationData = objectData['pdfAnnotation'][0]['shapeAnnotation'];
34+
let rect = null;
4635
shapeAnnotationData.forEach((data) => {
4736
if (data && data.rect && parseInt(data.rect.width)) {
48-
const pageHeight = this.pageSizes[parseInt(data.page)].Height;
49-
37+
const pageHeight = this.viewerRef.current.getPageInfo(parseInt(data.page)).height;
5038
// Converting PDF Library values into PDF Viewer values.
51-
const rect = {
39+
rect = {
5240
x: (parseInt(data.rect.x) * 96) / 72,
53-
54-
// Converting pageHeight from pixels(PDF Viewer) to points(PDF Library) for accurate positioning
55-
// The conversion factor of 72/96 is used to change pixel values to points
56-
y: (parseInt(pageHeight) * 72 / 96 - parseInt(data.rect.height)) * 96 / 72,
41+
y: (parseInt(pageHeight) - parseInt(data.rect.height)) * 96 / 72,
5742
width: (parseInt(data.rect.width) - parseInt(data.rect.x)) * 96 / 72,
5843
height: (parseInt(data.rect.height) - parseInt(data.rect.y)) * 96 / 72,
5944
};
60-
console.log(data.name, rect, '-------------------------');
61-
}
62-
if ((data.type === 'Line' || data.type === 'Arrow') && data.start && data.end) {
63-
const [startX, startY] = data.start.split(',').map(Number);
64-
const [endX, endY] = data.end.split(',').map(Number);
45+
if ((data.type === 'Line' || data.type === 'Arrow') && data.start && data.end) {
46+
const [startX, startY] = data.start.split(',').map(Number);
47+
const [endX, endY] = data.end.split(',').map(Number);
6548

66-
const pageHeight = this.pageSizes[parseInt(data.page)].Height;
67-
const pdfStartX = (startX * 96) / 72;
68-
const pdfStartY = (parseInt(pageHeight) * 72 / 96 - startY) * 96 / 72;
69-
const pdfEndX = (endX * 96) / 72;
70-
const pdfEndY = (parseInt(pageHeight) * 72 / 96 - endY) * 96 / 72;
49+
const pageHeight = this.viewerRef.current.getPageInfo(parseInt(data.page)).height;
50+
const pdfStartX = (startX * 96) / 72;
51+
const pdfStartY = (parseInt(pageHeight) - startY) * 96 / 72;
52+
const pdfEndX = (endX * 96) / 72;
53+
const pdfEndY = (parseInt(pageHeight) - endY) * 96 / 72;
7154

72-
const rect = {
73-
x: Math.min(pdfStartX, pdfEndX),
74-
y: Math.min(pdfStartY, pdfEndY),
75-
width: Math.abs(pdfEndX - pdfStartX),
76-
height: Math.abs(pdfEndY - pdfStartY),
77-
};
55+
rect = {
56+
x: Math.min(pdfStartX, pdfEndX),
57+
y: Math.min(pdfStartY, pdfEndY),
58+
width: Math.abs(pdfEndX - pdfStartX),
59+
height: Math.abs(pdfEndY - pdfStartY),
60+
};
61+
}
7862
console.log(data.name, rect, '-------------------------');
7963
}
8064
});

0 commit comments

Comments
 (0)