-
I am trying to put something together to allow me to drag and drop on a page in my pdf and get a visible "selection box" showing while im holding the mouse down until i let go of the mouse. at that point id like to get the instance to the canvas of the page im on and cut the image from the canvas out as an image using the coordinates of the box i dragged and dropped. Is something like this already present in the library and I just have not found it yet? If not, any ideas as to why this isnt working as expected? My extended pdf viewer is initialized like this using a blob as the source (turned off handTool to try to allow for a typical mouse drag and drop event without the pdf moving) <ngx-extended-pdf-viewer
class="h-full w-full"
[src]="selectedFileBlob"
(pageRendered)="onPageRendered($event)"
[handTool]="false"
></ngx-extended-pdf-viewer> the onPageRendered method uses the pdf service to get the canvas of the page that was just rendered and sends it to another method to set up some listeners on it onPageRendered(event: PageRenderedEvent) {
const pageNumber = event.pageNumber;
if (event.pageNumber) {
this.pdfService.getPageAsCanvas(pageNumber, { scale: 1 }).then((canvas) => {
if (canvas) {
this.setupEventsForCanvas(canvas);
}
});
}
} the setupEventsForCanvas method takes this canvas and adds mousedown, mouseup, and mousemove events to the canvas so that we can keep track of the rectangle and attempt to draw a box on the canvas during the duration of the drag and drop. when the mousedown event fires it should try to get the image from the canvas and set it for me to use. private setupEventsForCanvas(canvas: HTMLCanvasElement) {
let startX = 0;
let startY = 0;
let endX = 0;
let endY = 0;
let isDragging = false;
const ctx = canvas.getContext('2d');
if (ctx) {
canvas.addEventListener('mousedown', (e) => {
isDragging = true;
const rect = canvas.getBoundingClientRect();
startX = e.clientX - rect.left;
startY = e.clientY - rect.top;
});
canvas.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const rect = canvas.getBoundingClientRect();
endX = e.clientX - rect.left;
endY = e.clientY - rect.top;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.strokeRect(startX, startY, endX - startX, endY - startY);
});
canvas.addEventListener('mouseup', () => {
isDragging = false;
const image = ctx?.getImageData(startX, startY, endX, endY);
if (image) {
this.image.set(image);
}
});
}
} my issue is that none of these events actually trigger when i click, drag and drop, mouse move, on the canvases in the pdf viewer. Any ideas how I could get something like this working? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Your mistake (or misunderstanding) probably is in this code snippet: onPageRendered(event: PageRenderedEvent) {
const pageNumber = event.pageNumber;
if (event.pageNumber) {
this.pdfService.getPageAsCanvas(pageNumber, { scale: 1 }).then((canvas) => {
if (canvas) {
this.setupEventsForCanvas(canvas);
}
});
}
}
private PDFViewerApplication?: IPDFViewerApplication;
constructor(private readonly rendererFactory: RendererFactory2, notificationService: PDFNotificationService) {
this.renderer = this.rendererFactory.createRenderer(null, null);
effect(() => {
this.PDFViewerApplication = notificationService.onPDFJSInitSignal();
});
}
onPageRendered(event: PageRenderedEvent) {
const pageNumber = event.pageNumber;
if (!pageNumber) return;
const pageDiv = document.getElementById(`page${pageNumber}`);
if (!pageDiv) return;
const canvas = pageDiv.querySelector('.canvasWrapper > canvas') as HTMLCanvasElement | null;
if (!canvas) return;
this.setupEventsForCanvas(canvas);
} Also note that Also note that you're painting on the canvas displaying the PDF page. Basically, you are overwriting it, so the text becomes illegible. Better add a custom |
Beta Was this translation helpful? Give feedback.
-
I suppose I can try adding the listeners to the text layer element. I did try to turn text layer and annotation layer off (as users of my app should not need them as far as I know) before but I remember that they didn’t actually seem to be removed from the dom? Anyway, I’ll hopefully update this discussion with any solution I end up getting to work |
Beta Was this translation helpful? Give feedback.
-
Thanks for the advice! I was able to get this working out after a little while longer (did have to use AI to figure out that I was suffering from the concept of canvas scaling issues due to my retina screen where i was seeing my drag and drop not matching and the image i cut was scaled as well). But here is essentially what i have with a demo video working-example.mov
|
Beta Was this translation helpful? Give feedback.
Thanks for the advice! I was able to get this working out after a little while longer (did have to use AI to figure out that I was suffering from the concept of canvas scaling issues due to my retina screen where i was seeing my drag and drop not matching and the image i cut was scaled as well). But here is essentially what i have with a demo video
working-example.mov