Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 5, 2022 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/c7cafec957d110f5c10270d77195a36b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/c7cafec957d110f5c10270d77195a36b to your computer and use it in GitHub Desktop.
Split Word Documents using C#

Split to Microsoft Word Files using .NET Word API

C# code listed below for splitting Microsoft® Word formats and fully demonstrates how to split by section, page range and page by page. Developers can easily integrate and modify these as of their application requirement.

More details of splitting Word files using .NET Word Library at https://products.aspose.com/words/net/splitter/

A detailed blog post for splitting the document https://blog.aspose.com/2020/08/26/split-ms-word-documents-using-csharp/

For splitter code integration there are few prerequisite listed in installation section below.

Installation

Install from command line as nuget install Aspose.Words or via Package Manager Console of Visual Studio with Install-Package Aspose.Words.

Alternatively, get the offline MSI installer or DLLs in a ZIP file from downloads.

More About C# Word API

Home | Product Page | Docs | Demos | API Reference | Examples | Blog | Search | Free Support | Temporary License

// Open a Word document
Document doc = new Document("document.docx");
// Create and initialize document page splitter
DocumentPageSplitter splitter = new DocumentPageSplitter(doc);
// Get the page range
Document pageDoc = splitter.GetDocumentOfPageRange(3, 6);
pageDoc.Save("splitted.docx");
// Open a Word document
Document doc = new Document("Document.docx");
// Create and initialize the document page splitter
DocumentPageSplitter splitter = new DocumentPageSplitter(doc);
// Save each page as a separate document
for (int page = 1; page <= doc.PageCount; page++)
{
Document pageDoc = splitter.GetDocumentOfPage(page);
pageDoc.Save($"spliteed_{page}.docx");
}
// Open a Word document
Document doc = new Document("document.docx");
for (int i = 0; i < doc.Sections.Count; i++)
{
// Split a document into smaller parts, in this instance split by section
Section section = doc.Sections[i].Clone();
// Create a new document
Document newDoc = new Document();
newDoc.Sections.Clear();
Section newSection = (Section)newDoc.ImportNode(section, true);
newDoc.Sections.Add(newSection);
// Save each section as a separate document
newDoc.Save($"splitted_{i}.docx");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment