本文将介绍如何使用Spire.Doc删除Word文档中的指定段落以及所有段落。
源文档截图如下:
删除指定段落
C#
//创建Document实例
Document document = new Document();
//加载Word文档
document.LoadFromFile("Input.docx");
//删除文档中第一节的第一个段落
document.Sections[0].Paragraphs.RemoveAt(0);
//保存文档
document.SaveToFile("RemoveParagraph.docx", FileFormat.Docx2013);
VB.NET
'创建Document实例
Dim document As Document = New Document
'加载Word文档
document.LoadFromFile("Input.docx")
'删除文档中第一节的第一个段落
document.Sections(0).Paragraphs.RemoveAt(0)
'保存文档
document.SaveToFile("RemoveParagraph.docx", FileFormat.Docx2013)
删除所有段落
C#
//创建Document实例
Document document = new Document();
//加载Word文档
document.LoadFromFile("Input.docx");
//遍历文档中的节,删除其中的所有段落
foreach(Section section in document.Sections)
{
section.Paragraphs.Clear();
}
//保存文档
document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013);
VB.NET
'创建Document实例
Dim document As Document = New Document
'加载Word文档
document.LoadFromFile("Input.docx")
'遍历文档中的节,删除其中的所有段落
For Each section As Section In document.Sections
section.Paragraphs.Clear
Next
'保存文档
document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013)