在 Word 文档中,用户可以添加新的分页符或删除现有的分页符。此示例显示如何使用 Spire.Doc 从 word 文档中删除分页符。Spire.Doc支持从.docx、.doc、RTF等格式的word文档中去除分页符。
Spire.Doc 最新下载/product/3368/download
首先确保Spire.Doc for .NET已正确安装,然后通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“.. \Spire.Doc\Bin\NET4.0\ Spire.Doc .dll”。以下是如何在 C# 中删除分页符的详细信息。
//Create a new word document and load from the file.Document document = new Document();document.LoadFromFile("sample.docx");// Traverse every paragraph of the first section of the documentfor (int j = 0; j < document.Sections[0].Paragraphs.Count; j++){Paragraph p = document.Sections[0].Paragraphs[j];// Traverse every child object of a paragraphfor (int i = 0; i < p.ChildObjects.Count; i++){DocumentObject obj = p.ChildObjects[i];//Find the page break objectif (obj.DocumentObjectType == DocumentObjectType.Break){Break b = obj as Break;// Remove the page break object from paragraphp.ChildObjects.Remove(b);//save the document to file.document.SaveToFile("result.docx");
请查看有效截图:
完整代码:
using Spire.Doc;using Spire.Doc.Documents;namespace RemovePageBreak{class Program{static void Main(string[] args){Document document = new Document();document.LoadFromFile("sample.docx", FileFormat.Docx);for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++){Paragraph p = document.Sections[0].Paragraphs[j];for (int i = 0; i < p.ChildObjects.Count; i++){DocumentObject obj = p.ChildObjects[i];if (obj.DocumentObjectType == DocumentObjectType.Break){Break b = obj as Break;p.ChildObjects.Remove(b);}}}document.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");}}}