To get a form field from an existing document using the field name, you can use the TryGetField method in the PdfFormFieldCollection class. It specifies whether the particular field is available in the form or not by returning a boolean value.
The below code snippet explains how to get the field from the collection using the TryGetField method.
//Load the PDF document. FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); PdfLoadedDocument doc = new PdfLoadedDocument(docStream); //Load the form from the loaded document. PdfLoadedForm form = doc.Form;
//Load the form field collections from the form. PdfLoadedFormFieldCollection fieldCollection = form.Fields as PdfLoadedFormFieldCollection; PdfLoadedField loadedField = null; //Get the field using TryGetField Method. if (fieldCollection.TryGetField("f1-1", out loadedField)) { (loadedField as PdfLoadedTextBoxField).Text = "1"; }
//Save the document into stream. MemoryStream stream = new MemoryStream(); doc.Save(stream); //Close the document. doc.Close(true); |
Follow the below links for more information,
https://help.syncfusion.com/file-formats/pdf/working-with-forms#trygetfield
We request you share the specific PDF document to analyze the reported behavior on our end. So that we can assist you further in this.