How to Extract Data and Save in Database using ASP.NET PDF Viewer?
ASP.NET Web Forms 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 get 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; }