Spire.PDFViewer is a powerful PDF Viewer component for .NET. It allows developers to load PDF document from stream, file and byte array.

Tue Mar 24, 2020 3:56 am

I currently show a pdf file with pdfviewer and would like to let the user to draw a rectangle on it. The application then capture the position of the rectangle corresponding to the pdf file (not the screen). I currently create a Graphincs control and draw the rectangle according to the user's cursor's coordinates. My question is how should I capture the coordinate of the drawn rectangle with respect to the pdf and how should I get the position of the mouse when the pdf is being scrolled or zoomed? Are there any example doing what I want?

Thank you very much

gavinlai
 
Posts: 2
Joined: Fri Mar 20, 2020 3:20 am

Tue Mar 24, 2020 10:26 am

Hi,

Thanks for your inquiry.
The units of data captured by the control are pixels, but in pdf document, the units of data are points.
You only need to convert the data captured by pdfviewer control or other controls to data in points according to zoom factor.
Please refer to the below code to do the unit conversion.
Code: Select all
  PdfUnitConvertor unitconvertor = new PdfUnitConvertor();
           //Convert data in Pixels to data in Points
            int x-for-Pdf-document = (int)(unitconvertor.ConvertFromPixels(X-captured-by-controls, PdfGraphicsUnit.Point) / zoomFactor);
            int y-for-Pdf-document= (int)(unitconvertor.ConvertFromPixels(Y-captured-by-controls, PdfGraphicsUnit.Point) / zoomFactor);

            float width = unitconvertor.ConvertFromPixels(width-captured-by-controls , PdfGraphicsUnit.Point) / zoomFactor;
            float height = unitconvertor.ConvertFromPixels(height-captured-by-controls, PdfGraphicsUnit.Point) / zoomFactor;

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Thu Mar 26, 2020 10:02 am

Hi,

Hope you are doing well.
Did my code help you?
Looking forward to your feedback.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Wed Apr 01, 2020 1:15 am

Thanks, Amy. The code does help. Thank you very much! :)

gavinlai
 
Posts: 2
Joined: Fri Mar 20, 2020 3:20 am

Wed Apr 01, 2020 3:23 am

Hi,

I am glad to hear from you.
Welcome to contact us again if you need further assistances.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDFViewer