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

Free Support Forum - aspose.com

Smart font selection

Hi,

I’m facing an issue with unicode characters while using Aspose. The default font selection in my document is Arial. In that font some of the unicode characters are not visible, it comes as boxes. Here is the code

Document doc = new Document(@“D:\temp1\test1.doc”);
DocumentBuilder builder = new DocumentBuilder(doc);
StreamReader r=new StreamReader(@“D:\temp1\different texts.txt”);
string a = r.ReadToEnd();
builder.InsertHtml(a);
doc.Save(“d://temp1//generated.doc”);

I’m also attaching the generated document. If I apply Arial Unicode MS font to this text it becomes visible. In MS Word if I paste these unicode characters it automatically selects the appropriate font. Can something like this be done with InsertHtml? What I want is if the text is normal text it should come as Arial but if its some other language characters then it should select Arial Unicode MS font. Is this possible?

Thanks

Hi

Thanks for your request. I am not sure you need InsertHtml method in your case. Your text does not contain any HML formatting. So I think in your case you should use simple Write method. In this case, you can easily specify default and alternative fonts:

string text = File.ReadAllText(@"Test001\different+texts.txt");

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Specify default and alternative fonts.

builder.Font.Name = "Arial";

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

builder.Write(text);

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards,

thanks for your reply.
I was just giving an example, I want this to work with InsertHtml but it doesn’t seem to work. I’m thinking about using find-replace as given here: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-find-and-highlight-text.html to apply font to unicode characters based on their character code

thanks

Hi

Thanks for your request. I think, in your case, you can simply specify Arial Unicode font as an alternative font for all runs in your document. For example, you can use DocumentVisitor to achieve this:

[Test]

public void Test001()

{

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

doc.Accept(new FontChanger());

doc.Save(@"Test001\out.doc");

}

using Aspose.Words;

using Aspose.Words.Fields;

///

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

///

class FontChanger : DocumentVisitor

{

public FontChanger()

{

}

///

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

///

public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)

{

//Simply change font name

ResetFont(fieldEnd.Font);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)

{

ResetFont(fieldSeparator.Font);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitFieldStart(FieldStart fieldStart)

{

ResetFont(fieldStart.Font);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitFootnoteEnd(Footnote footnote)

{

ResetFont(footnote.Font);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitFormField(FormField formField)

{

ResetFont(formField.Font);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)

{

ResetFont(paragraph.ParagraphBreakFont);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitRun(Run run)

{

ResetFont(run.Font);

return VisitorAction.Continue;

}

///

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

///

public override VisitorAction VisitSpecialChar(SpecialChar specialChar)

{

ResetFont(specialChar.Font);

return VisitorAction.Continue;

}

private void ResetFont(Font font)

{

font.NameOther = mNewFont;

}

//Font by default

private string mNewFont = "Arial Unicode MS";

}

Best regards,

hi
thanks for your response.
the code you provided does solve the issue but it creates another issue with background color of paragraph. Arial Unicode MS font’s height seems to be greater than the characters. So the background color of the paragraph becomes a little thicker (in height). I’m attaching a image with an example. For lines without any special characters the line height is getting increased without any reason. Even this is happening for blank lines (I tried commenting the call to ResetFont in VisitParagraphEnd for that but it didn’t work). Is there a solution for this?

Thanks

Hi

Thanks for your request. Could you please attach your document here for testing? We will check the issue and provide you more information.

Best regards,

hi
thanks for your reply
I’m attaching a document which doesn’t have the problem but if you apply the visitor then if you check the last page then you’ll be able to see the difference (I’ve attached a document after applying the visitor as well).
This problem is only visible in MS Word 2007(and above I guess).

Hi

Thank you for additional information. Yes, I see the problem. It seems Arial Unicode MS glyph’s height is bigger than height of glyphs in Arial font. That is why there is a difference in your output document. In your case, there are non-breaking spaces. To resolve the problem you can try replacing these non-breaking spaces with simply whitespaces.

Best regards,

Thanks Alexey,

You are right I checked and this is due to multiple consecutive spaces (I hope non-breaking space is the same as multiple spaces together). But I cannot remove them as they are needed. Also blank lines also have this problem. Is there any solution?

Thanks again for your help.

Hi

Thanks for your request. I am afraid this is a side effect that you cannot control because it is due to difference in fonts.

Best regards,