ExcelTechnology
A Brief Introduction about Word Font and How to Set with VB.NET
Microsoft Word is frequently used because it is powerful on dealing with word information, for example, we often use it to write announcements, papers and so on.
Someone may not set format for Word documents for the documents are draft. However, when we submit the documents or the contents in documents to website or print them, we need to pay attention on format setting, especially word font settings.
Open Microsoft Word, we can find that there are several tools on Toolbar for setting font styles. The frequently used styles include font type, size, color and bold, italic, underline.
Generally speaking, the title is often set as bold and the key words are given in obvious color. If we insert hyperlink, the words which insert link will be underlined automatically and usually, the color will turn in blue.
About font type, I would like to use Times New Roman. Arial is the default font type. Also, you can use other types. There are lots of font types which can be chosen in Microsoft Word.
For common users, they can set format directly. For programmers, they may use C# or VB.Net to operate Word. And I want to introduce a method about using VB.NET to set word font.
Note: .Net Framework and Spire.Doc should be installed.
'Create word document
Dim document_Renamed As New Document()
'Create a new secition
Dim section_Renamed As Section = document_Renamed.AddSection()
'Create a new paragraph
Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
'Append Text
Dim text As String _
= "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. "
Dim txtRange As TextRange = paragraph_Renamed.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_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
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);
