Articles in this section
Category / Section

How to extract form field data from PDF and save it in the database

2 mins read

PDF Viewer

PDF Viewer control supports viewing, reviewing, and printing PDF files in ASP.NET Web Forms applications. The hyperlink and table of contents support provides easy navigation within and outside the PDF files. The form-filling support provides a platform to fill, flatten, save, and print PDF files with Acroform. PDF files can be reviewed with text markup annotation tools.

Refer to the following link to getting started with PDF Viewer,

https://help.syncfusion.com/aspnet/pdfviewer/gettingstarted

Extract form fields from PDF document

You can extract the form field data from the PDF document and save it as JSON data in the database.

The form filled PDF document can be submitted to the server using the download() API. Also, the new web action method (Submit()) in the Web API controller should be created to extract the from fields and then map the download function to it using the serverActionSettings API. Refer to the following code.

HTML

<div>
<button type="button" onclick="submitPDF();"> Submit</button>
</div>

JavaScript

  function submitPDF() {
            var pdfviewerObj = $("#pdfviewer").data("ejPdfViewer");
        //Mapping to Submit() in Web API Controller for extracting the form field data from the PDF document
            pdfviewerObj.model.serverActionSettings.download = "Submit";
            pdfviewerObj.download();
       //Mapping to Download() in Web API controller for downloading the PDF document from the PDF viewer control
            pdfviewerObj.model.serverActionSettings.download = "Download";
        }  

C#

public object Submit(Dictionary<string, string> jsonResult)
        {
            PdfViewerHelper helper = new PdfViewerHelper();  
                     
            var output = helper.GetDocumentData(jsonResult);
 
            //Creating the PdfLoadedDocument
 
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(helper.DocumentStream);
 
            Dictionary<string, string> formFieldsDictionary = new Dictionary<string, string>();
 
            if (loadedDocument.Form != null)
            {
                foreach (PdfField currentField in loadedDocument.Form.Fields)
                {
                    //To extract the form field values from the PdfLoadedDocuemnt
                    string currentFieldName = Regex.Replace(currentField.Name, "[^0-9a-zA-Z]+", "");
                    if (currentField is PdfLoadedTextBoxField)
                    {
                        PdfLoadedTextBoxField textbox = currentField as PdfLoadedTextBoxField;
                        formFieldsDictionary.Add(currentFieldName, textbox.Text);
                    }
                    if (currentField is PdfLoadedRadioButtonListField)
                    {
                        formFieldsDictionary.Add(currentFieldName, (currentField as PdfLoadedRadioButtonListField).SelectedValue);
                    }
                    if (currentField is PdfLoadedCheckBoxField)
                    {
                        formFieldsDictionary.Add(currentFieldName, ((currentField as PdfLoadedCheckBoxField).Checked).ToString());
                    }
                    if (currentField is PdfLoadedComboBoxField)
                    {
                        formFieldsDictionary.Add(currentFieldName, (currentField as PdfLoadedComboBoxField ).SelectedValue);
                    }
                }
            }
            //Converting to JSON object
            var data = JsonConvert.SerializeObject(formFieldsDictionary);
            System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("~/Output/formfields.json"), data);           
            return null;
        }  

Sample https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfViewer_ExtractformFields366803617

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied