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 example, if you open sample-shift-jis.txt in Notepad with the ANSI encoding, its Japanese text, which starts with 納品に関するご案内 (“Delivery information”), appears as strange symbols, because ANSI misinterprets the byte values that are specific to Shift_JIS.
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:
usingSystem.Text;usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...// .NET 6 and later need this once to know code-page encodings such as Shift_JIS// (.NET Framework knows them already).Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);// Specify an encoding.LoadOptionsloadOptions=newLoadOptions();loadOptions.Encoding=Encoding.GetEncoding("shift_jis");// Render a file.using(Viewerviewer=newViewer("sample-shift-jis.txt",loadOptions)){HtmlViewOptionsviewOptions=HtmlViewOptions.ForEmbeddedResources();viewer.View(viewOptions);}
ImportsSystem.TextImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())' .NET 6 and later need this once to know code-page encodings such as Shift_JIS
' (.NET Framework knows them already).
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)' Specify an encoding.
DimloadOptionsAsLoadOptions=NewLoadOptions()loadOptions.Encoding=Encoding.GetEncoding("shift_jis")' Render a file.
UsingviewerAsViewer=NewViewer("sample-shift-jis.txt",loadOptions)DimviewOptionsAsHtmlViewOptions=HtmlViewOptions.ForEmbeddedResources()viewer.View(viewOptions)EndUsingEndSubEndModule
sample-shift-jis.txt is the sample file used in this example. Click here to download it.