A modern ASP.NET Core MVC application that dynamically generates HTML forms from document templates and produces PDF contracts using TX Text Control.
This application demonstrates a dynamic form generation workflow:
- Extract form fields from a TX Text Control document template
- Generate an HTML form dynamically based on extracted fields
- Collect user input through a modern Bootstrap 5 interface
- Merge form data back into the template
- Generate a PDF document with the merged data
- .NET 10.0 - Latest .NET framework
- C# 14.0 - Modern C# language features
- ASP.NET Core MVC - Web application framework
- TX Text Control 34.0.3 - Document processing engine
- Bootstrap 5 - Modern UI framework
- Bootstrap Icons - Icon library
tx-html-forms/
βββ Controllers/
β βββ HomeController.cs # Main MVC controller
βββ Models/
β βββ TemplateField.cs # Field descriptor model
β βββ ErrorViewModel.cs # Error handling model
βββ Helpers/
β βββ TemplateFieldExtractor.cs # Template parsing logic
βββ TagHelpers/
β βββ FormFieldTagHelper.cs # Custom tag helper for form fields
βββ Views/
β βββ Home/
β β βββ Index.cshtml # Dynamic form view
β βββ Shared/
β β βββ _Layout.cshtml # Application layout
β βββ _ViewImports.cshtml # Global view imports
β βββ _ViewStart.cshtml # View initialization
βββ App_Data/
βββ Templates/
βββ ContractMergeTemplate.tx # TX Text Control template
Represents a form field extracted from the document template:
public class TemplateFieldDescriptor
{
public string Name { get; set; } // Field identifier
public string Description { get; set; } // User-friendly label
public string FieldType { get; set; } // Field type (TextFormField, DateFormField, etc.)
public bool Required { get; set; } // Validation flag
public string? DefaultValue { get; set; } // Pre-populated value
public List<string> Options { get; set; } // Options for SelectionFormField
}Extracts form field metadata from TX Text Control documents:
Supported Field Types:
TextFormField- Single-line text inputDateFormField- Date pickerCheckFormField- Checkbox/toggleSelectionFormField- Dropdown select
Process:
- Creates a
ServerTextControlinstance - Loads the template document
- Iterates through
FormFieldscollection - Maps each field to a
TemplateFieldDescriptor - Returns a strongly-typed list
A custom ASP.NET Core Tag Helper that renders form fields based on their type:
Features:
- Pattern matching with switch expressions
- Automatic icon assignment per field type
- Bootstrap 5 styling
- Required field indicators
- HTML encoding for XSS protection
- Responsive form controls (large size)
Field Rendering:
| Field Type | HTML Control | Icon | Features |
|---|---|---|---|
| TextFormField | <input type="text"> |
pencil-square | Placeholder text, large input |
| DateFormField | <input type="date"> |
calendar-date | Large date picker |
| CheckFormField | <input type="checkbox"> |
check-square | Bootstrap switch style |
| SelectionFormField | <select> |
list-ul | Default prompt option |
Endpoints:
- Loads the template file from
App_Data/Templates/ - Extracts form fields using
TemplateFieldExtractor - Renders the dynamic form view
- Receives form data via
IFormCollection - Serializes data to JSON
- Creates a new
ServerTextControlinstance - Loads the template
- Performs mail merge with
MailMergecomponent - Generates PDF using
BinaryStreamType.AdobePDF - Returns PDF file to browser
- Card Layout - Clean, professional appearance
- Responsive Grid - Works on all devices
- Icon System - Visual field type indicators
- Color Scheme - Primary blue theme
- Typography - Semi-bold labels, proper hierarchy
- Spacing - Generous margins and padding
- Form Controls - Large, touch-friendly inputs
- Semantic HTML structure
- Proper label associations
- Required field indicators
- ARIA roles for switches
Template Document (.tx)
β
[TemplateFieldExtractor]
β
List<TemplateFieldDescriptor>
β
[FormFieldTagHelper]
β
Dynamic HTML Form
β
User Input
β
Form Submission (JSON)
β
[MailMerge Component]
β
PDF Document
- Template Loading: TX Text Control loads
ContractMergeTemplate.tx - Field Extraction: Form fields are enumerated and converted to DTOs
- View Rendering: Tag helper generates HTML for each field
- Form Submission: User data is serialized to JSON
- Mail Merge: TX Text Control merges JSON data into template
- PDF Generation: Document is saved as PDF binary
- File Download: Browser receives PDF file
Before (46 lines, multiple if/else):
@if (field.FieldType == "TextFormField")
{
<input class="form-control" type="text" ... />
}
else if (field.FieldType == "DateFormField")
{
<input class="form-control" type="date" ... />
}
// ... more conditionsAfter (1 line):
<form-field descriptor="@field"></form-field>Benefits:
- Cleaner, more maintainable views
- Reusable across multiple forms
- Centralized rendering logic
- Type-safe with compile-time checking
- Easier to test
TX Text Control's ServerTextControl methods are inherently synchronous. Wrapping them in Task.Run() provides no real async benefits and adds overhead. The current implementation uses synchronous methods appropriately.
The template path is computed once in the constructor and reused:
- Eliminates repeated
Path.Combine()calls - Reduces string allocations
- Improves performance on high-traffic scenarios
- .NET 10.0 SDK or later
- TX Text Control license (place in appropriate directory)
- Visual Studio 2022 or Visual Studio Code
-
Clone the repository
git clone <repository-url> cd tx-html-forms
-
Configure TX Text Control License
- Ensure your TX Text Control license file is properly configured
- Default location:
~/.nuget/packages/txtextcontrol.textcontrol.core.sdk/34.0.3/contentFiles/any/any/txtextcontrol.server.license
-
Add your template
- Place your
.txtemplate file inApp_Data/Templates/ - Update the filename in
HomeControllerif needed
- Place your
-
Build and run
dotnet restore dotnet build dotnet run
-
Open browser
- Navigate to
https://localhost:5001or the URL shown in console
- Navigate to
- Navigate to the home page
- Fill out the dynamically generated form
- Click "Generate PDF Document"
- Verify the PDF contains merged data
Your TX Text Control template should include:
- Form fields with descriptive text
- Field names matching your data model
- Appropriate field types (text, date, checkbox, selection)
- HTML Encoding: All user input is encoded using
HtmlEncoder.Default - XSS Protection: Tag helper prevents script injection
- Type Safety: Strong typing throughout the application
- Validation: Required field support (extensible)
- Tag Helper: Reduces view rendering overhead
- Path Caching: Template path computed once
- Switch Expressions: Pattern matching for better performance
- Minimal Allocations: Efficient string handling
- Add a new case in
TemplateFieldExtractor.Extract() - Add a new case in
FormFieldTagHelper.Process() - Add icon mapping in
GetFieldIcon() - Add HTML generation method (e.g.,
GenerateCustomInput())
- Bootstrap variables can be customized in
site.css - Tag helper classes can be modified in
FormFieldTagHelper.cs - Layout changes in
_Layout.cshtml
This project uses TX Text Control, which requires a commercial license. Ensure compliance with TX Text Control licensing terms.
Contributions are welcome! Areas for enhancement:
- Client-side validation
- Field dependencies
- Conditional field visibility
- Multi-page forms
- Custom CSS themes
Built with TX Text Control and ASP.NET Core MVC
Version: 1.0.0
Last Updated: 2025
.NET Version: 10.0
TX Text Control Version: 34.0.3