PDF form submission to .Net Core Web API, return result

322 Views Asked by At

I have created a .net core web api controller in C#, that accepts PDF form data in FDF format, from a submit button on the form. Because my controller tries to store the data in a database, I would like to return a success/fail status to the form, which should be displayed to the user. The form is a regular Acrobat form (so not a web form) and is filled from the browser. I have read about returning FDF data with a /Status, but have not found how to translate that info to web api, which is quite new to me.

1

There are 1 best solutions below

1
On

You can Return a PdfResult that Adobe will accept. The return value must be based on where the document was submitted from. Here's my mvc controller

        [AllowAnonymous]
        [HttpPost]
        public PdfResult SubmitForm(string Browser)
        {
            using (var sr = new StreamReader(Request.InputStream))
            {
                var fdfStream = sr.ReadToEnd();
                var fieldsDict = _pdfFormService.ExtractFieldsText(fdfStream);

                var formJson = JsonConvert.SerializeObject(fieldsDict);

                var clientId = fieldsDict["client-id"];
                var agencyId = fieldsDict["agency-id"];
                var username = fieldsDict["username"];
                var formId = fieldsDict["form-id"];
                _pdfFormService.SubmitForm(int.Parse(agencyId), username, int.Parse(clientId), int.Parse(formId), formJson);

                var document = PdfHelper.BuildSuccessPdf();

                if (Browser == "Browser")
                    return document.ExportAsActionResult("success.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open);
                else
                    return document.ExportAsActionResult("success.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);
            }
        }

I found the definitions for PDF results and the document.ExportAsActionResult extension methods here.