title: Digitally sign multiple PDF files with an E-ID in a specified folder
body: <p>Note: I have heavily edited most of my post as I have advanced a bit further now</p>
  
  <p>I am currently working on a small project: the general idea is that the user selects a folder, inserts his E-ID and all PDF files in that folder are modified with his digital signature and an image to represent this when printed.
  I am currently using the iTextSharp framework to accomplish this. But because I have to convert the source to VB.NET, I have hit a full stop.</p>
  
  <p>The following code accomplishes its task of adding a digital signature to a PDF document, however. It only does so with the test-certificate I have created with Visual Studio. Anything else and the PDF just isn't created, I have checked with breakpoints and myPkcs12Store does not get filled with anything: I cannot retrieve the personal key from the eID.</p>
  
  <pre><code>   Private Sub Test()
      Dim myKeyStore As New X509Store(StoreName.My, StoreLocation.CurrentUser)
      myKeyStore.Open(OpenFlags.[ReadOnly])
      Dim myCertificateCollection As X509Certificate2Collection = myKeyStore.Certificates
      Dim myCertificate As X509Certificate2 = Nothing
      Dim selectedCertificates As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(myCertificateCollection, "Certificaten", "Select een certificaat om te tekenen", X509SelectionFlag.SingleSelection)
  
      If selectedCertificates.Count &gt; 0 Then
          Dim certificatesEnumerator As X509Certificate2Enumerator = selectedCertificates.GetEnumerator()
          certificatesEnumerator.MoveNext()
          myCertificate = certificatesEnumerator.Current
      End If
      myKeyStore.Close()
  
      'Settings'
      Dim source = "source.pdf"
      Dim result = "result.pdf"
      Dim reason = "test"
      Dim Location = "locatie"
  
  
      Dim myPkcs12Store As New Pkcs12Store()
      Using memorystreamPfx As New System.IO.MemoryStream(myCertificate.Export(X509ContentType.Pkcs12))
          myPkcs12Store.Load(memorystreamPfx, "")
      End Using
  
  
      For Each strAlias As String In myPkcs12Store.Aliases
          If myPkcs12Store.IsKeyEntry(strAlias) Then
              Dim pk = myPkcs12Store.GetKey(strAlias).Key
  
              Using myPdfReader As New PdfReader(source)
                  Using myFileStream As New FileStream(result, FileMode.Create, FileAccess.Write)
                      Using myPdfStamper As PdfStamper = PdfStamper.CreateSignature(myPdfReader, myFileStream, "0")
                          Dim myPdfDocument As New Document(myPdfReader.GetPageSizeWithRotation(1))
  
                          'Define the digital signature appearance'
                          Dim myPdfSignatureAppearance As PdfSignatureAppearance = myPdfStamper.SignatureAppearance
                          myPdfSignatureAppearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED
                          myPdfSignatureAppearance.Image = Image.GetInstance("Images/poro1_by_justduet-d63wx6c.png")
                          myPdfSignatureAppearance.Reason = reason
                          myPdfSignatureAppearance.Location = Location
                          myPdfSignatureAppearance.SetVisibleSignature(New iTextSharp.text.Rectangle(myPdfDocument.PageSize.Width - 120, 36, myPdfDocument.PageSize.Width - 36, 96), myPdfReader.NumberOfPages, "Digital Signature")
  
                          'Attach digital signature to PDF document'
                          Dim myExternalSignature As IExternalSignature = New PrivateKeySignature(pk, "SHA-256")
                          MakeSignature.SignDetached(myPdfSignatureAppearance, myExternalSignature, {(myPkcs12Store.GetCertificate(strAlias).Certificate)}, Nothing, Nothing, Nothing, 0, CryptoStandard.CMS)
                      End Using
                  End Using
              End Using
          End If
      Next
  </code></pre>
  
  <p>Any help would be appreciated! Further questions please ask</p>
  
  <p>bdebaere</p>