I need to draw a rectangle over pdf selected zone and get the exact coordinates to be used later in OCR the Zone. I have used ngx-extended-pdf-viewer but it didn't do it. Then I tried to add a canvas but it disabled the viewer controls and it draws the rectangle far away from the selected area.
Here is the code I used
HTML
<div style="position: relative;width: 50%;height: 90vh;" class="col-6">
<ngx-extended-pdf-viewer
[base64Src]="base64string.split(',')[1]"
height="90vh"
[zoom]="'page-actual'"
[useBrowserLocale]="true"
[textLayer]="true"
[showHandToolButton]="true">
</ngx-extended-pdf-viewer>
<canvas style="position: absolute; top: 0; left: 0; z-index: 1; width: 100%; height: 100%;border: red 2px solid;" (mousedown)="onMouseDown($event)" (mouseup)="onMouseUp($event)"></canvas>
</div>
TypeScript
onMouseDown(event: MouseEvent) {
if(this.fileDroped){
const canvas = document.getElementsByTagName('canvas')[0];
if (canvas) {
const rect = canvas.getBoundingClientRect();
this.startX = event.clientX - rect.left;
this.startY = event.clientY - rect.top;
this.isDrawing = true;
}
}
}
onMouseUp(event: MouseEvent) {
if(this.fileDroped){
const canvas = document.getElementsByTagName('canvas')[0];
if (canvas && this.isDrawing) {
const rect = canvas.getBoundingClientRect();
this.endX = event.clientX - rect.left;
this.endY = event.clientY - rect.top;
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.strokeStyle = 'blue';
ctx.lineWidth = 2;
ctx.strokeRect(this.startX, this.startY, (this.endX - this.startX), (this.endY - this.startY));
}
console.log((this.startX)+','+(this.startY)+','+((this.endX - this.startX))+','+((this.endY - this.startY)) );
this.isDrawing = false;
}
}
}
I have tried ngx-extended-pdf-viewer and canvas. I also tried ng2-pdf-viewer but it didn't event show the pdf. I also tried src = file and src= base64string.
I also want to implement the same functionality in my OCR application.
For the issue where the PDF in ng2-pdf-viewer is not showing, you just have to give a custom height property to it.