if you know the field names to be removed (like I do...)
Code:
XPoint lowestPoint = new XPoint(312,626);
redundantFieldList = new List<string>(8 - staffIdx); //names of redundant fields
if (staffIdx > 0)
{
PdfRectangle rawrect = Pdfsharp_Helpers.FindAnnotationRectangle(_qaForm.Pages[0], string.Format("Role{0}", staffIdx ));
//lowestPoint = new XPoint(rawrect.X2,847-rawrect.Y2); ///completely strange..... works for my form though
lowestPoint = new XPoint(rawrect.X2,rawrect.Y2);
for (int i = staffIdx+1; i <= 8; i++)
{
redundantFieldList.Add(string.Format("Name{0}", i));
redundantFieldList.Add(string.Format("Initials{0}", i));
redundantFieldList.Add(string.Format("Role{0}", i));
redundantFieldList.Add(string.Format("Signature{0}", i));
}
}
PdfPage activPage = _qaForm.Pages[0];
RemoveUnwantedFormfields(ref activPage, redundantFieldList);
return lowestPoint;
}
/// <summary>
/// Remove unneeded fields to cleanup page
/// </summary>
/// <param name="page"></param>
/// <param name="fieldList">List of unwanted fieldname</param>
private void RemoveUnwantedFormfields(ref PdfPage page, List<string> fieldList )
{
// Remove all the form fields from the copied page
var iRemoved = 0;
foreach (PdfAnnotation annotation in page.Annotations.ToList().OfType<PdfAnnotation>())
{
var title = (annotation.Elements[PdfAcroField.Keys.Parent] is PdfReference annotParent)
? ((PdfDictionary)annotParent.Value).Elements[PdfAcroField.Keys.T].ToString()
: annotation.Title;
if (fieldList.Contains(title))
{
page.Annotations.Remove(annotation);
iRemoved++;
}
}
//System.Diagnostics.Debug.Write ($"removed {iRemoved} fields");
}