Specify encoding when loading documents

This feature is supported for the following file types:

Different encodings use different byte sequences for characters, and opening a file in the wrong encoding leads to incorrect character rendering. To properly render a file, the correct encoding must be specified when opening the file. Another option is converting it to UTF-8 beforehand. By default GroupDocs.Viewer using UTF-8 encoding.

For examle, when you try to open sample-shift-jis.txt file in Notepad the text これはShift_JISエンコード用のサンプルテキストです。 will appear as strange symbols because ANSI is misinterpreting the byte values that are specific to Shift_JIS.

Shift_Jis text file opened in Notepad

To render text file properly you can set encoding using LoadOptions class. In the following code snippet we’ll set Shift_JIS encoding and render the text file to HTML:

using System.Text;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

// Specify an encoding.
LoadOptions loadOptions = new LoadOptions();
loadOptions.Encoding = Encoding.GetEncoding("shift_jis");
// Render a file.
using (Viewer viewer = new Viewer("sample-shift-jis.txt", loadOptions))
{
    HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources();
    viewer.View(viewOptions);
}
Imports System.Text
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        ' Specify an encoding.
        Dim loadOptions As LoadOptions = New LoadOptions()
        loadOptions.Encoding = Encoding.GetEncoding("shift_jis")
        ' Render a file.
        Using viewer As Viewer = New Viewer("sample-shift-jis.txt", loadOptions)
            Dim viewOptions As HtmlViewOptions = HtmlViewOptions.ForEmbeddedResources()
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample-shift-jis.txt is sample TXT file that is used in this example. Click here to download it.

The following image demonstrates the result:

Shift_Jis text file converted to HTML