SignWithP12.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Security;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Text;


namespace GcPdfWeb.Samples
{
    // This sample shows how to sign a PDF with a .p12 file
    // containing a digital certificate that uses PKCS#12
    // (Public Key Cryptography Standard #12) encryption.
    public class SignWithP12
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            using var s = File.OpenRead(Path.Combine("Resources", "PDFs", "SignWithP12.pdf"));
            doc.Load(s);

            var p12 = Path.Combine("Resources", "Misc", "GcPdfTest2KeyStore.p12");
            SignatureProperties sp = new SignatureProperties()
            {
                SignatureBuilder = new Pkcs7SignatureBuilder()
                {
                    CertificateChain = SecurityUtils.GetCertificateChain(p12, "qq"),
                },
                SignatureField = doc.AcroForm.Fields[0],
            };
            sp.SignatureAppearance.Caption = "PKCS #12";
            doc.Sign(sp, stream);

            // Done.
            return doc.Pages.Count;
        }
    }
}