Digitally sign PDF document - Aspose.PDF - Invalid signature after saving file

Programming in C#.NET. When setting all form fields except the signature field to readonly of an editable PDF file, the signature field will become invalid. The signature field will not be changed.
The signature is a PKCS#12 pfx file, that is used to set the signature manually into the signature field.
Any help would be appreciated.

Aspose.Pdf version 20.9.0.0
.NET Framework 4.7.2

I discovered that saving the file seems to break the signature field?
Is it possible to change the document without invalidating the signature by specifying the changes?

Code to set form fields readonly:

  var pdfDoc = new Document(fileName);
  foreach (var field in pdfDoc.Form.Fields)
  {
      if (field.PartialName != "DigitalSignature")
      {
          //field.Flatten();
          field.ReadOnly = true;
      }
  }
  pdfDoc.Save(fileName);

I tried also this code instead of above code (still signature is invalid):

  DocumentPrivilege privilege = DocumentPrivilege.ForbidAll;
  //privilege.ChangeAllowLevel = 1;
  privilege.AllowPrint = true;
  //privilege.AllowCopy = true;

  // Create PdfFileSecurity object 
  PdfFileSecurity fileSecurity = new PdfFileSecurity();
  fileSecurity.BindPdf(fileName);

  var tempPath = FileHelper.GetTempFileName("pdf");
  // Set document privileges
  fileSecurity.SetPrivilege(privilege);
  fileSecurity.Save(tempPath);

  FileHelper.Delete(fileName);
  return tempPath;

@NicoHorsmans

Could you please share the sample PDF document for our reference. We will test the scenario in our environment and address it accordingly.

@asad.ali

This is a test pdf, that I created for the case. This fillable document and also the original document are created in Adobe Acrobat Pro with some text fields and one signature field.
It is filled in right now and the signature is from a PKCS#12 pfx file. The signature is set manually by the user by opening the fillable form and save it afterwards.
Hope it is helpfull to you.

Test aspose signature.PDF (114.8 KB)

@NicoHorsmans

Any changes made to a signed PDF document, revoke the signatures. We further need to investigate if this can be prevented while making the other fields just read-only. After testing and replicating the scenario using Aspose.PDF for .NET 21.2, we have logged an investigation ticket as PDFNET-49557 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

@NicoHorsmans

var fileName = dataDir + "Test aspose signature.PDF";

using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
    using (var pdfDoc = new Document(fs))
    {
        foreach (var field in pdfDoc.Form.Fields)
        {
            if (field.PartialName != "DigitalSignature")
            {
                field.ReadOnly = true;
            }
        }
        pdfDoc.Save();
    }
}

It will save the signature and set the field as readonly.