Skip to main content

Generate dynamically a signed Pdf with Net Core 5 (C#)

Published —

Do you need to generate a certificate, diploma, or any pdf document signed with your own pfx? Here is a sample of how to do it with Net Core 5.0 using NuGet package iText7

You can download the project from GitHub.

The project includes in a resource file a test certificate generated in Pdf Powertool Website and a PDF Template to add text dynamically using the iText7 NuGet package.

This is the way to initialize your document and assign to a memory stream with IText7 objects (ResourcePDF.template is the demo pdf template added to the resource file) :

MemoryStream template = new MemoryStream(ResourcePDF.template);

 using (MemoryStream output = new MemoryStream())
 {
     PdfReader reader = new PdfReader(template);
     PdfWriter writer = new PdfWriter(output);
     PdfDocument pdf = new PdfDocument(reader, writer);
     Document document = new Document(pdf);

     Paragraph p = new Paragraph("Mr. JOHN DOE");
     p.SetFixedPosition(88, 690, 500);
     p.AddStyle(DefaultFont(18, true));
     document.Add(p);

    pdf.Close();
 }

Once we have the pdf info completed, we are going to sign it (input memory stream) with the pfx (password is needed) using Pkcs12Store and X509Certificate

MemoryStream msPFX = new MemoryStream(ResourcePDF.certificate);

Pkcs12Store pk12 = new Pkcs12Store(msPFX, _Password.ToArray());
string alias = null;
foreach (object a in pk12.Aliases)
{
    alias = ((string)a);
    if (pk12.IsKeyEntry(alias))
       break;
}
ICipherParameters pk = pk12.GetKey(alias).Key;

Org.BouncyCastle.Pkcs.X509CertificateEntry[] ce = pk12.GetCertificateChain(alias);
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[ce.Length];
for (int k = 0; k < ce.Length; ++k)
   chain[k] = ce[k].Certificate;

using (PdfReader reader = new PdfReader(input))
{
   using (MemoryStream output = new MemoryStream())
   {
      PdfSigner signer = new PdfSigner(reader, output, new StampingProperties());

      PdfSignatureAppearance appearance = signer.GetSignatureAppearance();
      appearance
         .SetReason("Certificate is issued at the upon request")
         .SetLocation("alcorcon")
         .SetContact("acbteam.com");
      signer.SetFieldName("Signature");

       IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256);
       signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
      
    }
}

The demo console project will generate this demo PDF in your local path (sample is set on “C:\dev)

DC

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles That Might
Interest You

ASP Net Core Service Lifetime

If you are going to use dependency injection framework in Net Core you must control the service l…

Configure SSL to securely connect to Azure Database for MySQL

Azure DB for MySQL supports connecting to client applications using SSL. Enforcing SSL between yo…

Generate dynamically a signed Pdf with Net Core 5 (C#)

Do you need to generate a certificate, diploma, or any pdf document signed with your own pfx? Her…