有时我们需要在段落中插入符 或特殊字符。文章介绍了用c#代码在Word中插入符 的解决方案。在名为 Spire.Doc的Word .NET API的帮助下,我们将使用 和 作为符 示例来完成该过程。
首先我们需要完成准备工作:
- 下载最新的 Spire.Doc并将其安装在您的机器上。
- 添加 Spire.Doc.dll 文件作为参考。
- 打开bin文件夹,选择.NET 4.0下的三个dll文件。
- 右键单击属性并在其菜单中选择属性。
- 将目标框架设置为 .NET 4。
- 添加 Spire.Doc 作为命名空间。
这里是代码的解释。
第 1 步:创建 Spire.Doc.Document 的实例。
Document document = new Document();
第 2 步:添加一个部分。
Section section = document.AddSection();
Section section = document.AddSection();
第 3 步:添加一个段落。
Paragraph paragraph = section.AddParagraph();
第 4 步:使用 unicode 字符创建符
TextRange tr = paragraph.AppendText('u00c4'.ToString());
其实下面的step5和step6只是根据你的需求选择。
第 5 步:设置符 的颜色。
tr.CharacterFormat.TextColor = Color.Red;
第 6 步:添加符
paragraph.AppendText('u00cb'.ToString());
第 7 步:保存到 Word 文件。
document.SaveToFile("sample.docx", FileFormat.Docx);
这是完整的代码:
[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);}}}
预览效果截图:

欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询在线客服 ;
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!