SignPAdESBT.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS 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 DsPdfWeb.Demos
{
    // This sample shows how to sign an existing PDF file containing
    // an empty signature field with a signature that complies with
    // PAdES (PDF Advanced Electronic Signatures) B-T level standard.
    public class SignPAdESBT
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            using var s = File.OpenRead(Path.Combine("Resources", "PDFs", "SignPAdESBT.pdf"));
            doc.Load(s);

            var pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx");
            var cert = new X509Certificate2(pfxPath, "qq");
            var sp = SignatureProperties.CreatePAdES_B_T(new TimeStamp("https://freetsa.org/tsr"), cert);
            sp.SignatureAppearance.Caption = "PAdES B-T";
            sp.SignatureField = doc.AcroForm.Fields[0];
            doc.Sign(sp, stream);

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