【问题标题】:PDF digital signing: customize appearance by using iTextSharpPDF 数字签名:使用 iTextSharp 自定义外观
【发布时间】:2016-08-16 03:23:51
【问题描述】:

我正在尝试使用 iTextSharp 5.5.9 签署 pdf 文件。自定义外观时,我收到一个无法理解的错误。这是我所做的

签名方法:

public void Sign1(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);
            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.SetVisibleSignature(name);
            // Custom text and custom font
            appearance.Layer2Text = "This document was signed by ABC";
            appearance.Layer2Font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN);
            // Creating the signature
            IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);
            MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter);
        }

我的签名方式

String KEYSTORE = "C:/Users/user/Desktop/temp.pfx";
char[] PASSWORD = "blabla".ToCharArray();
String SRC = "C:/Users/user/abc.pdf";
String DEST = "C:/Users/user/Desktop/aaa.pdf";
 Pkcs12Store store = new Pkcs12Store(new FileStream(KEYSTORE, 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;
Sign1(SRC, "Signature1", String.Format(DEST, 1), chain, parameters, DigestAlgorithms.SHA256, CryptoStandard.CMS, "Customize apprearance", "Blabla");

我收到的消息是:

如何解决?

【问题讨论】:

    标签: c# itext digital-signature


    【解决方案1】:

    一个无法理解的错误

    相反,它清楚地表明某些参数值不合适,并且附加信息甚至指出了哪个参数值和原因:

    字段 Signature1 不存在。

    “Signature1”是您用作签名字段名称的值,您的Sign1 方法在这里使用它:

    appearance.SetVisibleSignature(name);
    

    您调用的方法记录在如下来源中:

    /**
     * Sets the signature to be visible. An empty signature field with the same name must already exist.
     * @param fieldName the existing empty signature field name
     */
    virtual public void SetVisibleSignature(String fieldName)
    

    但您的 PDF 似乎还没有同名的空签名字段,这会触发您观察到的异常。

    如果您想对 PDF 进行签名(使用签名的文档内可视化),但不能使用现有的空签名字段,则必须使用该方法的不同重载,向该方法提供创建签名所需的信息现场可视化:

    /**
     * Sets the signature to be visible. It creates a new visible signature field.
     * @param pageRect the position and dimension of the field in the page
     * @param page the page to place the field. The fist page is 1
     * @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name
     */
    virtual public void SetVisibleSignature(Rectangle pageRect, int page, String fieldName)
    

    【讨论】:

    • 感谢 mkl。我必须在签名前创建空白字段。
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多