Search code examples
c#.netpdfpdfsharp

Get the PDF page that contains the PdfTextField


We need to get the PpdfPage that contains an especific PdfTextField, how can we do it?

Code:

PdfDocument document = PdfReader.Open("C:\\filex.pdf", PdfDocumentOpenMode.Modify);

// Get the root object of all interactive form fields
PdfAcroForm form = document.AcroForm;

// Get all form fields of the whole document
PdfAcroField.PdfAcroFieldCollection fields = form.Fields;

PdfTextField textfield1= (PdfTextField)fields["textfield1"];

//how to get the correct page reference respect the especified field?
PdfPage page = textfield1.Owner.Pages[???];

Solution

  • In the end we solved it creating this function:

        protected int PaginaCampo(string campofirma, PdfDocument document)
    {
        for (int i = 0; i < document.Pages.Count; i++)
        {
            PdfAnnotations anotations = document.Pages[i].Annotations;
            for (int j = 0; j < anotations.Count; j++)
            {
                if (anotations[j].Title != campofirma) continue;
                return i;
            }
        }
        return -1;
    }
    

    Not the best solution but it works...if someone adds a better one we will give the correct answer to him/her