I have written the code below to delete the form fields from an existing PDF that I am trying to update. When the process is finished and I review the pdf using Adobe Acrobat, all the form fields are displayed rather than a clean pdf.
var folderPath = Path.Combine(_environment.ContentRootPath, "Files");
var files = Directory.GetFiles(folderPath);
foreach(var file in files)
{
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
PdfLoadedForm loadedForm = loadedDocument.Form;
var fieldsCount = loadedDocument.Form.Fields.Count;
var pages = loadedDocument.Pages;
for(int i = fieldsCount-1; i >= 0; i--)
{
loadedForm.Fields.RemoveAt(i);
}
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
loadedDocument.Close(true);
}