简体   繁体   中英

Border in Digital Signature using itextsharp

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);`

When you sign a PDF using iText 5.5.x, you usually do something like

// Creating the reader and the stamper
PdfReader reader = new PdfReader(SRC);
FileStream os = new FileStream(DEST, FileMode.Create);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');

// Creating the appearance
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Reason = "Test customized appearance";
appearance.Location = "Singularity";
appearance.SetVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");

// Creating the signature
IExternalSignature pks = new PrivateKeySignature(parameters, DigestAlgorithms.SHA256);
MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, CryptoStandard.CADES);

You can freely customize the visual appearance by working on the layer templates after the appearance.SetVisibleSignature call, eg like this:

// trigger creation of default layers contents
appearance.GetAppearance();

// Customize the layer contents
PdfTemplate layer2 = appearance.GetLayer(2);
Rectangle rect = appearance.Rect;
layer2.SetRGBColorStroke(255, 0, 0);
layer2.SetLineWidth(2);
layer2.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);
layer2.Stroke();

to draw a red line along the signature appearance border

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM