How to manage the file encoding using C# and .NET

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 });
        }
    }
}

Download

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

Download


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:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.