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

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

Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://www.evget.com/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);
}

}
}

预览效果截图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值