Help needed with coordinate conversion for PDF annotation placement

I have attached the pdf file and code that i am using to place stamp, so i get blank space coordinate in system.drawing.rect that i convert them using below code, it works for most of the files but not working for one i attached. stamp needs to be movable that is why i am not using imagestamp which makes it flatten.

foreach (var pageKv in pages.OrderBy(x => x.Key))
{
int pageIndex = pageKv.Key;
var info = pageKv.Value;

   Aspose.Pdf.Page newPage;
   lock (_asposeLock)
   {
       newPage = pdfDocument.Pages[pageIndex];
   }

   await SendSignalRUpdate(projectId, fileId, Guid.Empty, $"Stamping page {pageIndex} of {totalPages}");
   if (!info.BlankSpace.IsEmpty)
   {
       int bmpWidth = info.bitmapWidth;
       int bmpHeight = info.bitmapHeight;

       double scaleX = bmpWidth / newPage.MediaBox.Width;
       double scaleY = bmpHeight / newPage.MediaBox.Height;

       // create rotated stamp bytes stream for Aspose and compute rectangle
       MemoryStream stampStreamForAspose = null;
       Aspose.Pdf.Rectangle rect;
       // create rotated bytes and compute rect inside a short lock to ensure page properties consistent
       lock (_asposeLock)
       {
           using var rotatedTemp = RotateImageBytes(stampBytes, info.Rotation);
           stampStreamForAspose = new MemoryStream();
           rotatedTemp.CopyTo(stampStreamForAspose);
           stampStreamForAspose.Position = 0;

           double x = info.BlankSpace.X / scaleX;
           double y = info.BlankSpace.Y / scaleY;
           double w = info.BlankSpace.Width / scaleX;
           double h = info.BlankSpace.Height / scaleY;
           double left = x;
           double bottom = newPage.MediaBox.Height - y - h;
           rect = new Aspose.Pdf.Rectangle(left, bottom, left + w, bottom + h);

           var stampAnnot = new Aspose.Pdf.Annotations.StampAnnotation(newPage, rect)
           {
               Modified = DateTime.Now,
               Image = stampStreamForAspose
           };
           newPage.Annotations.Add(stampAnnot);

           // restore rotation explicitly on the page
           newPage.Rotate = info.Rotation;
       }

       streamsForAspose.Add(stampStreamForAspose);
   }

test-document.pdf (2.3 MB)

@priyanshut2

To assist you with coordinate conversion for PDF annotation placement, it’s crucial to ensure that the scaling factors are accurately calculated based on the dimensions of the PDF page and the bitmap you are using for the stamp.

From your code, it appears you are calculating the scale factors (scaleX and scaleY) based on the bitmap dimensions and the PDF page’s media box. If the stamp placement is not functioning correctly for a specific PDF, it may be due to the dimensions of the MediaBox or the method used for calculating the coordinates.

Here’s a refined approach to ensure the coordinates are correctly calculated:

  1. Check MediaBox Dimensions: Verify that the MediaBox dimensions are valid and not zero or negative, as this can occur with certain PDFs.

  2. Coordinate Calculation: When calculating the coordinates for the stamp, ensure that you are using the correct formula. The y coordinate should be adjusted based on the height of the page.

Here’s a modified snippet of your code for clarity:

double scaleX = bmpWidth / newPage.MediaBox.Width;
double scaleY = bmpHeight / newPage.MediaBox.Height;

double x = info.BlankSpace.X / scaleX;
double y = info.BlankSpace.Y / scaleY;
double w = info.BlankSpace.Width / scaleX;
double h = info.BlankSpace.Height / scaleY;

// Adjust y coordinate based on the MediaBox height
double bottom = newPage.MediaBox.Height - y - h;
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(x, bottom, x + w, bottom + h);
  1. Debugging: If the issue persists, consider logging the values of scaleX, scaleY, and the calculated coordinates to verify they are as expected.

If you continue to encounter issues with a specific PDF, it may be beneficial to analyze the PDF structure or test with a different PDF to isolate the problem.

Feel free to reach out if you have more questions!

if i use the IMAGESTAMP it works with the same coordinates, the only issue with that is it makes stamp flatten.

The stamp issue is fixed, PDF have two main box crop box and media box, media box is the physical size of page and crop box is the visible size that we see on viewer. Earlier I was using mediabox. Now using cropbox will make sure that we will place stamp in visible area only,

var pageBox = newPage.CropBox ?? newPage.MediaBox;

@priyanshut2

It is nice to know that your issue has been resolved. Please keep using the API and feel free to create a new topic in case you face any kind of issues.