使用itextsharp在数字签名中的边框

时间:2018-02-20 06:25:15

标签: c# pdf itext digital-signature

我正在使用Itextsharp为pdf添加数字签名,这是完美添加的。但现在我需要在一个盒子里添加数字签名。

我尝试了下面的事情: - 1)在矩形中创建了一个textform字段,并尝试用数字签名替换表单字段,但没有发生,因为它不能将其识别为签名字段。 (IDK实际意味着什么)

2)当我在adobe中打开pdf时,我可以看到悬停在签名上的矩形框闪烁。 无需验证。

但我需要在框中签名。

@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();

目前使用5.5.13。

此外,在使用SSL证书的服务器上托管它时,我得到一个错误,DER长度超过4个字节。

堆栈跟踪相同。

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

再次编辑代码共享,请帮我查一下是否有错误。

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

1 个答案:

答案 0 :(得分:1)

使用iText 5.5.x签署PDF时,通常会执行类似

的操作
// 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);

您可以在appearance.SetVisibleSignature电话结束后通过处理图层模板自由地自定义视觉外观,例如像这样:

// 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();

沿着签名外观边框绘制一条红线