Processing barcodes and drawings in documents in .NET 6 console applications requires the project to load Windows Forms assemblies. This article shows how to create a new .NET 6 console application that is able to process drawings and barcodes.
The settings applied in this tutorial is not only valid for .NET 6 console applications, but ASP.NET Core web applications, too. The UseWindowsForms flag can be used in the same way in ASP.NET Core project files.
Creating the Project
In Visual Studio, create a new project and choose Console App from the project templates.
In the next step, choose a name for your application and continue with Next.
Select .NET 6.0 (Long-term support) and confirm with Create.
Adding Required Packages
In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.
Browse for System.Drawing.Common and Install the latest version of the System.Drawing.Common package.
Adding TX Text Control
While the project is selected in the Solution Explorer, choose Project -> Add Project Reference... to open the Reference Manager. In the opened dialog, select Browse... to select the required TX Text Control assemblies. Navigate to the installation folder of TX Text Control and select the following assemblies from the Assembly folder:
Repeat this step with the following assemblies from the Assembly/bin64 folder:
After selecting these assemblies, close the Reference Manager by confirming with OK.
While the project is selected in the Solution Explorer, choose Project -> Add New Item.... Select Text File, name the file licenses.licx and close the dialog by clicking Add.
Open the newly added file and add the following content:This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
TXTextControl.ServerTextControl, TXTextControl.Server, Version=30.0.1500.500, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638
Set the Build Action property to Embedded Resource.
Select the project in the Solution Explorer and choose Edit Project File from the Project main menu. Find the PropertyGroup entry and replace the whole node with the following code:This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <UseWindowsForms>true</UseWindowsForms> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization> <EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>true</EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization> </PropertyGroup>
The above setting is required to enable the obsolete .NET BinaryFormatter while compiling the license into the assembly. We are working with Microsoft to avoid this requirement in the near future.
In the above project settings, the UseWindowsForms flag is enabling the application to load Windows Forms assemblies. In order to use this flag, the Target OS must be set to Windows which is done through the TargetFramework element in the project XML.
Now, the console application is prepared to use barcodes and drawings in your ServerTextControl documents.