PDF signature image not displayed correctly

Hi,

when I create a new PDF signature and apply an image to it, the image is displayed incorrectly. The reason being, that the Rectangle used by the signing method erroneously uses Rectangle’s “Y” for “Width”.

For example:

PdfFileSignature sig = new PdfFileSignature();
// Code omitted…
DocMDPSignature d = new DocMDPSignature(signature, 0);
sig.certify(1, “Electronic signature”, null, null, true, new Rectangle(10, 35, 100, 25), d);

This should display the image at position (10, 35) with width = 100 and height = 25. Instead it displays the image at position (10, 35) with width = 35 and height = 25.

If you try this:

PdfFileSignature sig = new PdfFileSignature();
// Code omitted…
DocMDPSignature d = new DocMDPSignature(signature, 0);
sig.certify(1, “Electronic signature”, null, null, true, new Rectangle(10, 100, 1, 25), d);

This one displays the image at position (10, 100) with width = 100 and height = 25. So rectangle’s real width parameter is not used at all.

Please fix this.

Hi Tuukka,

Thanks for contacting support.

I have tested the scenario in our environment with sample PDF document and I was unable to notice the issue. Please check following code snippet which I have tried with Aspose.Pdf for .NET 17.4.0, to test the scenario. I have also attached an output, generated by below code, for your reference.

using (Document document = new Document(dataDir + “SignatureField_out.pdf”))

{

    string signatureFieldName = "sign1";

    using (Facades.PdfFileSignature signature = new Facades.PdfFileSignature(document))

    {

        PKCS7 pkcs = new PKCS7(dataDir + "mykey2.pfx", "aa");

        DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);

        System.Drawing.Rectangle rect = new System.Drawing.Rectangle((int)document.Form[signatureFieldName].GetRectangle(false).LLX, (int)document.Form[signatureFieldName].GetRectangle(false).LLY, (int)document.Form[signatureFieldName].GetRectangle(false).Width, (int)document.Form[signatureFieldName].GetRectangle(false).Height);

        signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";

        signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);

        signature.Save(dataDir + "SignatureField_out.pdf");

    }

}

Please try above code snippet to get correct output and in case if you still face further issue, please share your sample input file(s) along with code snippet so that we can try to replicate the issue in our environment and address it accordingly.

Best Regards,

Hello, the problem still persists. I am using Aspose PDF for Java 16.10.

The source code is as follows:

File file = new File(“test.pdf”);
File imagefile = new File(“image.png”);
File outfile = new File(“outtest.pdf”);
com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(new FileInputStream(file));
PdfFileSignature sig = new PdfFileSignature();
PKCS7 signature = new PKCS7(“cert.p12”, “password”);
signature.setImage(new FileInputStream(imagefile));
DocMDPSignature d = new DocMDPSignature(signature, 0);
sig.certify(1, “Electronic signature”, null, null, true, new Rectangle(10, 35, 100, 25), d);
sig.save(outfile.getAbsolutePath());

Hi Tuukka,

Thanks for writing back and adding more details to the scenario. I have again tested the scenario with the code snippet which you have shared, by using Aspose.Pdf for Java 17.4 and was unable to notice issue. Please check in the following code snippet where I have define the Rectangle object by specifying parameters’ values according to signature field boundaries/rectangle.

try

{

    File file = new File(dataDir + "SignatureField_out.pdf");

    File imagefile = new File(dataDir + "aspose.jpg");

    File outfile = new File(dataDir + "outtest.pdf");

    com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(new FileInputStream(file));

    Rectangle rect = pdf.getForm().get_Item("sign1").getRectangle(false);

    com.aspose.pdf.facades.PdfFileSignature sig = new com.aspose.pdf.facades.PdfFileSignature(pdf);

    PKCS7 signature = new PKCS7(dataDir + "mykey2.pfx", "aa");

    signature.setImage(new FileInputStream(imagefile));

    DocMDPSignature d = new DocMDPSignature(signature, 0);

    sig.certify(1, "Electronic signature", null, null, true, new java.awt.Rectangle((int)rect.getLLX(), (int)rect.getLLY(), (int)rect.getWidth(), (int)rect.getHeight()), d);

    sig.save(outfile.getAbsolutePath());

} catch (FileNotFoundException e)
{

    // TODO Auto-generated catch block

    e.printStackTrace();

}

Please try using latest version of the API as it is always recommended to use latest version and in case if you still face any issue, as requested earlier, please share your input document. This will really help us in observing the issue in our environment and share our findings appropriately.

PS: I have attached output as well, generated by shared code, for your reference.

Hi Asad,
Same issue, I am facing.
In your code, please explain this line, why are you getting sign1 rectangle “sign1” ?
and then you are getting x,y, width, height of this rectangle.
I have coordinates and width and height of image, if i will add hard coded here, is there any issue?

sig.certify(1, “Electronic signature”, null , null , true , new java.awt.Rectangle(30,50,100,200), d);

Waiting for your reply.

@mukhan12

Thank you for contacting support.

Above code snippet is using a source document which contains a signature field with name sign1, therefore the rectangular coordinates for that specific field are used above and there would not be any issue if you want to place signature at any location of the page. Moreover, you may also visit Add Digital Signature to PDF file for your kind reference.

Thanks Farhan,

This issue is solved in latest Aspose.PDF for Java 19.8 .
I updated it, now its working fine.

@mukhan12

We are glad to know it is working fine now. Please feel free to contact us if you need any further assistance.