Word允许通过插入符号操作来向文档中添加一些键盘上没有的特殊符号,本文将介绍如何使用Spire.Doc来进行同样的操作。
C#
//实例化一个Document对象
Document doc = new Document();
//向文档中添加一个Section对象
Section sec = doc.AddSection();
//在这个section上添加一个段落
Paragraph p = sec.AddParagraph();
p.AppendText("这是一个打勾的复选框:");
//在段落之后追加一个打勾的复选框,这个符号的十六进制是"\u0052",
//也可以用十进制(char)82).ToString()来表示它。
//TextRange tr=p.AppendText("\u0052");
TextRange tr= p.AppendText(((char)82).ToString());
//设置字体,可以在word中查看对应的符号是什么字体,这里打勾的复选框是Wingdings2字体
tr.CharacterFormat.FontName = "Wingdings 2";
//添加一个新的段落,并添加一个邮件图标
Paragraph p1 = sec.AddParagraph();
p1.AppendText("这是一个邮件图标:");
//TextRange tr1 = p1.AppendText("\u002A");
TextRange tr1 = p1.AppendText(((char)42).ToString());
tr1.CharacterFormat.FontName = "Wingdings";
//添加一个新的段落,并添加一个笑脸符号
Paragraph p2 = sec.AddParagraph();
p2.AppendText("这是一个笑脸符号:");
//TextRange tr2 = p2.AppendText("\u004A");
TextRange tr2 = p2.AppendText(((char)74).ToString());
tr2.CharacterFormat.FontName = "Wingdings";
//保存文档
doc.SaveToFile("添加特殊符号.docx");
VB.NET
'实例化一个Document对象 Dim doc As Document = New Document() '向文档中添加一个Section对象
Dim sec As Section = doc.AddSection()
'在这个section上添加一个段落 Dim p As Paragraph = sec.AddParagraph() p.AppendText(""这是一个打勾的复选框:"") '实例化一个Document对象
Dim doc As Document = New Document()
'向文档中添加一个Section对象 Dim sec As Section = doc.AddSection() '在这个section上添加一个段落
Dim p As Paragraph = sec.AddParagraph()
p.AppendText("这是一个打勾的复选框: ")
'在段落之后追加一个打勾的复选框,这个符号的十六进制是"\u0052",也可以用十进制(char)82).ToString()来表示它 'Dim tr As TextRange = p.AppendText("\u0052")
Dim tr As TextRange = p.AppendText(ChrW(82).ToString())
'设置字体,可以在word中查看对应的符号是什么字体,这里打勾的复选框是Wingdings2字体 tr.CharacterFormat.FontName = "Wingdings 2" '添加一个新的段落,并添加一个邮件图标
Dim p1 As Paragraph = sec.AddParagraph()
p1.AppendText("这是一个邮件图标: ")
'Dim tr1 As TextRange = p1.AppendText("\u002A") Dim tr1 As TextRange = p.AppendText(ChrW(42).ToString()) tr1.CharacterFormat.FontName = "Wingdings" '添加一个新的段落,并添加一个笑脸符号
Dim p2 As Paragraph = sec.AddParagraph()
p2.AppendText("这是一个笑脸符号: ")
'Dim tr2 As TextRange = p2.AppendText("\u004A") Dim tr2 As TextRange = p.AppendText(ChrW(74).ToString()) tr2.CharacterFormat.FontName = "Wingdings" '保存文档
doc.SaveToFile("添加特殊符号.docx")
效果图如下

删除特殊符号
C#
// 实例化一个Document对象
Document document = new Document();
document.LoadFromFile("特殊符号.docx");
// 用FindString方法找到要删除的邮件图标
TextRange tr = document.FindString("\u002A", true, true).GetAsOneRange();
// 定位到这个TextRange所在的段落然后删除这个TextRange
Paragraph p = tr.OwnerParagraph;
p.ChildObjects.Remove(tr);
// 保存文档
document.SaveToFile("test.docx", FileFormat.Docx2013);
8. VB代码中 Dim tr As TextRange = doc.FindString("\u002A", true, true).GetAsOneRange 改为 Dim tr As TextRange = doc.FindString("\u002A", true, true).GetAsOneRange()
VB.NET
'实例化一个Document对象 Dim doc As Document = New Document '加载文档
doc.LoadFromFile("特殊符号.docx")
'用FindString方法找到要删除的邮件图标 Dim tr As TextRange = doc.FindString("\u002A",true,true).GetAsOneRange '定位到这个TextRange所在的段落然后删除这个TextRange
Dim p As Paragraph = tr.OwnerParagraph
p.ChildObjects.Remove(tr)
'保存文档 doc.SaveToFile("test.docx",FileFormat.Docx2013)效果图如下








