Python Api position discrepancies

Product:
PDFNetPython3
Product Version:

Please give a brief summary of your issue:

  1. Is there a standard way of setting RECT position for form field widgets? The Rect property in Python/Java has a format Rect(x1, y1, x2, y2) where x1, y1 are lower left coordinates and x2,y2 are upper right coordinates. I was expecting it to be like Rect(x, y, width, height) where x,y are top left coordinates. Is it not possible this way?

  2. Similarly, when I use Stamp annotation, the Stamper class has Position property with top left coordinates which does not seem to be in sync with Rect property arguments. If I have to place Stamp annotation over the Signature field via Python api, how to place it in the right position?

  3. Is there a Python3 api documentation similar to this. I can’t find it. Package Index - PDFTron PDFNet SDK API Reference | PDFTron Systems Inc.

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

Please provide a link to a minimal sample where the issue is reproducible:

1 No, PDF format stores Rects as any two points x1,y1,x2,y2, and this is what our API follows.

2 The Stamper class does not provide 100% control over the size+position of the output. It is for different use case.

If I have to place Stamp annotation over the Signature field

You would just use the Signature fields Widget annotation Rect to know the exact position of the field on the page, and use that Rect with either Annotation subclass, or ElementWriter API.

Why is using Stamper over top of a Field important for you?
Why not edit the field appearance itself?

3 No, the Python API is generated from the C++ API
https://www.pdftron.com/api/PDFTronSDK/cpp/index.html

Hi Ryan,

  1. You mean RECT coordinates x1,y1 can be top-left or bottom left and x2,y2 can be top-right or bottom right? I can use it any which way?

  2. My use case is I am placing a Signature widget on the document and want to add a signature (base64 or image) using backend apis over it. For that I tried using Stamper class and it works for me, just that the correct position has to be known. Can you suggest if Stamper is the right choice for this or I should use ElementWriter?

Why not edit the field appearance itself?

Can I edit the Signature widget to add its value? So I should use SetValue method to add ElementWriter object there?

Ryan,

I tried using Stamper to add signature over the SignatureWidget as below:

    fld = doc.GetField('signature0')
    signature_widget = Widget(fld.GetSDFObj())
    bbox = signature_widget.GetRect()
    page = signature_widget.GetPage()
    
    page_h = page.GetPageHeight()

    s = Stamper(Stamper.e_relative_scale, 0.1, 0.1)
    s.SetOpacity(1)
    s.SetAsBackground(False)
    s.SetAsAnnotation(True)
    s.SetAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_top)
    s.SetTextAlignment(Stamper.e_align_center)
    s.SetPosition(bbox.GetX1(), page_h - bbox.GetY1() - (bbox.GetY2() - bbox.GetY1()))
    img = Image.Create(doc.GetSDFDoc(), 'stamp.png')
    ps = PageSet(page.GetIndex())
    s.StampImage(doc, img, ps)
    doc.Save(output.pdf, SDFDoc.e_linearized)

Can you tell me if this is the right approach if I have to add signature from backend apis?

want to add a signature (base64 or image) using backend apis over it.

If you have an image than you should be able to load it using one of the Image.Create APIs

https://www.pdftron.com/api/PDFTronSDK/cpp/classpdftron_1_1_p_d_f_1_1_image.html#ac98b19bbffd54c4870a9df1e47697568

Then you can pass the Image to SignatureWidget.CreateSignatureAppearance

https://www.pdftron.com/api/PDFTronSDK/cpp/classpdftron_1_1_p_d_f_1_1_annots_1_1_signature_widget.html#a6d172d65bc63efed3db6b9d22969e98d

Otherwise, if you want to have more control, such as text, you can follow this forum post, and SignatureWidget.SetAppearance API

When I use CreateSignatureAppearance() method to add an image and save it onto an output pdf doc, I dont see the signature image in the pdf under Acrobat reader. However, if I render the same doc in PDFTron Viewer, it shows up. Similarly when I flattened the doc, then also it shows up. Any idea why the image dont show up in the output doc in Acrobat?

Also, when I use SignatureAppearance to update the signature on the widget using backend apis, it creates appearances element under the Widget in XFDF. But if I add a signature from docviewer onto the same document, it creates a Stamp annotation for each Signature widget. Why both the flows are different?

Why both the flows are different?

I am not sure why WebViewer does that. Placing one annotation over top of another is not something I would recommend doing.

However, I believe you might run into some issues with some PDF viewers if a Digital Signature Field only has an appearance, and not an actual Cryptographic signature. I believe Adobe might not show the appearance at all. So you might want to flatten the fields/annotations so the appearance is baked into the PDF and will show everywhere.

Yes that is how WebViewer is behaving. Adding signature over the widget is actually creating Stamp annotation. I thought iofdoing the same from backend to keep the behavior consistent. Using Stamper I could achieve it. If I use widget appearance to add signature that has issues with Adobe might create problems as we want to store the PDF with signature (without flattening) for troubleshooting.