iTextSharp 5.5.8使用现有签名字段对pdf进行数字签名

时间:2016-03-02 02:31:24

标签: c# asp.net pdf itextsharp

所以我需要通过webform从用户的输入中签名PDF,我在这里的iTextSharp教程中遇到了SignEmptyField示例:

http://gitlab.itextsupport.com/itextsharp/tutorial/raw/master/signatures/chapter2/C2_03_SignEmptyField/C2_03_SignEmptyField.cs

我稍微修改了以下内容。

public class C2_03_SignEmptyField
    {       
        public char[] PASSWORD = "test".ToCharArray();
        public String SRC = ConfigurationManager.AppSettings["pdfSource"];
        public String DEST = ConfigurationManager.AppSettings["pdfDest"];

        public void Sign(String src, String name, String dest, ICollection<X509Certificate> chain, ICipherParameters pk,
                         String digestAlgorithm, CryptoStandard subfilter,
                         String reason, String location)
        {
            // Creating the reader and the stamper
            PdfReader reader = new PdfReader(src);
            PdfReader.unethicalreading = true;
            AcroFields fields = reader.AcroFields;
            FileStream os = new FileStream(dest, FileMode.Create);
            PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
            // Creating the appearance
            PdfSignatureAppearance appearance = stamper.SignatureAppearance;
            appearance.Reason = reason;
            appearance.Location = location;   
            appearance.Layer2Text = "Ben Ziegler"; // SignatureTextBox.Text;
            appearance.Layer2Font = new Font(Font.FontFamily.COURIER, 7.0f, Font.NORMAL, BaseColor.LIGHT_GRAY);
            appearance.Acro6Layers = true;
            appearance.SetVisibleSignature(name);

            // Creating the signature
            IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);
            //ExternalDigest digest = new BouncyCastleDigest();
            MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter);
        }

        public void Main()
        {
            Pkcs12Store store = new Pkcs12Store(new FileStream(@"D:\test.pfx", FileMode.Open), PASSWORD);
            String alias = "";
            ICollection<X509Certificate> chain = new List<X509Certificate>();
            // searching for private key

            foreach (string al in store.Aliases)
            {
                if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
                {
                    alias = al;
                    break;
                }
            }

            AsymmetricKeyEntry pk = store.GetKey(alias);
            foreach (X509CertificateEntry c in store.GetCertificateChain(alias))
            {
                chain.Add(c.Certificate);
            }

            RsaPrivateCrtKeyParameters parameters = pk.Key as RsaPrivateCrtKeyParameters;
            C2_03_SignEmptyField app = new C2_03_SignEmptyField();
            app.Sign(SRC, "Digital Signature", String.Format(DEST, 1), chain, parameters, DigestAlgorithms.SHA256,
                     CryptoStandard.CMS, "Test 1", "Ghent");
        }
    }

我添加了appearance.Layer2Text和appearance.Layer2Font的东西,好像来自文本框。它没有错误,但签名也没有显示在签名字段中,因为我没有收到任何错误,也没有显示,所以不知道从哪里开始。

是否有不同的方式来签署空的现有签名字段?

0 个答案:

没有答案