Draw rectangle with loaded pdf file in canvas using pdf.js


Sharma Vikram

I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly.

I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening.

Here is my code:

Rendering code for pdf (rendering works fine)

function pdfFile (file) {
pdfjsLib.workerSrc = 'pdf.worker.js';
pdfjsLib.getDocument(file).promise.then(function(pdfDoc) {
  pdf = pdfDoc;
  document.getElementById('page_count').textContent = pdfDoc.numPages;
  showButtonGroup(pdf);
  renderPage(pageNum);
});
} 

 function renderPage(num) {
    pageRendering = true;
    pdf.getPage(num).then(function(page) {
      var viewport = page.getViewport({scale: scale});
      canvas.height = viewport.height;
      canvas.width = viewport.width;
      var renderContext = {
        canvasContext: ctx,
        viewport: viewport
      };
      var renderTask = page.render(renderContext);
      renderTask.promise.then(function() {
        pageRendering = false;
        if (pageNumPending !== null) {
          renderPage(pageNumPending);
          pageNumPending = null;
        }
      });
    });

    document.getElementById('page_num').textContent = num;
}

Mouse movement not working

function mouseMove(e) {
    if (drag) {
        ctx.putImageData(pdfPages[pageNum], 0, 0);
        ctx.clearRect(rect.startX, rect.startY, rect.w, rect.h);
        rect.w = ((e.pageX - x) - this.offsetLeft) - rect.startX;
        rect.h = ((e.pageY - y) - this.offsetTop) - rect.startY;      
        ctx.strokeStyle = color;
        ctx.strokeRect(rect.startX, rect.startY, rect.w, rect.h);
        Object.assign(data, {
            x: rect.startX,
            y: rect.startY,
            w: rect.w,
            h: rect.h
        })
        console.log(data);
    }
}

Notice

  1. When I enable the clearRect and putImageData functions, the rectangle is drawn correctly, but the canvas pdf is showing empty. Here is the attached image

    enter image description here

  2. Only when the clearRect function is enabled, then multiple rectangles are displayed in the pdf. Here is the attached image

    enter image description here

Praveen Kumar

Please check the following, existing rectangles need to be cleared before creating new ones

function renderPage(num) {
    pageRendering = true;
    pdf.getPage(num).then(function (page) {
        var viewport = page.getViewport({ scale: scale });
        canvas.height = viewport.height;
        canvas.width = viewport.width;
        var renderContext = {
            canvasContext: ctx,
            viewport: viewport
        };
        var renderTask = page.render(renderContext);
        renderTask.promise.then(function () {
            pageRendering = false;

            //You need to clear the existing rectangle
            pdfPages[num] = ctx.getImageData(0, 0, canvas.width, canvas.height);

            //Now you can draw new rectangle
            drawRectangle(10, 10, 100, 50);
            if (pageNumPending !== null) {
                renderPage(pageNumPending);
                pageNumPending = null;
            }
        });
    });

    function drawRectangle(x, y, w, h) {
        ctx.strokeStyle = color;
        ctx.strokeRect(x, y, w, h);
    }

Related


Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw the contents of a PDF file to the canvas

multimedia My Android app is using PdfDocument to programmatically generate multipage PDF files: public byte[] buildPDF() { int pageWidth = Math.round(8.5f * 72f); int pageHeight = Math.round(11.0f * 72f); int margin = Math.round(.5f * 72f); in

Draw a rectangle in a PDF document using iText

Milhous: Is it possible to draw rectangles in PDF documents in iText? Milhous: Here is the solution. Thanks Dylan McClung. PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x

Draw a rectangle in a PDF document using iText

Milhous: Is it possible to draw rectangles in PDF documents in iText? Milhous: Here is the solution. Thanks Dylan McClung. PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x

Draw a rectangle in a PDF document using iText

Milhous: Is it possible to draw rectangles in PDF documents in iText? Milhous: Here is the solution. Thanks Dylan McClung. PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x

How to draw path on pdf canvas using itext pdf?

User 123456 I am implementing a pdf editing application using itextpdf. I added a text to the pdf and used the canvas.drawPath() method to draw a name on the android system canvas like I want to draw a name on the pdfCanvas? I tried some examples of drawing cu

How to draw path on pdf canvas using itext pdf?

User 123456 I am implementing a pdf editing application using itextpdf. I added a text to the pdf and used the canvas.drawPath() method to draw a name on the android system canvas, just like I want to draw a name on the pdfCanvas? I tried some examples of draw

Load PDF in Canvas and Drawing Rectangle

Southhurst S I'm trying to build a web page to display a PDF file inside a canvas and allow the user to draw rectangles. Below is the code I am trying. The problem is that the mouse events also move outside the canvas. How to limit mouse drag events only insid

Load PDF in Canvas and Drawing Rectangle

Southhurst S I'm trying to build a web page to display a PDF file inside a canvas and allow the user to draw rectangles. Below is the code I am trying. The problem is that the mouse events also move outside the canvas. How to limit mouse drag events only insid

draw line with canvas in pdf dart

tuner I am using the plugin dart-pdf to generate pdf documents. I need to add a checkbox, but there is no checkbox widget, so I've been trying to draw it with canvas (like this would be perfect). However, I can't even draw a line. The following code does not d

Draw rounded rectangle using RectF and canvas?

TokTok123 I am trying to draw a rounded rectangle using RectF and canvas.drawRoundRect(). Please see my code below: package com.example.test; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

How to display pdf on canvas using angular7 and fabric js

Anjali I want to display pdf as image on canvas using angular7 and fabric js I can't find any code to try in angular7 Anjali Finally I was able to fix this...I have used pdfjsLib and I have imported in my ts file (import * from 'pdfjs-dist/build/pdf' as pdfjsL