DBDilli Babu Nandha Gopal Syncfusion Team February 8, 2019 01:18 PM
On analyzing further on the given input text file which contains Chinese text (Chinese Simplified (GB2312)) and without BOM (Byte Order Mask). In this case Microsoft Word Application automation detect the encoding format and read text properly as like in the below screen shot.
Basically, DocIO properly read the text file when text file exists with BOM otherwise reads the text based on the default encoding format. DocIO does not have support to find encoding format when text file exists without BOM.
To achieve your requirement, you can create a text file with BOM (or) read the text based on the corresponding encoding format by using “StreamReader” instance which is illustrated in the following code example.
// Opens the file as Stream. StreamReader docStream = new StreamReader( new FileStream(@"Chinese.txt", FileMode.Open, FileAccess.Read), Encoding.GetEncoding("GB2312"));
//Writes the text into stream. MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream,new UTF8Encoding());
writer.Write(docStream.ReadToEnd()); writer.Flush(); //Dispose the stream docStream.Dispose(); stream.Position = 0;
//Opens the text file in Word document object. WordDocument document = new WordDocument(stream, Syncfusion.DocIO.FormatType.Txt);
// Create a custom style to preserve chinese text in Word document. WParagraphStyle paragraphStyle = document.Styles.FindByName("Normal") as WParagraphStyle; paragraphStyle.CharacterFormat.Font = new System.Drawing.Font("SimSun", 12);