Sometimes we need to insert a symbol or special character in the paragraph. The article is introducing the solution to insert a symbol in Word with c# code. We will use Ä and Ë as the symbol examples to complete the process, with the help of a Word .NET API called Spire.Doc.
First we need to complete the preparatory work:
- Download the latest Spire.Doc and install it on your machine.
- Add the Spire.Doc.dll files as reference.
- Open bin folder and select the three dll files under .NET 4.0.
- Right click property and select properties in its menu.
- Set the target framework as .NET 4.
- Add Spire.Doc as namespace.
Here comes to the explanation of the code.
Step 1: Create an instance of Spire.Doc.Document.
[C#]
Document document = new Document();
Step 2: Add a section.
[C#]
Section section = document.AddSection();
Step 3: Add a paragraph.
[C#]
Paragraph paragraph = section.AddParagraph();
Step 4: Use unicode characters to create symbol Ä.
[C#]
TextRange tr = paragraph.AppendText('\u00c4'.ToString());
In fact, the following step5 and step6 are just the option according to your requirement.
Step 5: Set the color of symbol Ä.
[C#]
tr.CharacterFormat.TextColor = Color.Red;
Step 6: Add symbol Ë.
[C#]
paragraph.AppendText('\u00cb'.ToString());
Step 7: Save to Word file.
[C#]
document.SaveToFile("sample.docx", FileFormat.Docx);
Here is the full code:
[C#]
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertSymbol { class Program { static void Main(string[] args) { Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); TextRange tr = paragraph.AppendText('\u00c4'.ToString()); tr.CharacterFormat.TextColor = Color.Red; paragraph.AppendText('\u00cb'.ToString()); document.SaveToFile("sample.docx", FileFormat.Docx); } } }
Preview the effect screenshot: