This article shows how to create a .NET 8 console application on Linux using Docker and WSL that uses the ServerTextControl to create a document and MailMerge to merge JSON data into the document. The document is saved as a PDF file.
Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 8 SDK.
1. In Visual Studio 2022, create a new project by choosing Create a new project.
2. Select Console App as the project template and confirm with Next.
3. Enter a project name and choose a location to save the project. Confirm with Next.
4. Choose .NET 8.0 (Long Term Support) as the Framework.
5. Enable the Enable container support checkbox and choose Linux as the Container OS.
6. Choose Dockerfile for the Container build type option and confirm with Create.
7. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project menu.
Install the following package:
8. Find the Program.cs file in the Solution Explorer and replace the code with the provided snippet.
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
// Create a new ServerTextControl instance
tx.Create();
// Adding static text with formatting
TXTextControl.Selection sel = new TXTextControl.Selection
{
Text = "Welcome to Text Control
", // Static welcome message
Bold = true // Apply bold formatting
};
tx.Selection = sel;
// Adding a merge field for dynamic data insertion
TXTextControl.DocumentServer.Fields.MergeField mergeField = new TXTextControl.DocumentServer.Fields.MergeField
{
Text = "{{company}}", // Placeholder text in the document
Name = "company", // Internal name for the merge field
TextBefore = "Company name: " // Prefix text before the field
};
// Add the merge field to the document
tx.ApplicationFields.Add(mergeField.ApplicationField);
// ---- Optionally load a template ----
// If a predefined document template is available, uncomment the following lines
/*
TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings
{
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord // Preserve Word field format
};
tx.Load("template.docx", TXTextControl.StreamType.WordprocessingML, ls);
*/
// Perform mail merge using JSON data
using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge())
{
mailMerge.TextComponent = tx; // Link MailMerge engine to the text component
// JSON data to be merged into the document
string jsonData = "[{\"company\": \"Text Control, LLC\" }]";
mailMerge.MergeJsonData(jsonData); // Merge the provided JSON data
}
tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
Console.WriteLine("Document created: results.pdf");
}
9. Select Container (Dockerfile) as the startup profile and start the application.
For a faster development experience, you can run the application in WSL (Windows Subsystem for Linux).
Select WSL as the startup profile and start the application.
In case you haven't enabled WSL yet, follow these steps:
To set WSL 2 as the default version, run wsl --set-default-version 2 in PowerShell.