Skip to content

Commit 8fc9185

Browse files
Optimized the code logic
1 parent bfde40c commit 8fc9185

File tree

5 files changed

+52
-35
lines changed
  • Split-Word-document-by-bookmarks/.NET/Split-Word-document-by-bookmarks/Split-Word-document-by-bookmarks
  • Split-Word-document-by-headings/.NET/Split-Word-document-by-headings/Split-Word-document-by-headings
  • Split-Word-document-by-placeholders/.NET/Split-Word-document-by-placeholders/Split-Word-document-by-placeholders
  • Split-Word-document-by-sections/.NET/Split-Word-document-by-sections/Split-Word-document-by-sections

5 files changed

+52
-35
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# Compare Word documents in C#
1+
# Split Word documents in C#
22

3-
This repository contains examples that illustrates how to split DOCX format document programmatically in C#. The Syncfusion [.NET Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) (DocIO) split Word document with just few lines of code and also it does not require Microsoft Word application to be installed in the machine.
4-
5-
## Key Features
6-
- [Split by Sections](https://help.syncfusion.com/document-processing/word/word-library/net/word-document/split-word-documents#split-by-section?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) - Split the Word document based on sections.
7-
- [Split by Headings](https://help.syncfusion.com/document-processing/word/word-library/net/word-document/split-word-documents#split-by-headings?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) - Split the Word document based on headings.
8-
- [Split by Bookmarks](https://help.syncfusion.com/document-processing/word/word-library/net/word-document/split-word-documents#split-by-bookmark?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) - Split the Word document based on bookmarks.
9-
- [Split by Placeholder text](https://help.syncfusion.com/document-processing/word/word-library/net/word-document/split-word-documents#split-by-placeholder-text?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) - Split the Word document based on placeholder text.
3+
This repository contains examples that illustrates how to split Word document programmatically in C#. The Syncfusion [.NET Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) (DocIO) split Word document with just few lines of code and also it does not require Microsoft Word application to be installed in the machine.
104

115
<p align="center">
126
<img src="Images/Split-Word-document.png" alt="Split Word document in C#"/>
137
</p>
148

9+
## Key Features
10+
- [Split by Sections](Split-Word-document-by-sections/) - Split the Word document based on sections.
11+
- [Split by Headings](Split-Word-document-by-headings/) - Split the Word document based on headings.
12+
- [Split by Bookmarks](Split-Word-document-by-bookmarks/) - Split the Word document based on bookmarks.
13+
- [Split by Placeholder text](Split-Word-document-by-placeholders/) - Split the Word document based on placeholder text.
14+
15+
16+
1517
## Syncfusion .NET Word Library
1618
The Syncfusion DocIO is a [.NET Word library](https://www.syncfusion.com/document-processing/word-framework/net/word-library?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) allows you to add advanced Word document processing functionalities to any .NET application and does not require Microsoft Word application to be installed in the machine. It is a non-UI component that provides a full-fledged document instance model similar to the Microsoft Office COM libraries to iterate with the document elements explicitly and perform necessary manipulation.
1719

18-
Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/docio/getting-started?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), where you can find basic Word document processing options along with the features like [mail merge](https://help.syncfusion.com/file-formats/docio/working-with-mail-merge?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [merge](https://help.syncfusion.com/file-formats/docio/word-document/merging-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [split](https://help.syncfusion.com/file-formats/docio/word-document/split-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [compare documents](https://help.syncfusion.com/file-formats/docio/word-document/compare-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [find and replace](https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) text in the Word document, [protect](https://help.syncfusion.com/file-formats/docio/working-with-security?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) the Word documents, and most importantly, the [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [Image](https://help.syncfusion.com/file-formats/docio/word-to-image?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) conversions with code examples.
20+
Take a moment to peruse the [documentation](https://help.syncfusion.com/document-processing/docio/getting-started?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), where you can find basic Word document processing options along with the features like [mail merge](https://help.syncfusion.com/document-processing/docio/working-with-mail-merge?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [merge](https://help.syncfusion.com/document-processing/docio/word-document/merging-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [split](https://help.syncfusion.com/document-processing/docio/word-document/split-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [compare documents](https://help.syncfusion.com/document-processing/docio/word-document/compare-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [find and replace](https://help.syncfusion.com/document-processing/docio/working-with-find-and-replace?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) text in the Word document, [protect](https://help.syncfusion.com/document-processing/docio/working-with-security?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) the Word documents, and most importantly, the [PDF](https://help.syncfusion.com/document-processing/docio/word-to-pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) and [Image](https://help.syncfusion.com/document-processing/docio/word-to-image?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) conversions with code examples.
1921

2022
Compatible Microsoft Word Versions
2123
----------------------------------
@@ -31,8 +33,8 @@ Compatible Microsoft Word Versions
3133
Supported File Formats
3234
----------------------
3335

34-
* Creates, reads, and edits popular text file formats like [DOC](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#doc-to-docx-and-docx-to-doc), DOT, [DOCM](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#macros), DOTM, [DOCX](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#doc-to-docx-and-docx-to-doc), [DOTX](https://help.syncfusion.com/file-formats/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#templates), [HTML](https://help.syncfusion.com/file-formats/docio/html?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [RTF](https://help.syncfusion.com/file-formats/docio/rtf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [TXT](https://help.syncfusion.com/file-formats/docio/text?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [XML (WordML)](https://help.syncfusion.com/file-formats/docio/word-file-formats#word-processing-xml-xml?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
35-
* Converts Word documents also to [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Image](https://help.syncfusion.com/file-formats/docio/word-to-image?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [ODT](https://help.syncfusion.com/file-formats/docio/word-to-odt?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) files.
36+
* Creates, reads, and edits popular text file formats like [DOC](https://help.syncfusion.com/document-processing/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#doc-to-docx-and-docx-to-doc), DOT, [DOCM](https://help.syncfusion.com/document-processing/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#macros), DOTM, [DOCX](https://help.syncfusion.com/document-processing/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#doc-to-docx-and-docx-to-doc), [DOTX](https://help.syncfusion.com/document-processing/docio/word-file-formats?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples#templates), [HTML](https://help.syncfusion.com/document-processing/docio/html?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [RTF](https://help.syncfusion.com/document-processing/docio/rtf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [TXT](https://help.syncfusion.com/document-processing/docio/text?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [XML (WordML)](https://help.syncfusion.com/document-processing/docio/word-file-formats#word-processing-xml-xml?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).
37+
* Converts Word documents also to [PDF](https://help.syncfusion.com/document-processing/docio/word-to-pdf?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), [Image](https://help.syncfusion.com/document-processing/docio/word-to-image?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples), and [ODT](https://help.syncfusion.com/document-processing/docio/word-to-odt?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) files.
3638

3739
## How to run the examples
3840
- Download this project to a location in your disk.
@@ -44,9 +46,9 @@ Supported File Formats
4446
## Resources
4547

4648
- **Product page:** [Syncfusion Word Framework](https://www.syncfusion.com/document-processing/word-framework/net?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
47-
- **Documentation:** [Compare Word documents using Syncfusion Word library](https://help.syncfusion.com/file-formats/docio/word-document/compare-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
49+
- **Documentation:** [Split Word document using Syncfusion Word library](https://help.syncfusion.com/document-processing/docio/word-document/compare-word-documents?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
4850
- **GitHub Examples:** [Syncfusion Word library examples](https://github.com/SyncfusionExamples/DocIO-Examples?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
49-
- **Online demo:** [Syncfusion Word library - Online demos](https://ej2.syncfusion.com/aspnetcore/DocIO/SalesInvoice?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
51+
- **Online demo:** [Syncfusion Word library - Online demos](https://ej2aspnetcore.azurewebsites.net/aspnetcore/word/splitbysection#/material3?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples)
5052

5153
## Support and feedback
5254
For any other queries, reach our [Syncfusion support team](https://support.syncfusion.com/?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples) or post the queries through the [community forums](https://www.syncfusion.com/forums?utm_source=github&utm_medium=listing&utm_campaign=github-docio-examples).

Split-Word-document-by-bookmarks/.NET/Split-Word-document-by-bookmarks/Split-Word-document-by-bookmarks/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
using Syncfusion.DocIO.DLS;
2-
using Syncfusion.DocIO;
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
34

4-
//Load an existing Word document
55
using (FileStream fileStreamPath = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
66
{
7+
//Open an existing Word document
78
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
89
{
910
//Create the bookmark navigator instance to access the bookmark
1011
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
12+
//Get all bookmarks in Word document
1113
BookmarkCollection bookmarkCollection = document.Bookmarks;
1214

1315
//Iterate each bookmark in Word document

Split-Word-document-by-headings/.NET/Split-Word-document-by-headings/Split-Word-document-by-headings/Program.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using (FileStream inputStream = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read))
55
{
6-
//Load the template document as stream
6+
//Open an existing Word document
77
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
88
{
99
WordDocument newDocument = null;
@@ -12,7 +12,7 @@
1212
//Iterate each section in the Word document
1313
foreach (WSection section in document.Sections)
1414
{
15-
//Clone the section and add into new document
15+
//Clone the section without items and add into new document
1616
if (newDocument != null)
1717
newSection = AddSection(newDocument, section);
1818
//Iterate each child entity in the Word document
@@ -55,32 +55,37 @@
5555
}
5656
}
5757
}
58+
/// <summary>
59+
/// Clone and add the section without content into new Word document
60+
/// </summary>
5861
static WSection AddSection(WordDocument newDocument, WSection section)
5962
{
60-
//Create new session based on original document
63+
//Create new section based on original document
6164
WSection newSection = section.Clone();
65+
//Remove body items from section
6266
newSection.Body.ChildEntities.Clear();
63-
//Remove the first page header
67+
//Remove headers and footers
6468
newSection.HeadersFooters.FirstPageHeader.ChildEntities.Clear();
65-
//Remove the first page footer
6669
newSection.HeadersFooters.FirstPageFooter.ChildEntities.Clear();
67-
//Remove the odd footer
6870
newSection.HeadersFooters.OddFooter.ChildEntities.Clear();
69-
//Remove the odd header
7071
newSection.HeadersFooters.OddHeader.ChildEntities.Clear();
71-
//Remove the even header
7272
newSection.HeadersFooters.EvenHeader.ChildEntities.Clear();
73-
//Remove the even footer
7473
newSection.HeadersFooters.EvenFooter.ChildEntities.Clear();
7574
//Add cloned section into new document
7675
newDocument.Sections.Add(newSection);
7776
return newSection;
7877
}
78+
/// <summary>
79+
/// Add item into the section.
80+
/// </summary>
7981
static void AddEntity(WSection newSection, Entity entity)
8082
{
8183
//Add cloned item into the newly created section
8284
newSection.Body.ChildEntities.Add(entity.Clone());
8385
}
86+
/// <summary>
87+
/// Save the Word document.
88+
/// </summary>
8489
static void SaveWordDocument(WordDocument newDocument, string fileName)
8590
{
8691
using (FileStream outputStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))

Split-Word-document-by-placeholders/.NET/Split-Word-document-by-placeholders/Split-Word-document-by-placeholders/Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using Syncfusion.DocIO.DLS;
33
using System.Text.RegularExpressions;
44

5-
//Load an existing Word document
65
using (FileStream fileStreamPath = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
76
{
7+
//Open an existing Word document
88
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
99
{
1010
//Find all the placeholder text in the Word document
@@ -13,8 +13,10 @@
1313
{
1414
//Unique ID for each bookmark
1515
int bkmkId = 1;
16-
//Collection to hold the inserted bookmarks
16+
//Collection to hold the newly inserted bookmarks
1717
List<string> bookmarks = new List<string>();
18+
19+
#region Add bookmark start and end in the place of placeholder text
1820
//Iterate each text selection
1921
for (int i = 0; i < textSelections.Length; i++)
2022
{
@@ -24,6 +26,7 @@
2426
WParagraph startParagraph = textRange.OwnerParagraph;
2527
int index = startParagraph.ChildEntities.IndexOf(textRange);
2628
string bookmarkName = "Bookmark_" + bkmkId;
29+
2730
//Add new bookmark to bookmarks collection
2831
bookmarks.Add(bookmarkName);
2932
//Create bookmark start
@@ -39,6 +42,7 @@
3942
//Get the index of the placeholder text
4043
WParagraph endParagraph = textRange.OwnerParagraph;
4144
index = endParagraph.ChildEntities.IndexOf(textRange);
45+
4246
//Create bookmark end
4347
BookmarkEnd bkmkEnd = new BookmarkEnd(document, bookmarkName);
4448
//Insert the bookmark end after the end placeholder
@@ -47,6 +51,9 @@
4751
//Remove the placeholder text
4852
textRange.Text = string.Empty;
4953
}
54+
#endregion
55+
56+
#region Split document based on newly inserted bookmarks
5057
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
5158
int fileIndex = 1;
5259
foreach (string bookmark in bookmarks)
@@ -66,6 +73,7 @@
6673
}
6774
fileIndex++;
6875
}
76+
#endregion
6977
}
7078
}
7179
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
using Syncfusion.DocIO.DLS;
2-
using Syncfusion.DocIO;
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
33

44
using (FileStream inputStream = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
55
{
6-
//Load the template document as stream
6+
//Open an existing Word document
77
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
88
{
9-
int fileId = 1;
9+
int sectionNumber = 1;
1010
//Iterate each section from Word document
1111
foreach (WSection section in document.Sections)
1212
{
1313
//Create new Word document
1414
using (WordDocument newDocument = new WordDocument())
1515
{
16-
//Add cloned section into new Word document
16+
//Clone and add section from one Word document to another
1717
newDocument.Sections.Add(section.Clone());
18-
//Save the Word document to MemoryStream
19-
using (FileStream outputStream = new FileStream(@"../../../Section" + fileId + ".docx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
18+
//Save the Word document
19+
using (FileStream outputStream = new FileStream(@"../../../Section" + sectionNumber + ".docx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
2020
{
2121
newDocument.Save(outputStream, FormatType.Docx);
2222
}
2323
}
24-
fileId++;
24+
sectionNumber++;
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)