700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Word控件Spire.Doc 【文本】教程(4) ;C#如何在Word中插入符号

Word控件Spire.Doc 【文本】教程(4) ;C#如何在Word中插入符号

时间:2022-01-23 18:45:23

相关推荐

Word控件Spire.Doc 【文本】教程(4) ;C#如何在Word中插入符号

有时我们需要在段落中插入符号或特殊字符。文章介绍了用c#代码在Word中插入符号的解决方案。在名为 Spire.Doc的Word .NET API的帮助下,我们将使用 Ä 和 Ë 作为符号示例来完成该过程。

Spire.Doc 最新下载/product/3368/download

首先我们需要完成准备工作:

下载最新的 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);}}}

预览效果截图

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。