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

1、分隔线

  openxml中的SeparatorMark类及ContinuationSeparatorMark类用于生成一条分隔主文档内容和注释内容的水平分隔线,前者的长度短一些,后者贯穿整个页面宽度。这两个类的命名空间为DocumentFormat.OpenXml.Wordprocessing。
  注意,虽然这两个类可以向本文中插入水平分割线(如下面示例代码及word文档效果所示),但通常它们用于Word文档的脚注或尾注区域,在文档正文中使用这两个类插入横线从语义和规范上都不正确,需要插入横线时,应使用段落边框(BottomBorder)或 Drawing类。

csharp 复制代码
run.Append(new Break());
run.Append(new SeparatorMark());

run.Append(new Break());
run.Append(new ContinuationSeparatorMark());
2、连字符

  openxml的SoftHyphen类,命名空间DocumentFormat.OpenXml.Wordprocessing,用于表示可选连字符,它在单词中标记一个仅在行末作为断字点时才显示为普通连字符"-" 的位置,在其他情况下不占用字符宽度,处于隐藏状态。

  NoBreakHyphen类,命名空间DocumentFormat.OpenXml.Wordprocessing,用于表示非断字连字符,它与普通连字符(-)视觉上完全相同,区别在于其强制使其两侧的文本被视作一个不可分割的整体,从而防止在此处自动换行,常用于确保特定词组或数字序列保持完整。

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

csharp 复制代码
run.Append(new Break());
run.AppendChild(new Text("可选连字符示例:This is the word line"));
run.AppendChild(new SoftHyphen()); 
run.AppendChild(new Text("breaking in a sentence."));

run.Append(new Break());
run.AppendChild(new Text("非断字连字符示例:客服电话:123"));
run.AppendChild(new NoBreakHyphen()); 
run.AppendChild(new Text("4567"));  
3、制表符

  openxml的TabChar类,命名空间为DocumentFormat.OpenXml.Wordprocessing,用于向Word文档插入一个制表符(Tab),精确控制文本的水平位置,可用来实现对齐、缩进或构建类似表格的排版效果,类似于在word文档中按键盘上的Tab键。

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

csharp 复制代码
run.Append(new Text("---------------------------------------------"));

if (rbShortDate.Checked)
{
    run.Append(new Break());
    run.Append(new TabChar());
    run.Append(new TabChar());
    run.Append(new TabChar());
    run.Append(new TabChar());
    run.Append(new TabChar());
    run.Append(new Text("日期:"));
    run.Append(new YearShort());
    run.Append(new Text("-"));
    run.Append(new MonthShort());
    run.Append(new Text("-"));
    run.Append(new DayShort());
}

参考文献

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

相关推荐
gc_229910 小时前
学习C#调用OpenXml操作word文档的基本用法(37:学习复杂字段相关类)
word·openxml·复杂字段
蝈蝈噶蝈蝈噶1 天前
poi-tl填充柱状图折线图无法指定y坐标轴导致重复数据
java·word
gc_22991 天前
学习C#调用OpenXml操作word文档的基本用法(35:学习图形类-5)
word·openxml·图表·drawing
weixin_416660071 天前
Gemini 里的公式怎么转到 Word?保留公式、表格和排版的方法
word
诸葛大钢铁1 天前
知网CAJ格式文件如何转为Word/PDF?CAJ转Word的三个免费方法
pdf·word·知网·caj·caj转word
gc_22992 天前
学习C#调用OpenXml操作word文档的基本用法(34:学习图形类-4)
word·openxml·anchor·浮动式布局
yuhulkjv3352 天前
腾讯元宝公式粘贴word乱码
人工智能·chatgpt·word·deepseek·ai导出鸭
hef2883 天前
Java读取Word图片坐标的两种方法
java·开发语言·word
OEC小胖胖3 天前
ChatGPT导出Word怎么做?Chat2File 安装与使用教程
chatgpt·word·效率工具·ai工具·浏览器扩展