I am adding a digital signature to a pdf using Itextsharp, which is being added perfectly. But now I need to add that digital signature in a box.
I tried the below things:- 1) created a textform field in a rectangle and tried to replace the form field with a digital signature, but did not happen as it does not recognise it as a signature field. (IDK what it actually means)
2) When I open the pdf in adobe I can see a rectangular box flicker on hover of the signature. No verification needed.
But i need the signature in the box.
My code after adding the changes from @mkl's answer:
PdfReader reader1 = new PdfReader(this.inputPDF);
PdfStamper st = PdfStamper.CreateSignature(reader1, new FileStream(this.outputPDF, FileMode.Create, FileAccess.ReadWrite), '\0');
AcroFields pdfFormFields = st.AcroFields;
st.SetEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowPrinting);
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SignDate = Convert.ToDateTime(DateTime.Now.ToLocalTime()); 
sap.Layer2Font = new Font(Font.HELVETICA, 12, Font.BOLD);
sap.Layer2Text = "Digitally Signed \nVICE PRESIDENT- \nDate:" + ; //+ "\nLocation: BENGALURU";
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);
sap.Contact = SigContact;
sap.Location = SigLocation;
Rectangle obj = new iTextSharp.text.Rectangle(75, 90, 600, 300);
sap.SetVisibleSignature(obj, 1, "sig");
sap.GetAppearance();
PdfTemplate layer20 = sap.GetLayer(2);
Rectangle rectangle = sap.Rect;
layer20.SetRGBColorStroke(0, 0, 0);
layer20.SetLineWidth(5);
layer20.Rectangle(00, 80, 250, 100);
layer20.Stroke();
st.close();
Currently using 5.5.13.
Moreover, I get an error DER length is more than 4 bytes while hosting it on server with SSL certificate.
Stack trace for the same.
`System.IO.IOException was caught
  HResult=-2146232800
  Message=DER length more than 4 bytes: 32
  Source=itextsharp
  StackTrace:
       at Org.BouncyCastle.Asn1.Asn1InputStream.ReadLength(Stream s, Int32 limit)
       at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
       at Org.BouncyCastle.Asn1.Asn1InputStream.BuildEncodableVector()
       at Org.BouncyCastle.Asn1.Asn1InputStream.BuildDerEncodableVector(DefiniteLengthInputStream dIn)
       at Org.BouncyCastle.Asn1.Asn1InputStream.CreateDerSequence(DefiniteLengthInputStream dIn)
       at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag, Int32 tagNo, Int32 length)
       at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
       at Org.BouncyCastle.Asn1.Asn1InputStream.BuildEncodableVector()
       at Org.BouncyCastle.Asn1.Asn1InputStream.BuildDerEncodableVector(DefiniteLengthInputStream dIn)
       at Org.BouncyCastle.Asn1.Asn1InputStream.CreateDerSequence(DefiniteLengthInputStream dIn)
       at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag, Int32 tagNo, Int32 length)
       at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
       at Org.BouncyCastle.Asn1.Asn1Object.FromStream(Stream inStr)
       at Org.BouncyCastle.Pkcs.Pkcs12Store.Load(Stream input, Char[] password)
       at Org.BouncyCastle.Pkcs.Pkcs12Store..ctor(Stream input, Char[] password)
       at Letters.Models.PDFSigner.Sign(String SigReason, String SigContact, String SigLocation, Boolean visible, String strType, String path, String password) in :line 93`
Edited code sharing again, Please help me find if there is some error in it.
Stream path1 = new FileStream(path, FileMode.Open, FileAccess.Read);
      Pkcs12Store pk12 = new Pkcs12Store(path1, password.ToCharArray());
        path1.Dispose();
        //then Iterate throught certificate entries to find the private key entry
        string alias = null;
        foreach (string tAlias in pk12.Aliases)
        {
            if (pk12.IsKeyEntry(tAlias))
            {
                alias = tAlias;
                break;
            }
        }
        var pk = pk12.GetKey(alias).Key;
        PdfReader reader = new PdfReader(this.inputPDF);
        FileStream os = new FileStream(this.outputPDF, FileMode.Create);
        PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
        PdfSignatureAppearance appearance = stamper.SignatureAppearance;
        appearance.Layer2Font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);`