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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Chinese text file is incorrect as docx

Thread ID:

Created:

Updated:

Platform:

Replies:

142419 Feb 1,2019 09:27 PM UTC Feb 8,2019 06:18 PM UTC WinForms 1
loading
Tags: DocIO
Sheri Steeves
Asked On February 1, 2019 09:27 PM UTC

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

Dilli Babu Nandha Gopal [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon