We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problems with displaying japanese characters in word document

OH Great ASPOSE Technical Support,

You've always helped in the past, so I figure you can help me again.

In my application, I get data from a sql 2000 database table. The column I am interested in is defined as nvarchar(2000). The records can be in any language, including japanese.

a code snippet is provided below - i found the font stuff in another post, but after the code runs, the japanese strings still look like garbage. I've attached part of a document that shows what I am talking about. I've also attached an xml file I created from one of the datatables that is passed into the template (although it probably doesn't correspond to the sample text document I've attached - but it does show how the japanese strings are being passed in). Can you point me in the right direction to get the japanese characters printed correctly?;

templatePath = Path.Combine(strTemplatePath, "QuestionTextTemplateTextResponseQuestion.doc");

dtQTs.Clear();

dtQTs = getSurveyQuestionTemplateIDs(iSurveyID, 0);

foreach (DataRow drQT in dtQTs.Rows)

{

iQuestionTemplateID = int.Parse(drQT["QuestionTemplateID"].ToString());

Document doc = new Document(templatePath);

DataTable dtQuestions = getSurveyQuestionScoreTextTable(iSurveyID, iQuestionTemplateID, strLanguageCode, sStratFilter);

dtQuestions.TableName = "Questions";

doc.MailMerge.ExecuteWithRegions(dtQuestions);

dtQuestions.Clear();

dtQuestions = getSurveyQuestionTextResponseTable(iSurveyID, iQuestionTemplateID, strLanguageCode, sStratFilter);

dtQuestions.TableName = "TextResponse";

doc.MailMerge.ExecuteWithRegions(dtQuestions);

doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

AppendDoc(docRCSReport, doc);

}

foreach (Run run in docRCSReport.GetChildNodes(NodeType.Run, true))

{

run.Font.NameFarEast = "Arial Unicode MS";

run.Font.LocaleIdFarEast = 1041;

}

docRCSReport.Save(strFileName);

bReportCreated = true;

Hi

Thanks for your request. Unfortunately, data you have attached looks bad on my side (<ResultText>‚ ‚è‚Ü‚¹‚ñB</ResultText>). I have tried to use the following code and it seems that all works fine on my side.

public void TestMailMerge_100529()

{

Document doc = new Document(@"313_100529_dchillman\in.doc");

string[] names = { "ResultText" };

string[] values = { "你好世界" };

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_100529);

doc.MailMerge.Execute(names, values);

doc.Save(@"313_100529_dchillman\out.doc");

}

void MailMerge_MergeField_100529(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "ResultText")

{

e.Field.Start.ParentParagraph.ParagraphFormat.Style.Font.NameFarEast = "SimSun";

e.Field.Start.ParentParagraph.ParagraphFormat.Style.Font.LocaleIdFarEast = 2052;

}

}

Make sure that you get a correct data from your data base.

Also please attach your template.

Best regards.

i was afraid you were going to say something like that. All I know is that the data in the database field is unicode and it is supposed to be Japanese. I am just doing a simple select from the database field and passing that to the template. Since you are a wealth of knowledge, do you know if I am supposed to be doing something other than a simple select?

Hi

Can you do something like this on your side?

Document doc = new Document();

DataTable dtQuestions = getSurveyQuestionScoreTextTable(iSurveyID, iQuestionTemplateID, strLanguageCode, sStratFilter);

DocumentBuilder builder = new DocumentBuilder(doc);

doc.Save(“Out.doc”);

///

/// Insert Table to document

///

public void InsertTable(DocumentBuilder builder, DataTable table)

{

builder.StartTable();

foreach (DataRow row in table.Rows)

{

foreach (object cell in row.ItemArray)

{

builder.InsertCell();

builder.Font.NameFarEast = "Arial Unicode MS";

builder.Font.LocaleIdFarEast = 1041;

builder.Write(cell.ToString());

}

builder.EndRow();

}

builder.EndTable();

}

And then attach the generated document here.

Best regards.

I have just been thrown back into this project. I think this must be the first time that the production system had to actually handle Japanese characters. A file is created from database records. Some characters in the file are Japanese. Sometimes when the resulting document is opened in Word the Japanese characters are displayed correctly and sometimes they are not. I have one dev machine (windows xp) that just shows a bunch of block characters and one that displays them correctly (windows 7). My client only has machines that display them incorrectly. I've checked my machines and they have international fonts installed and support for far eastern fonts. On my dev machine (windows xp) that doesn't display correctly, if I manually select the Japanese text and change the font to MS Mincho or SimSun or MS Arial Unicode, it is displayed correctly.

My problem is that I need it to always display correctly. I've tried using some code to force the font but the problem remains. I've attached a sample document. Any ideas? thanks

if (e.FieldValue.ToString().StartsWith("Language:"))

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

e.Text = string.Empty;

builder.MoveToField(e.Field, false);

builder.ListFormat.RemoveNumbers();

builder.Font.Underline = Underline.Single;

if (e.FieldValue.ToString().Contains("JA-JP") == true)

{

builder.Font.NameFarEast = "Arial Unicode MS";

builder.Font.LocaleIdFarEast = 1041;

}

builder.Writeln(e.FieldValue.ToString());

}

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Maybe in your case you can try specifying alternative font for whole document. For example, please try using the following code:

AlternativeFontChanger changer = new AlternativeFontChanger("Arial Unicode MS");

doc.Accept(changer);

====================================================================

using Aspose.Words;

using Aspose.Words.Fields;

///

/// Class inherited from DocumentVisitor, that chnges fornt of each node to font specified in the constructor

///

class AlternativeFontChanger : DocumentVisitor

{

public AlternativeFontChanger(string fontName)

{

mFontName = fontName;

}

///

/// Called when a FieldEnd node is encountered in the document.

///

public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)

{

//Simply change font name

fieldEnd.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a FieldSeparator node is encountered in the document.

///

public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)

{

fieldSeparator.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a FieldStart node is encountered in the document.

///

public override VisitorAction VisitFieldStart(FieldStart fieldStart)

{

fieldStart.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a Footnote end is encountered in the document.

///

public override VisitorAction VisitFootnoteEnd(Footnote footnote)

{

footnote.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a FormField node is encountered in the document.

///

public override VisitorAction VisitFormField(FormField formField)

{

formField.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a Paragraph end is encountered in the document.

///

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)

{

paragraph.ParagraphBreakFont.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a Run node is encountered in the document.

///

public override VisitorAction VisitRun(Run run)

{

run.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

///

/// Called when a SpecialChar is encountered in the document.

///

public override VisitorAction VisitSpecialChar(SpecialChar specialChar)

{

specialChar.Font.NameFarEast = mFontName;

return VisitorAction.Continue;

}

//Font by default

private readonly string mFontName = "Arial Unicode MS";

}

Please let me know if this helps.

Also, the problem might occur because different version of fonts are installed on your testing machines.

Best regards.