ExcelTechnology
Reason for Setting Font Style and How to Set Font by C#
When we write an article, we may set several subtitles to express what will be introduced in the paragraph. In order to learn clearly about the article, we need to set the font style of headline, subtitles and contents.
Generally speaking, the font size of headline is larger then subtitles and contents. Also, the headline may be set as bold. Also, the sizes of subtitles are larger than contents but smaller than headline.
Besides the font size, we set the font style to show a beautiful layout of the article after printing. I would like to use Times New Roman as the font style. Additionally, in one article, it includes key words. For highlighting it, we set some obvious color for the key words.
After we open a Word document which we need to set the font style, we can find that there are several tools on designing on Tool bar. Also, you can click the Format on Tools menu to meet more requirements.
It is very easy to set font by using the Microsoft Word function. But for programmers, they may need to set the font by programming not set in Word directly.
Programmers often ask questions about font settings on the forums or display their problems when setting font to get a good answer. Also, they can get several methods form forum.
As a junior programmer, I often visit the forums and get one good method on setting font by C#. Now, I want to show the method. In addition, we need to install .Net Framework 2.0 and Spire.Doc to realize the method.
//Create word document
Document document = new Document();
//Create a new secition
Section section = document.AddSection();
//Create a new paragraph
Paragraph paragraph = section.AddParagraph();
//Append Text
String text
= "This paragraph is demo of text font and color. "
+ "The font name of this paragraph is Tahoma. "
+ "The font size of this paragraph is 20. "
+ "The under line style of this paragraph is DotDot. "
+ "The color of this paragraph is Blue. ";
TextRange txtRange = paragraph.AppendText(text);
//Font name
txtRange.CharacterFormat.FontName = "Tahoma";
//Font size
txtRange.CharacterFormat.FontSize = 20;
//Underline
txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;
//Change text color
txtRange.CharacterFormat.TextColor = Color.Blue;
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
