Products Technologies Demo Docs Blog Support Company

DocumentViewer: Deploying Forms

This sample shows how to deploy a form with form fields using the DocumentViewer in order to collect the completed data.

DocumentViewer: Deploying Forms

The DocumentViewer for ASP.NET and Angular supports the completion of MS Word compatible form fields including text form fields, checkboxes, dropdowns and date picker fields. Form fields are available automatically when they are part of the document that is loaded into the DocumentViewer. Based on the Conditional Instructions , required fields can be validated.

Forms, that can be created in the TX Text Control editor, are deployed using the DocumentViewer that allows end-users to complete form fields. The completed data can be merged back into the document in order to create a PDF document or it is possible to extract the form data only. This is helpful, if the completed data should be used to write them back into a database or the form data should be analyzed.

This ASP.NET Core sample shows how to deploy a form and how to collect the resulting data. For demo purposes, all Deployments are stored in a simple JSON file server-side. After creating a new Deployment, the list of available deployments is updated:

Creating documents with TX Text Control

The Deployment simply stores a user name (or id) and the available form fields:

public class Deployment {
  public string Username { get; set; }
  public FormField[] FormFields { get; set; }
}

The FormField model contains the Name, Type and Value and is returned by the forms.getValues JavaScript method of the DocumentViewer:

public class FormField {
  public string name { get; set; }
  public string type { get; set; }
  public dynamic value { get; set; }
}

After clicking on Open, the end-user's view is opened in a new tab:

Creating documents with TX Text Control

After all fields have been completed, the form field values are posted back to the controller:

function submitform() {

    // validate forms
    var valid = TXDocumentViewer.forms.validate();

    if (valid === false) { // not valid
        $('.alert-info').text(
            "Document contains invalid form fields.").show().delay(5000).fadeOut();
        return;
    }

    // -> valid

    // get the form values
    var formsJson = JSON.stringify(TXDocumentViewer.forms.getValues());

    // send values to controller
    var serviceURL = "@Url.Action("FormValues", "Home", new { username = Model.Username })";

    $.ajax({
        type: "POST",
        url: serviceURL,
        contentType: 'application/json',
        data: formsJson,
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data, status) {
        $('.alert-info').text(
            "Thanks. Your document has been submitted!").show().delay(5000).fadeOut();
        TXDocumentViewer.downloadDocument(TXDocumentViewer.StreamType.AdobePDF, true);
    }

    function errorFunc() {
        $('.alert-info').text("Oops! There was an error.").show().delay(5000).fadeOut();
    }
}

The controller method FormValues is deserializing the form field values and calls the helper method DeploymentHelpers.WriteValue:

[HttpPost]
public IActionResult FormValues(string Username) {

  // read stream to retrieve form field values
  Stream inputStream = Request.Body;
  FormField[] fields;

  StreamReader str = new StreamReader(inputStream);

  string sBuf = str.ReadToEndAsync().Result;
  fields = (FormField[])JsonConvert.DeserializeObject(sBuf, typeof(FormField[]));

  // write the values
  DeploymentHelpers.WriteValue(Username, fields);

  return Ok();
}

The DeploymentHelpers.WriteValue method is writing the completed form field values into the Deployments data source:

public static bool WriteValue(string username, FormField[] formFields) {

  List<Deployment> deployments = GetDeployments();

  foreach (Deployment deployment in deployments) {
    if (deployment.Username == username) {
      deployment.FormFields = formFields;

      System.IO.File.WriteAllText("App_Data/deployments.json", 
                                  JsonConvert.SerializeObject(deployments));

      return true;
    }
  }

  return false;
}

Back in the overview, the posted values are now visible in the table:

Creating documents with TX Text Control

You can test this sample on your own by downloading the project from our GitHub account.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Text Control .NET Server X19
  • Visual Studio 2019

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularASP.NETDS Server

Version 30.0 Live Preview

Still in early beta, we published a live demo that shows some of the new features of TX Text Control 30.0 including comments and the new online document editor ribbon style.


AngularASP.NETDS Server

TX Text Control 30.0 Preview: Improved Online Document Editor Ribbon Design

We are currently working on version 30.0 that is planned to be released in Q4 2021. In this new version, the online document editor will receive an improved ribbon design and additional sidebars.…


AngularASP.NETCollaboration

DocumentViewer 29.2 (29.0.302.500) Final Released

We are happy to announce the final release of version 29.2 of the DocumentViewer for ASP.NET and ASP.NET Core. It comes with many new annotation and form field features to deploy documents for…


ASP.NETDocumentViewerDS Server

DocumentViewer Pre-Release: UI Improvements and Events

We published another pre-release version of the TX Text Control DocumentViewer that includes UI improvements and event handling for specific events. This article gives an overview of the latest…


AngularASP.NETAnnotations

DocumentViewer Annotations: Highlight Text

We just published a pre-release version of the DocumentViewer that includes a new annotation feature: Text highlights. It is now possible to select text to add a comment at a specific text…