Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add field for a digital signature to PDF

I create a new PDF file using PDFsharp and MigraDoc. Now I want to add a field where the user can click on it and select a certificate for digital signing the file.

I found in the web, that this should be possible with AcroForms. But I wasn't able to use AcroForm because it is always null.

My current example code:

Document document = new Document();

Section section = document.AddSection();
section.AddParagraph("Signature Test");


PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();

// NullPointerException at the following line. AcroForm is null 
pdfRenderer.PdfDocument.AcroForm.Elements.Add(PdfAcroForm.Keys.SigFlags, new PdfInteger(3));

const string filename = "HelloWorld.pdf";
pdfRenderer.PdfDocument.Save(filename);
Process.Start(filename);

Why is this property null? What can I do to set this to a correct value?

Or better, how can I add a field to select the digital certificate?

like image 589
Sammy Avatar asked Aug 26 '19 08:08

Sammy


1 Answers

I found a solution that adds a signature field to the document. Unfortunately I had to use reflection because auf missing access to the requried objects.

using PdfSharp.Pdf;
using PdfSharp.Pdf.Annotations;
using System;
using System.Linq;
using System.Reflection;

internal sealed class MyPdfSignatureField : PdfAnnotation
{
    public MyPdfSignatureField(PdfDocument document, PdfRectangle rect) : base(document)
    {
        Elements.Add("/FT", new PdfName("/Sig"));
        Elements.Add(Keys.T, new PdfString("Signature1"));
        Elements.Add("/Ff", new PdfInteger(132));
        Elements.Add("/DR", new PdfDictionary());
        Elements.Add(Keys.Subtype, new PdfName("/Widget"));
        Elements.Add("/P", document.Pages[0]);


        PdfDictionary sign = new PdfDictionary(document);
        sign.Elements.Add(Keys.Type, new PdfName("/Sig"));
        sign.Elements.Add("/Filter", new PdfName("/Adobe.PPKLite"));
        sign.Elements.Add("/SubFilter", new PdfName("/adbe.pkcs7.detached"));
        sign.Elements.Add(Keys.M, new PdfDate(DateTime.Now));

        var irefTable = document
            .GetType()
            .GetField("_irefTable", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(document);
        var irefTableAdd = irefTable
            .GetType()
            .GetMethods()
            .Where(m => m.Name == "Add").Skip(1).FirstOrDefault();

        irefTableAdd.Invoke(irefTable, new object[] { sign });

        Elements.Add("/V", sign);

        Elements.Add("/Rect", rect);
        Flags = PdfAnnotationFlags.Print;
        Opacity = 1;
    }
}

And to use:

var sig = new MyPdfSignatureField(document, new PdfRectangle(new XPoint(480, 33.75), new XPoint(612.2, 62.1)));
document.Pages[0].Annotations.Add(sig);

This is no very nice solution but it covers my basic requirements. If you have some better idea, please let me know.

This idea of this class comes from here: https://github.com/empira/PDFsharp/pull/11/files

like image 165
Sammy Avatar answered Oct 02 '22 17:10

Sammy