Products Technologies Demo Docs Blog Support Company

How to Create and Deploy PDF Forms in ASP.NET Core C#

TX Text Control can be used for the creation and editing of form fields in PDF documents. This article explains how to create a PDF form from scratch and how to deploy the form using the Document Viewer.

How to Create and Deploy PDF Forms in ASP.NET Core C#

TX Text Control offers extensive functionality to create and deploy PDF forms, including form text boxes, checkboxes, and combo boxes.

This article describes how to use TX Text Control to create PDF documents with form fields and how to deploy the document using Document Viewer with the PDF.js rendering option.

It uses a simple ASP.NET Core Web App (MVC) to create a document that uses the ServerTextControl class to create the PDF form fields.

Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 6 (Long-term support) as the Framework and confirm with Create.

Adding the NuGet Packages

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Package Source

    Select either Text Control Offline Packages or nuget.org as the Package source. Packages in the official Text Control NuGet profile are frequently updated.

    Install the latest versions of the following packages:

    • TXTextControl.Web.DocumentViewer
    • TXTextControl.TextControl.ASP.SDK

    ASP.NET Core Web Application

  2. Switch the package source to nuget.org and choose Installed to check for available updates. Update the installed packages to the most current version.

    ASP.NET Core Web Application

Adding PDF.js

  1. In the Solution Explorer, select your created project and choose Add -> Client-Side Library... from the right-click context menu.

  2. Search for pdf.js by typing it into the Library text box and confirm with Install.

    ASP.NET Core Web Application

Adding the Document Viewer

  1. Find the Index.cshtml file in the Views -> Home folder. Replace the complete content with the following code:

    @model byte[]
    
    @using TXTextControl.Web.MVC.DocumentViewer
    @using System.Text
                    
    <div style="width: 800px; height: 600px;">
                
    @Html.TXTextControl().DocumentViewer(settings => {
            settings.DocumentData = Convert.ToBase64String(Model);
            settings.Dock = DocumentViewerSettings.DockStyle.Fill;
            settings.DocumentLoadSettings.PDFJS.BasePath = "/lib/pdf.js/";
    }).Render()
                
    </div>

    This code adds the DocumentViewer to the view and loads the PDF document that has been created from the model.

Creating the PDF Form

  1. Find the HomeController.cs and replace the Index() method with the following code:

    public IActionResult Index()
    {
            byte[] bDocument = null;
    
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                    tx.Create();
    
                    tx.Selection.FontSize = 800;
                    tx.Selection.Text = "Sample Form\r\n\r\n";
    
                    tx.Selection.FontSize = 400;
                    tx.Selection.Text = "Company name:\r\n";
    
                    // add a text form field
                    TXTextControl.TextFormField textFormField = new TXTextControl.TextFormField(1000);
                    textFormField.Name = "company_name";
                    textFormField.Text = "Text Control, LLC";
                    tx.FormFields.Add(textFormField);
    
                    tx.Selection.Text = "\r\n\r\nCountry:\r\n";
    
                    // add a text form field
                    TXTextControl.SelectionFormField selectionFormField = new TXTextControl.SelectionFormField(1000);
                    selectionFormField.Name = "company_country";
                    selectionFormField.Items = new string[] { "United States", "Germany", "Italy" };
                    selectionFormField.Editable = true;
    
                    tx.FormFields.Add(selectionFormField);
    
                    tx.InputPosition = new TXTextControl.InputPosition(-1, TXTextControl.TextFieldPosition.OutsideTextField);
    
                    tx.Selection.Text = "\r\n\r\nTaxable:\r\n";
    
                    // add a text form field
                    TXTextControl.CheckFormField checkFormField = new TXTextControl.CheckFormField(false);
                    checkFormField.Name = "company_taxable";
                    checkFormField.CheckedCharacter = 'X';
                    checkFormField.UncheckedCharacter = 'O';
                    tx.FormFields.Add(checkFormField);
    
                    // save in the internal format
                    tx.Save(out bDocument, TXTextControl.BinaryStreamType.AdobePDF);
                    tx.Save("test.pdf", TXTextControl.StreamType.AdobePDF);
    
            }
    
            return View(bDocument);
    }

Compile and start the application.

ASP.NET Core Web Application

Learn More

Read this article to learn how to pre-select form fields or flatten completed forms.

Create, Pre-Select, Flatten and Extract PDF Form Fields using C#

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETAcroFormsForms

How to Load and View PDF Documents in ASP.NET Core C#

A very typical task in business applications is to view PDF documents in web applications. This article describes how to use the TX Text Control Document Viewer to display PDF documents in ASP.NET…


ASP.NETASP.NET CoreForms

Streamline Data Collection with Embedded Forms in C# .NET

Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…


ASP.NETASP.NET CoreForms

Convert MS Word DOCX to PDF with Form Fields in C# .NET: Preserve or Flatten…

TX Text Control converts DOCX to PDF while preserving or flattening form fields, ensuring accurate import and export. Its flexible API simplifies form field handling for seamless document processing.


ASP.NETASP.NET CoreForms

Export and Import Adobe PDF XFDF XML Files in .NET C#

This article shows how to export and import Adobe PDF XFDF XML files in .NET C# using the TX Text Control .NET Server component. Form fields from PDF documents are exported to XFDF XML files and…


ASP.NETASP.NET CoreDOCX

Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in…

This article shows how to create a self-signed digital ID using Adobe Acrobat Reader and how to use it to sign documents in .NET C#. The article also shows how to create a PDF document with a…