With iText i can't fill signature field using base64 string image on setValue

Viewed 59

With iText i can't fill signature field using base64 string image on setValue

There is no signature filled so what's wrong with this code?

private byte[] Sign(byte[] src, X509Certificate[] chain, ICipherParameters pk,
   String digestAlgorithm, PdfSigner.CryptoStandard subfilter, string from, string to, string subject, string business, List<PDFExistingFieldsInfos> pdfExistingFieldsInfos)
{

  byte[] filledPDF = src;

  using (MemoryStream srcMemStream = new MemoryStream(src))
  {
    using (MemoryStream memStream = new MemoryStream())
    {
      PdfReader r = new PdfReader(srcMemStream);
      r.SetUnethicalReading(true); // todo: à utilisé pour bypass l'encryption du document
      PdfDocument pdfDoc = new PdfDocument(r, new PdfWriter(memStream));

      PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
      form.SetGenerateAppearance(true); // a tester!!

      foreach (var pdfFieldInfos in pdfExistingFieldsInfos)
      {
        switch (pdfFieldInfos.FieldType)
        {
          case "sign":
            PdfSignatureFormField myPdfSignatureFormField = (PdfSignatureFormField)form.GetField(pdfFieldInfos.FieldName);
            myPdfSignatureFormField.SetValue(pdfFieldInfos.FieldValue);
            break;

          case "text":
            form.GetField(pdfFieldInfos.FieldName).SetValue(pdfFieldInfos.FieldValue ?? "", null);
            break;
        }
      }
      form.FlattenFields();
      pdfDoc.Close();

      filledPDF = memStream.ToArray();
    }
  }
  return filledPDF;
}

pdfFieldInfos.FieldValue is a base64 image string

1 Answers

PdfSignatureFormField models the PDF AcroForm field type Sig. Fields of this type can hold digital signatures, i.e. structures that cryptographically allow to verify both that the signed data has not been changed and who the signer was. Such fields can also have an appearance in the document which may contain a bitmap image. This appearance, though, is not the value of the field, merely its looks.

Strictly speaking the PDF standard does not specify form fields with image values at all. As over time the need for image fields arose, though, PDF creators started to (mis)use push buttons as image fields.

If you want to add signature bitmap images to a PDF, therefore, consider using push buttons.

Related