C# 插入、删除段落到Word文档
1、下载安装该类库后,注意添加引用Spire.Doc.dll到程序,dll 文件可在安装路径下的Bin文件夹中获取。

1、【C#】
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertPara_Doc
{
class Program
{
static void Main(string[] args)
{
//实例化Document类,加载文件
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
//获取section,添加新的段落
Paragraph paraInserted = document.Sections[0].AddParagraph();
TextRange textRange1 = paraInserted.AppendText("天文现象火星大冲,即地球和火星与太阳在同一条直线上,"
+ "这一天文现象称为“冲(chong读第四声)日”,简称“冲”。靠得近则为大冲。");
textRange1.CharacterFormat.TextColor = Color.Black;
textRange1.CharacterFormat.FontName="幼圆";
textRange1.CharacterFormat.FontSize = 11;
textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
//添加新的段落到指定位置
document.Sections[0].Paragraphs.Insert(0, document.Sections[0].Paragraphs[document.Sections[0].Paragraphs.Count - 1]);
//保存并打开文档
document.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");
}
}
}
2、完成代码后,生成文档,如下:

1、【C#】
using Spire.Doc;
namespace RemovePara_Doc
{
class Program
{
static void Main(string[] args)
{
//实例化Document类,加载文档
Document document = new Document();
document.LoadFromFile("sample.docx");
//获取指定段落,调用方法RemoveAt()移除
document.Sections[0].Paragraphs.RemoveAt(1);
//保存并打开文档
document.SaveToFile("output.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("output.docx");
}
}
}
2、调试运行程序,生成文件,下面是删除段落的前后效果:

