学习C#调用OpenXml操作word文档的基本用法(38:学习特殊符号类-2)

1、特殊符号类

  openxml的SymbolChar类用于向Word文档中插入特殊的、非标准字母字符,例如各种字体图标等,其命名空间为DocumentFormat.OpenXml.Wordprocessing。其主要包含以下两个属性:
  1)Font属性:设置包含所需特殊符号的字体名称;
  2)Char属性:设置特殊符号在指定字体中的十六进制字符代码。

  可以在word文档的"插入"->"符号"界面中查看不同字体中的特殊符号的十六进制字符代码,如下图所示:

  SymbolChar类的使用示例及在word文档中的效果如下所示:

csharp 复制代码
run.Append(new Break());
run.Append(new SymbolChar() { Font = "Wingdings", Char = "F0FE" });
run.AppendChild(new Text(" "));
run.Append(new SymbolChar() { Font = "Webdings", Char = "0086" });
run.Append(new Text("符号已插入"));

run.Append(new Break());
run.Append(new SymbolChar() { Font = "Wingdings", Char = "F0FE" });
run.AppendChild(new Text(" "));
run.Append(new SymbolChar() { Font = "Wingdings 2", Char = "0093" });
run.Append(new Text("符号已插入"));
2、回车符

  CarriageReturn类用于在Word文档的文本中强制结束当前行并在下一行继续,类似于键盘上的Enter键作用。CarriageReturn类与Break类的用途类似,前者属于纯粹的回车符(即按下Enter键的效果),而后者是更强大的中断符类,能实现多种中断效果,如分页或分栏。

  CarriageReturn类的使用示例及在word文档中的效果如下所示:

csharp 复制代码
for (int i = 0; i < txtContent.Lines.Length; i++)
{
    Text text = new Text();
    text.Space = SpaceProcessingModeValues.Preserve;
    text.Text = txtContent.Lines[i];
    run.AppendChild(text);

    run.Append(new CarriageReturn());
}

参考文献

1https://github.com/dotnet/Open-XML-SDK

2https://learn.microsoft.com/zh-cn/office/open-xml/open-xml-sdk

3https://learn.microsoft.com/zh-cn/dotnet/api/documentformat.openxml.wordprocessing.style?view=openxml-3.0.1

4https://blog.csdn.net/i042416/article/details/126228816

相关推荐
库拉库拉里4 小时前
Word文档的标题总是自动出现首行缩进怎么办?
c#·word·xhtml
ms365copilot8 小时前
Word Copilot 上新模型 Claude Opus 5
microsoft·word·copilot
金豆呀10 小时前
WPS批量提取Word文档内容生成固定格式Excel表格
word·excel·wps
一只小灿灿1 天前
Word快捷键集合z
word
2501_930707781 天前
使用C#代码将 Word 转换为 EPUB 电子书格式
word
Am-Chestnuts2 天前
国产AI多轮对话归档与多格式导出实践
人工智能·word
chatexcel3 天前
ChatExcel AI Word 升级:多模态读资料、按模板生成、智能排版配图,还能批注修订和联动Excel 复核
人工智能·ai·word·excel·ai写作
Metaphor6923 天前
使用 Python 在 Word 文档中添加或删除文本框
python·word
Sunflower026 天前
如何解决word/WPS word 中表格跳到下一页(上一页有大篇幅空白)
word·wps
AI导出鸭6 天前
如何让deepseek生成word文档 ?「AI 导出鸭」苹果版:从API流式解析到Pages级渲染,硬核攻克公式裂变与表格回流的终极方案。
人工智能·chatgpt·word·cocoa·ai导出鸭