In modern programming, working with text files is one of the most common tasks. It is especially important to manage the encodings correctly to avoid character display errors, data loss, and compatibility issues. In this article, we will explain how to effectively manage file encoding using C# and .NET, using a powerful tool — SautinSoft Document .NET library.
The encoding defines how a sequence of bytes is interpreted as characters. There are many encodings in the modern world: UTF-8, UTF-16, Windows-1251, ISO-8859-1 and others. Incorrect definition or use of encoding leads to:
Proper encoding management is the key to reliable and correct text applications!
If you are creating solutions where the accuracy of displaying and exchanging text information is important, do not ignore the ability to manage encodings using this library. This will not only increase the stability of your applications, but also save you from a lot of problems related to incorrect processing of text data.
Step-by-step guide:
Complete code
using SautinSoft.Document;
using System.IO;
using System.Text;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Get your free trial key here:
// https://sautinsoft.com/start-for-free/
Unicode();
}
/// <summary>
/// Convert files using different encodings.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/encodings.php
/// </remarks>
static void Unicode()
{
string inpFile = @"..\..\..\example.txt";
string outFile = @"Result.pdf";
// Provides access to an encoding provider for code pages that otherwise are available only in the desktop .NET Framework and .NET
// https://learn.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
DocumentCore dc = DocumentCore.Load(inpFile, new TxtLoadOptions { Encoding = Encoding.GetEncoding(1251)});
dc.Save(outFile);
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}Imports SautinSoft.Document
Imports System.IO
Imports System.Text
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Get your free trial key here:
' https://sautinsoft.com/start-for-free/
Unicode()
End Sub
''' <summary>
''' Convert files using different encodings.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/encodings.php
''' </remarks>
Private Shared Sub Unicode()
Dim inpFile As String = "..\..\..\example.txt"
Dim outFile As String = "Result.pdf"
' Provides access to an encoding provider for code pages that otherwise are available only in the desktop .NET Framework and .NET
' https://learn.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider
System.Text.Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
Dim dc As DocumentCore = DocumentCore.Load(inpFile, New TxtLoadOptions With {.Encoding = System.Text.Encoding.GetEncoding(1251)})
dc.Save(outFile)
' Important for Linux: Install MS Fonts
' sudo apt install ttf-mscorefonts-installer -y
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: