# Convert mouse coordinates to PDF units using Aspose.PDF for .NET

I've been googling for the past couple days on how to convert mouse coordinates to pdf units & this has proven to be very difficult...or at least I haven't been able to find it. I have an X,Y pixel coordinate and I have to place stamp in a pdf document in that location. I've found out that pdf units are different & start from the bottom.

I've tried;

point=pixel * 96 / 72.....also tried a few other options like pixel * 150/72 & pixel * 72 / 300.

While the last attempt produced my results for the X coordinate my Y is way off. An example I have is;

x:1341

y:294

I can't believe I'm the first to encounter this so I'm hoping someone can help me out.

Steve.

Hi Steve,

Thanks for your inquiry. After initial investigation, we have logged an investigation ticket as PDFNEWNET-36812 for your requirements. We will keep you updated about the issue progress via this forum thread.

Moreover you are right that PDF coordinates starts from left bottom(0,0) and right top is (page width, page height). Aspose.Pdf measuring unit is point and where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points.

Best Regards,

Hi Steve,

Thanks for contacting support and sorry for the delayed response.

The page height and width properties use points as the basic unit, where
1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points. When dealing with images, the conversion from point to pixel depends on an image’s DPI (dots per
inch) property. For example, if an image’s DPI is 96 (96 pixels for each
inch), and it is 100 points high, its height in pixels is (100 / 72) *
96 = 133.3. The general formula is: pixels = ( points / 72 ) * DPI.

As per your scenario, you may skip the DPI part and get pixels converted to points using above stated formula.

I'm a little confused...

I have the pixels in the mouse coordinates....X:1341 & Y:294 which places the point in the upper right corner.

I thought the formula was points = pixels/72 * DPI but you have points & pixels switched around a little. I need the point value correct? If I use points=pixels/72 & drop the DPI that puts it at 18 & 4 which is way off. I know that the value needs to be around 300/690 for this particular entry.

Regards,

Steve.

Hi Steve,

In order to calculate the point position inside PDF document based on mouse coordinates, you my consider using following formula. Please note that pdfx is x coordinate inside PDF file and pixelX is mouse X coordinates over screen.

• pixelX = (pdfX/72)* DPI
• pdfX = pixelX * 72 / DPI

Where DPI is screen resolution and usually its 96 can be calculated programatically.

[C#]

float dpiX, dpiY;<o:p></o:p>

Graphics graphics = this.CreateGraphics();

dpiX = graphics.DpiX;

dpiY = graphics.DpiY;

Console.WriteLine("DPIx = " + dpiX + " DPIy " + dpiY);

Should you have any further query, please feel free to contact.

As you can see by my first post, I've tried your calculation. I am using MVC so I don't have the liberty of using Graphics g = this.CreateGraphics();....or I haven't been able to get it to work on a view.

In my example numbers and using your example...

X = 1341 * 72/96 = 1005.75 which is outside of the page.

Y= 294 * 72/96 = 2.31 which would put it at the bottom of the page.

X;1341, Y294 is a mouse point at the top right of the page.

As you can see, the points are way off. Am I the first to attempt this? I would have thought this would be fairly standard. Should I take a completely different approach? I do see that one of your competitors has this functionality build in;

sourcePoint = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pixel, PDFCoordinateType.Pdf, destPointPixels);

I was hoping for something similar or a workaround to this. I am trying to create a pdf from items that are added to a canvas which contains the original pdf. I could easily create an image from the canvas & just add the image to a pdf but from my experience the image becomes blurry or distorted when printed. Which is why I wanted to attempt to use the original pdf & add the stamps....but if I cannot place them in the exact location of the mouse coordinates then I will have a major issue.

Steve.

striano:

As you can see by my first post, I’ve tried your calculation. I am using MVC so I don’t have the liberty of using Graphics g = this.CreateGraphics();…or I haven’t been able to get it to work on a view.

In my example numbers and using your example…

X = 1341 * 72/96 = 1005.75 which is outside of the page.

Y= 294 * 72/96 = 2.31 which would put it at the bottom of the page.

X;1341, Y294 is a mouse point at the top right of the page.

As you can see, the points are way off. Am I the first to attempt this? I would have thought this would be fairly standard. Should I take a completely different approach? I do see that one of your competitors has this functionality build in;

sourcePoint = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pixel, PDFCoordinateType.Pdf, destPointPixels);

I was hoping for something similar or a workaround to this. I am trying to create a pdf from items that are added to a canvas which contains the original pdf. I could easily create an image from the canvas & just add the image to a pdf but from my experience the image becomes blurry or distorted when printed. Which is why I wanted to attempt to use the original pdf & add the stamps…but if I cannot place them in the exact location of the mouse coordinates then I will have a major issue.

Hi Steve,

Thanks for sharing the details.

I
have logged an investigation ticket in our issue tracking system as PDFNEWNET-36826. the development team will investigate this
issue in details and will keep you updated on the status of a correction. <o:p></o:p>

Any update on this? I have not heard anything in a week now.

Steve.

Hi Steve,

Thanks for your inquiry. I am afraid we have recently noticed the issue and investigation of issue is still pending due to other priority tasks. However, we have requested our development team to investigate it and share their finding at their earliest. We will notify you via this forum thread as soon as we made a significant progress towards issue resolution.

We are sorry for the inconvenience caused.

Best Regards,

Not sure if this issue got resolved. I have the same problem (3 years later

moe799:
Not sure if this issue got resolved. I have the same problem (3 years later :)
Hi Mohamed,

I have similar issue like striano.

Unable to open above shared link.
Error: The specified CGI application encountered an error and the server terminated the process.

Thanks for contacting support.

Regarding the earlier reported issue, an investigation ticket as PDFNET-36826 is still pending to be resolved.

Furthermore, you may also share complete information of the scenario and issue you are facing, by sharing sample PDF document and code snippet. We will test the scenario in our environment and address it accordingly.

ScenarioForAspose.zip (3.9 MB)

Thanks,

Thanks for sharing further information.

Please note that Aspose.PDF HTML5 Editor was just a demonstration and example of how you can use API features in order to display PDF files in HTML Canvas and perform some basic operations over them. As it was not product offered by the Aspose, we have discontinued it and are not maintaining it for further issues and updates.

Usually, the editor application used to convert PDF pages into images and display converted images in HTML Canvas for further operations (e.g. stamping, annotating, commenting, etc.). Since the coordinates were being calculated with respect to rendered image, stamp in output PDF document was expected to be appeared at different coordinates. Please note that this behavior of the application may also be document specific.

We would like to request you to please try specifying desired coordinates for image stamp directly inside PDF using Aspose.PDF API and in case you still face any issue with output PDF document, please let us know by sharing the values of coordinates. We will test the scenario in our environment and address it accordingly.

Following are the details of the stamp co-ordinates at document image using html canvas.

``````Ratio: 0.606060606060606
X-Axis: 368.7624750499
Y-Axis: 335.131782945737
Width:200
Height:60
``````

Following are the co-ordinates calculated while puting stamp on actual documents using following formula.

``````double shapeX = (shapes[shapeIndex].x * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 292.05988023952085
double shapeY = (shapes[shapeIndex].y * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 265.42437209302369
double shapeW = (shapes[shapeIndex].w * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 158.40000000000003
double shapeH = (shapes[shapeIndex].h * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 47.52000000000001

double yaxis = (doc.Pages[shapes[shapeIndex].p].Rect.URY - (shapeH + shapeY)); //479.05562790697627
``````

I used attached document.
86_ebrochure.pdf (1.3 MB)

Would you please confirm if you have tried adding stamp in PDF using Aspose.PDF for .NET Library in a separate program with same parameters.

In case stamp is not appearing at desired position even using Aspose.PDF for .NET separately, please share the sample image file which you are using for stamp. We will test the scenario in our environment and address it accordingly.

signature.png (656 Bytes)

Please do testing using shared document.

Following are the co-ordinates from HTML canvas

Ratio: 0.606060606060606
X-Axis: 368.7624750499
Y-Axis: 335.131782945737
Width:200
Height:60

And Actual converted co-ordinates while putting on document are

double shapeX = (shapes[shapeIndex].x * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 292.05988023952085

double shapeY = (shapes[shapeIndex].y * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 265.42437209302369

double shapeW = (shapes[shapeIndex].w * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 158.40000000000003

double shapeH = (shapes[shapeIndex].h * 72 / 150) / Convert.ToDouble(shapes[shapeIndex].ratio); // 47.52000000000001

double yaxis = (doc.Pages[shapes[shapeIndex].p].Rect.URY - (shapeH + shapeY)); // 479.05562790697627

I have attached both working and not working document,
Please refer these document while testing.
NotWorkingForThisDocument.pdf (1.3 MB)

WorkingForThisDocument.pdf (287.5 KB)

Thanks,

We have added an image stamp using above parameters and following code snippet in our environment. For your kind reference, an output PDF is also attached.

``````string sImageUrl = dataDir + "signature.png";
Document pdfDoc = new Document(dataDir + "86_ebrochure.pdf");
ImageStamp imageStamp = new ImageStamp(sImageUrl);
imageStamp.Height = 60;
imageStamp.Width = 200;
imageStamp.XIndent = 368.7624750499;
imageStamp.YIndent = 335.131782945737;
foreach (Page opage in pdfDoc.Pages)