euaccount Posted June 14, 2011 at 02:47 PM Report #396026 Posted June 14, 2011 at 02:47 PM Boas estou a desenvolver uma pagina web asp.net c# para assinar documentos pdf tendo como source um smartcard para assinar os documentos estou a usar o itextsharp http://itextpdf.sourceforge.net/howtosign.html#signextitextsharp2 Portanto dessa pagina estou a usar o SignHashed() problema Erros: //sap.PreClose(exc); The best overloaded method match for 'iTextSharp.text.pdf.PdfSignatureAppearance.PreClose(System.Collections.Generic.Dictionary<iTextSharp.text.pdf.PdfName,int>)' has some invalid arguments pelo o que comentei essa linha umas linhas mais tarde Erro while ((read = s.Read(buff, 0, 8192)) > 0) {"Object reference not set to an instance of an object."} Aqui esta o codigo para assinar o pdf public static void SignHashed() { X509Certificate2 card = GetCertificate(); Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(card.RawData) }; PdfReader reader = new PdfReader(@"C:\_Roberto\_PDF\temp\CC.pdf"); PdfStamper stp = PdfStamper.CreateSignature(reader, new FileStream(@"C:\_Roberto\_PDF\temp\temp.pdf", FileMode.Create), '\0'); PdfSignatureAppearance sap = stp.SignatureAppearance; sap.SetVisibleSignature(new Rectangle(100, 100, 300, 200), 1, null); sap.SignDate = DateTime.Now; sap.SetCrypto(null, chain, null, null); sap.Reason = "I like to sign"; sap.Location = "Universe"; sap.Acro6Layers = true; sap.Render = PdfSignatureAppearance.SignatureRender.NameAndDescription; PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1); dic.Date = new PdfDate(sap.SignDate); dic.Name = PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN"); if (sap.Reason != null) dic.Reason = sap.Reason; if (sap.Location != null) dic.Location = sap.Location; sap.CryptoDictionary = dic; int csize = 4000; Hashtable exc = new Hashtable(); exc[PdfName.CONTENTS] = csize * 2 + 2; [i] //sap.PreClose(exc);[/i] HashAlgorithm sha = new SHA1CryptoServiceProvider(); Stream s = sap.RangeStream; int read = 0; byte[] buff = new byte[8192]; [i] while ((read = s.Read(buff, 0, 8192)) > 0)[/i] { sha.TransformBlock(buff, 0, read, buff, 0); } sha.TransformFinalBlock(buff, 0, 0); byte[] pk = SignMsg(sha.Hash, card, false); byte[] outc = new byte[csize]; PdfDictionary dic2 = new PdfDictionary(); Array.Copy(pk, 0, outc, 0, pk.Length); dic2.Put(PdfName.CONTENTS, new PdfString(outc).SetHexWriting(true)); sap.Close(dic2); } Ideias? Sugestões? Secalhar vou é ler o smartcard para um txt e fazer um read para inserir os dados mais tarde no código assim evito isto... mas por agora vou ser preguiçoso. Atenciosamente Eu.
Lozy Posted July 15, 2011 at 05:40 PM Report #402767 Posted July 15, 2011 at 05:40 PM amigo, tive o mesmo problema e resolvi da seguinte forma... o parametro passado para o metodo PreClose não é um [glow=red,2,300]Hashtable[/glow] e sim um [glow=red,2,300]Dictionary<PdfName, int>[/glow] portanto substitua esta parte do codigo: Hashtable exc = new Hashtable(); exc[PdfName.CONTENTS] = csize * 2 + 2; //sap.PreClose(exc); por este Dictionary<PdfName, int> dic_ex = new Dictionary<PdfName, int>(); dic_ex.Add(PdfName.CONTENTS, csize * 2 + 2); sap.PreClose(dic_ex); abraços
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now