Skip to content

TextControl/TXTextControl.FormsToPDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TX Text Control HTML Forms to PDF

A modern ASP.NET Core MVC application that dynamically generates HTML forms from document templates and produces PDF contracts using TX Text Control.

πŸ“‹ Overview

This application demonstrates a dynamic form generation workflow:

  1. Extract form fields from a TX Text Control document template
  2. Generate an HTML form dynamically based on extracted fields
  3. Collect user input through a modern Bootstrap 5 interface
  4. Merge form data back into the template
  5. Generate a PDF document with the merged data

πŸ—οΈ Architecture

Technology Stack

  • .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

Project Structure

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

πŸ”§ Key Components

1. TemplateFieldDescriptor Model

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
}

2. TemplateFieldExtractor

Extracts form field metadata from TX Text Control documents:

Supported Field Types:

  • TextFormField - Single-line text input
  • DateFormField - Date picker
  • CheckFormField - Checkbox/toggle
  • SelectionFormField - Dropdown select

Process:

  1. Creates a ServerTextControl instance
  2. Loads the template document
  3. Iterates through FormFields collection
  4. Maps each field to a TemplateFieldDescriptor
  5. Returns a strongly-typed list

3. FormFieldTagHelper

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

4. HomeController

Endpoints:

GET / - Index Action

  • Loads the template file from App_Data/Templates/
  • Extracts form fields using TemplateFieldExtractor
  • Renders the dynamic form view

POST /Submit - Submit Action

  • Receives form data via IFormCollection
  • Serializes data to JSON
  • Creates a new ServerTextControl instance
  • Loads the template
  • Performs mail merge with MailMerge component
  • Generates PDF using BinaryStreamType.AdobePDF
  • Returns PDF file to browser

🎨 UI Features

Modern Bootstrap 5 Design

  • 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

Accessibility

  • Semantic HTML structure
  • Proper label associations
  • Required field indicators
  • ARIA roles for switches

πŸš€ How It Works

Workflow Diagram

Template Document (.tx)
        ↓
[TemplateFieldExtractor]
        ↓
List<TemplateFieldDescriptor>
        ↓
[FormFieldTagHelper]
        ↓
Dynamic HTML Form
        ↓
User Input
        ↓
Form Submission (JSON)
        ↓
[MailMerge Component]
        ↓
PDF Document

Data Flow

  1. Template Loading: TX Text Control loads ContractMergeTemplate.tx
  2. Field Extraction: Form fields are enumerated and converted to DTOs
  3. View Rendering: Tag helper generates HTML for each field
  4. Form Submission: User data is serialized to JSON
  5. Mail Merge: TX Text Control merges JSON data into template
  6. PDF Generation: Document is saved as PDF binary
  7. File Download: Browser receives PDF file

πŸ”‘ Key Technical Decisions

Why Custom Tag Helper?

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 conditions

After (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

Why Synchronous Operations?

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.

Why Path Caching?

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

πŸ“¦ Setup & Installation

Prerequisites

  • .NET 10.0 SDK or later
  • TX Text Control license (place in appropriate directory)
  • Visual Studio 2022 or Visual Studio Code

Installation Steps

  1. Clone the repository

    git clone <repository-url>
    cd tx-html-forms
  2. 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
  3. Add your template

    • Place your .tx template file in App_Data/Templates/
    • Update the filename in HomeController if needed
  4. Build and run

    dotnet restore
    dotnet build
    dotnet run
  5. Open browser

    • Navigate to https://localhost:5001 or the URL shown in console

πŸ§ͺ Testing

Manual Testing

  1. Navigate to the home page
  2. Fill out the dynamically generated form
  3. Click "Generate PDF Document"
  4. Verify the PDF contains merged data

Sample Template Requirements

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)

πŸ”’ Security Features

  • 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)

πŸ“ˆ Performance Optimizations

  1. Tag Helper: Reduces view rendering overhead
  2. Path Caching: Template path computed once
  3. Switch Expressions: Pattern matching for better performance
  4. Minimal Allocations: Efficient string handling

πŸ› οΈ Customization

Adding New Field Types

  1. Add a new case in TemplateFieldExtractor.Extract()
  2. Add a new case in FormFieldTagHelper.Process()
  3. Add icon mapping in GetFieldIcon()
  4. Add HTML generation method (e.g., GenerateCustomInput())

Styling Modifications

  • Bootstrap variables can be customized in site.css
  • Tag helper classes can be modified in FormFieldTagHelper.cs
  • Layout changes in _Layout.cshtml

πŸ“ License

This project uses TX Text Control, which requires a commercial license. Ensure compliance with TX Text Control licensing terms.

🀝 Contributing

Contributions are welcome! Areas for enhancement:

  • Client-side validation
  • Field dependencies
  • Conditional field visibility
  • Multi-page forms
  • Custom CSS themes

πŸ“š Resources

πŸ‘€ Author

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors