Field Rect.LLY returns negative values on 8.5 x 14

Drawing a watermark over a field works just fine on 8.5 x 11, but doesn’t on any other paper size.

In the attached example change the file to Sample1.pdf to see it work, then Sample2.pdf to see the problem.

Stamp.zip (7.3 KB)

@alistairw
We are looking into it and will be sharing our feedback with you shortly.

@alistairw
In the Sample2 document, the Rect annotation has a value corresponding to LLY less than zero.
LLY.png (31.9 KB)

It seems that on legal size page, the lower left is not 0, 0?
Also, the co-ordinates returned from the field position cannot be used to draw the stamp due to the negative number? How should one convert that?

@alistairw

I don’t understand the question, could you please formulate it in more detail?

The most correct thing, of course, is to understand the reason for the formation of such values ​​in the pdf document. Or do you not have such an opportunity?

Basically I need to draw a rectangle over a form field. This works on 8.5x11 but does not on legal as the fields Rect position returns a negative value for LLY when lower on the page. How do I draw a rectangle over a field that will work on any page size?

I attempted to show this in the sample code, it can clearly be seen when switching between Sample1.pdf and Sample2.pdf in the code, line 15.

@alistairw
This has nothing to do with page size, the other document (originally the document itself) simply has an incorrect LLY value.
I’ll think about it and maybe I can advise you something.

Definitely a defect in the fields Rect value, managed to get it working on any paper size and orientation, here is the code that calculates the offset:

double xOffset = (pdfDocument.Pages[f.PageIndex].Rect.Width - 8.5 * 72.0) / 2;
double yOffset = (pdfDocument.Pages[f.PageIndex].Rect.Height - 11 * 72.0) / 2;

Full working example, needs a PDF with field named WATERMARK_AMENDMENT and draws a diagonal watermark over the field.

using System;
using System.Diagnostics;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Facades;
using Aspose.Pdf.Forms;
using Aspose.Pdf.Text;

namespace Stamp
{
   internal class Program
   {
      static void Main(string[] args)
      {
         const string exportedPdf = "Sample1_out.pdf";
         if (File.Exists(exportedPdf))
            File.Delete(exportedPdf);
         
         // Open document
         Document pdfDocument = new Document("Sample2.pdf");

         // Get a field
         Field f = pdfDocument.Form["WATERMARK_AMENDMENT"] as Field;
         //f.Value = $"{f.Rect.LLX}, {f.Rect.LLY}, {f.Width}, {f.Height}";

         double xOffset = (pdfDocument.Pages[f.PageIndex].Rect.Width - 8.5 * 72.0) / 2;
         double yOffset = (pdfDocument.Pages[f.PageIndex].Rect.Height - 11 * 72.0) / 2;

         string watermark = "This is a sample text message.";

         FormattedText text = new FormattedText(watermark);
         TextStamp stamp = new TextStamp(text)
         {
            Opacity = 0.8,
            XIndent = f.Rect.LLX + xOffset,
            YIndent = f.Rect.LLY + yOffset,
            Width = f.Rect.Width,
            Height = f.Rect.Height,
            Scale = false,
            Background = false,
            TextState =
            {
               Font = FontRepository.FindFont("Helvetica", FontStyles.Bold, true),
               FontSize = 20,
               ForegroundColor = Color.Red
            },
         };

         // auto size font if it's too large to fit
         var wd = stamp.TextState.MeasureString(watermark);
         while (wd > stamp.Width)
         {
            wd = stamp.TextState.MeasureString(watermark);
            stamp.TextState.FontSize--;
         }

         double h = stamp.Height;
         double w = stamp.Width;
         double angle = Math.Atan2(h, w);
         double stringWidth = stamp.TextState.MeasureString(watermark);
         double diagonalLength = (float)Math.Sqrt(w * w + h * h);
         double x = (diagonalLength - stringWidth) / 2;
         //double y = (stamp.TextState.FontSize * 3) / 4; // Aspose has not way to measure string height

         float dx = (float)(Math.Cos(angle) * x);
         float dy = (float)(Math.Sin(angle) * x);

         stamp.RotateAngle = angle * (180.0 / Math.PI);
         stamp.XIndent += dx;
         stamp.YIndent += dy - stamp.TextState.FontSize / 2;

         pdfDocument.Pages[1].AddStamp(stamp);

         // Save updated document
         pdfDocument.Save(exportedPdf);

         Process.Start(exportedPdf);
      }
   }
}

@alistairw
Thank you for writing us your solution.
I was thinking about using Page.MediaRect to control the validity of the rectangle values. But I couldn’t figure out how to not only control it, but also fix it.