We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Chinese text file is incorrect as docx

When I use DocIO to open a text file containing Chinese characters and resave it as DOCX, the fonts in the Word document are gibberish. 
I opened the text file using the FormatType.Text, and saved it using DOCX.

Is there any way to get DocIO to recognize the Chinese characters? When I open the TXT file with Office Word, it prompts me to choose an encoding, defaulting to Chinese Simplified (GB2312).

I've attached a screen shot showing the original txt in Word, and the DOCX created by DocIO.Save.

Ultimately I need to then convert this document to PDF.

I am using SyncFusion trial v 16.4450.0.42.



Attachment: Chinese_TXT_to_DOCX_1ebb7045.zip

1 Reply

DB Dilli Babu Nandha Gopal Syncfusion Team February 8, 2019 06:18 PM UTC

Hi Sheri, 

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);
 
  
            document.Save("Output.docx"); 
 
Regards, 
Dilli babu 


Loader.
Up arrow icon