PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 31, 2024 11:55 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jun 10, 2020 7:29 pm 
Offline

Joined: Wed Jun 10, 2020 7:15 pm
Posts: 7
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.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 22, 2021 10:54 am 
Offline

Joined: Sat Jan 09, 2021 7:09 am
Posts: 18
Hi! Try this.

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



Best,
Reas


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 08, 2021 4:12 am 
Offline

Joined: Wed Oct 19, 2016 9:14 am
Posts: 7
Location: Australia
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");
        }

_________________
Thanks

Rob


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 27 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group