Hi there I´m new to iTextSharp and I´m testing the examples in the site. (The 
code can be finded here 
http://itextpdf.sourceforge.net/howtosign.html#signextitextsharp2).  Right now 
I'm trying to sign a PDF with a Smart Card certificate. I used this code that 
it's available on line, as a sample code but I can't make it work. 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:\\hello.pdf");
    PdfStamper stp = PdfStamper.CreateSignature(reader, new 
FileStream("c:\\hello_hashed.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;
    Dictionary<PdfName,int> exc = new Dictionary<PdfName,int>();
    exc[PdfName.CONTENTS] = csize * 2 + 2;
    sap.PreClose(exc); //this is the line where the code fails

    HashAlgorithm sha = new SHA1CryptoServiceProvider();
    
    Stream s = sap.GetRangeStream();
    int read = 0;
    byte[] buff = new byte[8192];
    while ((read = s.Read(buff, 0, 8192)) > 0) {
        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);
}The error is thisSystem.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=itextsharp
  StackTrace:
       at 
iTextSharp.text.pdf.security.CertificateInfo.GetSubjectFields(X509Certificate 
cert)
       at iTextSharp.text.pdf.PdfSignatureAppearance.GetAppearance()
       at iTextSharp.text.pdf.PdfSignatureAppearance.PreClose(Dictionary`2 
exclusionSizes)
       at FirmaDocs.Form1.SignHashed() in H:\...\Form1.cs:line 217
       at FirmaDocs.Form1.button1_Click(Object sender, EventArgs e) in 
H:\...\Form1.cs:line 186
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons 
button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, 
Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at 
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr
 dwComponentID, Int32 reason, Int32 pvLoopData)
       at 
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 
reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 
reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at FirmaDocs.Program.Main() in H:\...\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] 
args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence 
assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext 
executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext 
executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
 Can anyone please help me, this error is driving me crazy.
JMC                                       
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to