Generating PDF from Form Submission
Our sample projects and report templates can help you learn the basics of working with our products.Apply submitted form values to the template in ASP.NET Core and return the completed PDF.
Filling the template. ASP.NET Core receives the submitted values together with the form definition and applies them before export.
Returning the PDF. The action in
Filling the template. ASP.NET Core receives the submitted values together with the form definition and applies them before export.
Returning the PDF. The action in
Generating PDF from Form Submission/Controllers/FormsController.cs creates the finished document and sends its bytes back with the PDF content type:private static StiPdfFormSubmission submission;
private static bool dataChanged = false;
static FormsController()
{
}
[HttpPost]
public IActionResult Action()
{
try
{
var data = JObject.Parse(this.HttpContext.Request.Form["data"]);
var action = data["action"].ToString();
switch (action)
{
case "Initialize":
resultForm = null;
var initData = StiWebForm.Initialize(data, Initialize(data));
return Json(initData.Content);
case "SaveForm":
var form = new StiForm();
form.Load(Convert.FromBase64String(data["form"].ToString()));
form.Save(FormPath);
return Json(form);
case "IsDataChanged":
return Json(dataChanged);
default:
var result = StiWebForm.ProcessRequest(data);
return result.ContentType switch
{
"application/pdf" => new FileContentResult(result.Content as byte[], result.ContentType),
_ => Json(result.Content),
};
}
}
catch (Exception e)
{
return new ContentResult()
{
Content = e.Message,
ContentType = "text/plain"
};
}
}
public IActionResult PostForm()
{
HttpRequest request = this.HttpContext.Request;
string requestBodyString;
try
{
request.EnableBuffering();
byte[] buffer = new byte[Convert.ToInt32(request.ContentLength)];
request.Body.ReadAsync(buffer, 0, buffer.Length);
requestBodyString = Encoding.UTF8.GetString(buffer);
submission = new StiPdfFormSubmission();
submission.ParseXFDF(buffer);
dataChanged = true;
}