Search code examples
c#.netpdfitextacrobat

Error while signing multiple signature fields using itext c#


When the PDF is opened for the first time, the user can add a signature without any issue. However, during the same session, if the user tries to add a second signature, this error occurs. After signing for the first time, closing, and then reopening the PDF, the user can sign multiple signature fields without any issues.

 public static void Main(string[] args)
 {
     string DEST = "path";
     PdfWriter writer = new PdfWriter(DEST);
     PdfDocument pdfDoc = new PdfDocument(writer);
     Document document = new Document(pdfDoc);

     PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);

     AddSignatureField(form, "signature1", 36, 700, 200, 50);
     AddSignatureField(form, "signature2", 36, 600, 200, 50);
     AddSignatureField(form, "signature3", 36, 500, 200, 50);

     document.Close();
 }

 public static void AddSignatureField(PdfAcroForm form, string fieldName, float x, float y, float width, float height)
 {
     Rectangle rect = new Rectangle(x, y, width, height);
     PdfWidgetAnnotation pdfWidgetAnnotation = new PdfWidgetAnnotation(rect);
     PdfSignatureFormField signatureField = PdfFormCreator.CreateSignatureFormField(pdfWidgetAnnotation, form.GetPdfDocument());
     signatureField.SetFieldName(fieldName);
     form.AddField(signatureField);
 }

Solution

  • The error

    Unfortunately you didn't tell which error occurred while signing with which software. Out of interest I tried it nonetheless, starting with Adobe Acrobat, and indeed, there was an error! After successfully signing the first signature field, while trying to sign the second one, I got the following messages, one after the other:

    Screenshot

    Screenshot

    Screenshot

    The cause

    I created a similar file using Adobe Acrobat, i.e. a document with one page with three empty signature fields. With this file the error did not occur.

    Thus, I compared the file generated by iText and the file generated by Acrobat with each other. The latter contained a number of additional entries but according to the PDF specification all of them are optional.

    Nonetheless, removing those entries one after the other from the Acrobat generated file I eventually could reproduce the issue with that file, too. As it turned out, Acrobat needs the Type entry of the merged field/widget objects to be present!

    As this entry is optional and it also is clear by structure that those objects are merged field/widget objects, this clearly is a bug of Adobe Acrobat. Thus, you should file a bug with Adobe.

    A work-around

    To be able to create signature field files for the current Acrobat, though, you can extend your code to set the widget annotation Type field accordingly, change your AddSignatureField method like this:

    Rectangle rect = new Rectangle(x, y, width, height);
    PdfWidgetAnnotation pdfWidgetAnnotation = new PdfWidgetAnnotation(rect);
    pdfWidgetAnnotation.Put(PdfName.Type, PdfName.Annot);
    PdfSignatureFormField signatureField = PdfFormCreator.CreateSignatureFormField(pdfWidgetAnnotation, form.GetPdfDocument());
    signatureField.SetFieldName(fieldName);
    form.AddField(signatureField);
    

    (From AddSignatureField method AddSignatureFieldDharmendraSinsinwarImproved)

    in particular, add the pdfWidgetAnnotation.Put(PdfName.Type, PdfName.Annot) line.