PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

How to find all AcroFields on a page
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4144
Page 1 of 1

Author:  wiliek [ Wed Jun 10, 2020 7:29 pm ]
Post subject:  How to find all AcroFields on a page

Is there a way to find all acrofields on a page and then delete one if it falls with some specified coordinates?

It doesn't appear you can use pdfPage to find fields. I guess they don't exist on the page but in AcroForms object.

So I need to traverse ACroForms and find each field? I'm not sure how to do that. Do I get each element then traverse that element looking for children until I find /Rect? If I knew the name of the field then I could search for it. But maybe I should get all field names then search each field name looking for elements? I just need to start by finding all AcroFields but I don't even know how to do that. Please help.

Author:  rsoeung [ Fri Jan 22, 2021 10:54 am ]
Post subject:  Re: How to find all AcroFields on a page

Hi! Try this.

Code:
using (var doc = PdfReader.Open("myPdf.pdf", PdfDocumentOpenMode.ReadOnly))
{
    int fieldCount = doc.AcroForm.Fields.Count;
}



Best,
Reas

Author:  robberbaron [ Mon Feb 08, 2021 4:12 am ]
Post subject:  Re: How to find all AcroFields on a page

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");
        }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/