We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: ASP.NET Web Forms |
Control: PdfViewer |
Published Date: October 31, 2018 |
Last Revised Date: June 19, 2019

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 http://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfViewer_ExtractformFields366803617

 

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon