C#向word文档插入新段落及隐藏段落的方法
作者:Yesi 时间:2022-04-11 16:29:10
编辑Word文档时,我们有时会突然想增加一段新内容;而将word文档给他人浏览时,有些信息我们是不想让他人看到的。那么如何运用C#编程的方式巧妙地插入或隐藏段落呢?本文将与大家分享一种向Word文档插入新段落及隐藏段落的好方法。
这里使用的是Free Spire.Doc for .NET组件,该组件允许开发人员轻松并灵活地操作Word文档。
向Word文档插入一个新段落的操作步骤
步骤1:新建一个文档并加载现有文档
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx);
步骤2:插入新段落并设置字体格式
Paragraph paraInserted = document.Sections[0].AddParagraph();
TextRange textRange1 = paraInserted.AppendText("向日葵的花语是——太阳、光辉、高傲、忠诚、爱慕、沉默的爱。向日葵又叫望日莲,一个很美的名字");
textRange1.CharacterFormat.TextColor = Color.Blue;
textRange1.CharacterFormat.FontSize = 15;
textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
步骤3:保存文档
document.SaveToFile("result.docx", FileFormat.Docx);
以下是程序运行前后的对比图:
运行前
运行后
隐藏段落的操作步骤
当操作Word文档时,我们可以通过Microsoft Word点击字体对话框来隐藏所选择的文本。请通过如下的屏幕截图来查看Microsoft是如何隐藏文本的:
然而,Free Spire.Doc for .NET可以通过设置CharacterFormat.Hidden的属性来隐藏指定文本或整个段落,下面将为大家介绍详细步骤:
步骤1:新建一个文档并加载现有文档
Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\雏菊.docx", FileFormat.Docx);
步骤2:获取Word文档的第一个section和最后一段
Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[sec.Paragraphs.Count - 1];
步骤3:调用for循环语句来获取最后一段的所有TextRange并将CharacterFormat.Hidden的属性设置为true
for (int i = 0; i < para.ChildObjects.Count;i++)
{
(para.ChildObjects[i] as TextRange).CharacterFormat.Hidden = true;
}
步骤4:保存文档
doc.SaveToFile("result1.docx", FileFormat.Docx);
以下是程序运行前后的对比图:
运行前
运行后
C#完整代码
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace insert_new_paragraph_and_hide
{
class Program
{
static void Main(string[] args)
{ //该部分为插入新段落的代码
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx);
Paragraph paraInserted = document.Sections[0].AddParagraph();
TextRange textRange1 = paraInserted.AppendText("向日葵的花语是——太阳、光辉、高傲、忠诚、爱慕、沉默的爱。向日葵又叫望日莲,一个很美的名字");
textRange1.CharacterFormat.TextColor = Color.Blue;
textRange1.CharacterFormat.FontSize = 15;
textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
document.SaveToFile("result.docx", FileFormat.Docx);
//该部分为隐藏段落的代码
Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\雏菊.docx", FileFormat.Docx);
Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[sec.Paragraphs.Count - 1];
for (int i = 0; i < para.ChildObjects.Count;i++)
{
(para.ChildObjects[i] as TextRange).CharacterFormat.Hidden = true;
}
doc.SaveToFile("result1.docx", FileFormat.Docx);
}
}
}
这是我本次要分享的全部内容,感谢您的浏览。
标签:C#,word,段落
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 中clone()的使用方法
2023-03-31 12:11:37
OpenCV实现反阈值二值化
2022-06-29 16:02:33
![](https://img.aspxhome.com/file/2023/6/76316_0s.jpg)
Spring Boot如何整合FreeMarker模板引擎
2022-09-06 15:49:32
使用SpringBoot获取所有接口的路由
2021-08-29 22:28:34
Java实例讲解动态代理
2023-03-21 22:56:45
在IntelliJ IDEA中使用gulp的方法步骤(图文)
2022-10-12 06:29:08
![](https://img.aspxhome.com/file/2023/0/60770_0s.png)
Java循环队列原理与用法详解
2023-11-13 20:05:36
![](https://img.aspxhome.com/file/2023/4/61574_0s.png)
Docker下搭建一个JAVA Tomcat运行环境的方法
2022-01-13 14:13:29
arthas jprofiler做复杂链路的调用分析
2022-01-15 12:01:25
![](https://img.aspxhome.com/file/2023/6/78956_0s.png)
C#调用FFmpeg操作音视频的实现示例
2021-10-08 12:40:00
解决idea check out 切换分支时找不到需要的分支问题
2023-04-04 09:12:57
![](https://img.aspxhome.com/file/2023/3/65743_0s.jpg)
IntelliJ IDEA基于SpringBoot如何搭建SSM开发环境的步骤详解
2022-11-24 12:10:39
![](https://img.aspxhome.com/file/2023/9/79349_0s.png)
Java接口的作用_动力节点Java学院整理
2021-12-23 20:46:14
![](https://img.aspxhome.com/file/2023/0/61570_0s.png)
java 同步、异步、阻塞和非阻塞分析
2022-08-09 03:02:42
![](https://img.aspxhome.com/file/2023/1/70321_0s.png)
Spring Boot整合Redis的完整步骤
2023-06-03 03:21:56
![](https://img.aspxhome.com/file/2023/2/67652_0s.png)
Springboot添加支付接口
2023-06-21 00:10:20
![](https://img.aspxhome.com/file/2023/5/77085_0s.png)
Spring如何使用注解的方式创建bean
2022-01-29 03:45:49
Java的关键字与标识符小结
2023-08-17 08:36:50
![](https://img.aspxhome.com/file/2023/2/72602_0s.png)
Unity UGUI实现简单拖拽图片功能
2022-11-26 19:55:03
![](https://img.aspxhome.com/file/2023/9/77289_0s.jpg)
Unity实现苹果手机Taptic震动
2023-02-15 07:18:47