Form field co-ordinates incorrect on non 8.5x11 paper size

When drawing a stamp at 0, 0 it’s at the bottom left of the page, as expected on any paper size.

When drawing a field at 0, 0 it’s up off the bottom of the page on 8.5x13 paper.

I need to draw a stamp over a field on 8.5x13 paper, it only works on 8.5x11 paper.

In the example code, change the filename from Sample2.pdf to Sample1.pdf to compare.

Stamp2.zip (7.2 KB)

@alistairw

We are checking it and will get back to you shortly.

Seems to be a defect in the way the fields returns it’s Rect.

Using the following code to calculate an offset got it working on any paper size and both landscape and portrait orientation. Note that it does no adjustment for 8.5x11.

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;

@alistairw

Looks like the margins of page are also being considered while adding the form fields. The text stamp positions itself without taking margins into consideration. Therefore, please try adding margin values in the calculations and let us know if that resolves the issues.

I tried the suggestion, however the margins are the same for all these different paper sizes so it does not address the defect.

@alistairw

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-57675

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@alistairw

There is nothing wrong with positioning the field. The problem arises because in the document with 8.5x14 in. page, the page coordinates begin not with (0, 0), but rather with (0, -108) - the lowest Y-coordinate is negative. You can check it by looking at the page MediaBox rectangle.

The stamp is positioned using relative XOffset and YOffset values - that’s why it’s always in the corner of the page. While the field is positioned using the absolute coordinates, and it so happen that in this particular document the 0 Y-coordinate is way above the bottom of the page.

The solution is to calculate the field rectangle relative to the coordinates of the page lower left corner:

string exportedPdf = $"Sample1_out{DateTime.Now.Ticks}.pdf";
if (File.Exists(exportedPdf))
    File.Delete(exportedPdf);

// Open document
Document pdfDocument = new Document("Sample2.pdf");

// Get page content rectangle
Page p = pdfDocument.Pages[1];
Rectangle pRect = p.Rect;

// Get a field
Field f = pdfDocument.Form["WATERMARK_AMENDMENT"] as Field;
// Calculate the field rectangle relative to the page's lower left corner
f.Rect = new Rectangle(pRect.LLX, pRect.LLY, pRect.LLX + 504, pRect.LLY + 144);
f.Value = $"{f.Rect.LLX}, {f.Rect.LLY}, {f.Width}, {f.Height}";


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

FormattedText text = new FormattedText(watermark);
TextStamp stamp = new TextStamp(text)
{
    Opacity = 0.8,
    XIndent = 0,
    YIndent = 0,
    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
    },
};


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

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

Process.Start(exportedPdf);

57675_out.pdf (5.6 KB)