Skip to content

Commit 93f8f98

Browse files
Merge pull request #187 from AntonihilSahayaraj/main
Modified the to Split Word document samples
2 parents 038e53f + b492d25 commit 93f8f98

File tree

12 files changed

+110
-67
lines changed

12 files changed

+110
-67
lines changed
Binary file not shown.

Word-document/Split-by-bookmark/.NET-Standard/Split-by-bookmark/Program.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
3-
using System;
43
using System.IO;
54

65
namespace Split_a_document_by_bookmark
@@ -10,29 +9,27 @@ class Program
109
static void Main(string[] args)
1110
{
1211
//Load an existing Word document.
13-
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
12+
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1413
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
1514
{
1615
//Create the bookmark navigator instance to access the bookmark.
17-
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
18-
//Get the bookmark collections in the document.
19-
BookmarkCollection bookmarkCollection = document.Bookmarks;
16+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
17+
BookmarkCollection bookmarkCollection = document.Bookmarks;
18+
//Iterate each bookmark in Word document.
2019
foreach (Bookmark bookmark in bookmarkCollection)
2120
{
2221
//Move the virtual cursor to the location before the end of the bookmark.
23-
bookmarkNavigator.MoveToBookmark(bookmark.Name);
24-
//Get the bookmark content.
25-
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
26-
//Create a new Word document.
27-
WordDocument newDocument = new WordDocument();
28-
newDocument.AddSection();
29-
//Add the retrieved content into another new document.
30-
for (int i = 0; i < part.BodyItems.Count; i++)
31-
newDocument.LastSection.Body.ChildEntities.Add(part.BodyItems[i].Clone());
32-
//Save the Word document to file stream.
33-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result"+ bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
22+
bookmarksNavigator.MoveToBookmark(bookmark.Name);
23+
//Get the bookmark content as WordDocumentPart.
24+
WordDocumentPart documentPart = bookmarksNavigator.GetContent();
25+
//Save the WordDocumentPart as separate Word document
26+
using (WordDocument newDocument = documentPart.GetAsWordDocument())
3427
{
35-
newDocument.Save(outputFileStream, FormatType.Docx);
28+
//Save the Word document to file stream.
29+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
30+
{
31+
newDocument.Save(outputFileStream, FormatType.Docx);
32+
}
3633
}
3734
}
3835
}
Binary file not shown.
Binary file not shown.

Word-document/Split-by-heading/.NET-Standard/Split-by-heading/Program.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using Syncfusion.DocIO;
43
using Syncfusion.DocIO.DLS;
54

@@ -9,52 +8,56 @@ class Program
98
{
109
static void Main(string[] args)
1110
{
12-
using (FileStream inputStream = new FileStream(@"../../../Template.docx", FileMode.Open, FileAccess.Read))
11+
using (FileStream inputStream = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read))
1312
{
1413
//Load the template document as stream
1514
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
1615
{
1716
WordDocument newDocument = null;
1817
WSection newSection = null;
19-
int i = 0;
20-
//Iterate each section from Word document
18+
int headingIndex = 0;
19+
//Iterate each section in the Word document.
2120
foreach (WSection section in document.Sections)
2221
{
22+
// Clone the section and add into new document.
2323
if (newDocument != null)
2424
newSection = AddSection(newDocument, section);
25-
foreach (TextBodyItem textbodyitem in section.Body.ChildEntities)
25+
//Iterate each child entity in the Word document.
26+
foreach (TextBodyItem item in section.Body.ChildEntities)
2627
{
27-
if (textbodyitem is WParagraph)
28+
//If item is paragraph, then check for heading style and split.
29+
//else, add the item into new document.
30+
if (item is WParagraph)
2831
{
29-
WParagraph para = textbodyitem as WParagraph;
30-
if (para.StyleName == "Heading 1")
32+
WParagraph paragraph = item as WParagraph;
33+
//If paragraph has Heading 1 style, then save the traversed content as separate document.
34+
//And create new document for new heading content.
35+
if (paragraph.StyleName == "Heading 1")
3136
{
3237
if (newDocument != null)
3338
{
3439
//Saves the Word document
35-
string fileName = @"../../../Heading" + i + ".docx";
40+
string fileName = @"../../../Document" + (headingIndex + 1) + ".docx";
3641
SaveWordDocument(newDocument, fileName);
37-
i++;
42+
headingIndex++;
3843
}
39-
//Create new Word document
44+
//Create new document for new heading content.
4045
newDocument = new WordDocument();
4146
newSection = AddSection(newDocument, section);
42-
//Add cloned paragraphs into new section
43-
AddEntity(newSection, para);
47+
AddEntity(newSection, paragraph);
4448
}
4549
else if (newDocument != null)
46-
//Add cloned paragraphs into new section
47-
AddEntity(newSection, para);
50+
AddEntity(newSection, paragraph);
4851
}
49-
else
50-
//Add cloned item into new section
51-
AddEntity(newSection, textbodyitem);
52+
else
53+
AddEntity(newSection, item);
5254
}
5355
}
56+
//Save the remaining content as separate document.
5457
if (newDocument != null)
5558
{
5659
//Saves the Word document
57-
string fileName = @"../../../Heading" + i + ".docx";
60+
string fileName = @"../../../Document" + (headingIndex + 1) + ".docx";
5861
SaveWordDocument(newDocument, fileName);
5962
}
6063
}
Binary file not shown.

Word-document/Split-by-placeholder-text/.NET-Standard/Split-by-placeholder-text/Program.cs

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
3-
using System;
3+
using System.Collections.Generic;
44
using System.IO;
5+
using System.Text.RegularExpressions;
56

67
namespace Split_a_document_by_placeholder_text
78
{
@@ -10,35 +11,78 @@ class Program
1011
static void Main(string[] args)
1112
{
1213
//Load an existing Word document into DocIO instance.
13-
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
14+
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1415
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
1516
{
16-
String[] findPlaceHolderWord = new string[] { "[First Content Start]", "[Second Content Start]", "[Third Content Start]" };
17-
for (int i = 0; i < findPlaceHolderWord.Length; i++)
17+
18+
//Finds all the placeholder text in the Word document.
19+
TextSelection[] textSelections = document.FindAll(new Regex("<<(.*)>>"));
20+
if (textSelections != null)
1821
{
19-
//Get the start placeholder paragraph in the document.
20-
WParagraph startParagraph = document.Find(findPlaceHolderWord[i], true, true).GetAsOneRange().OwnerParagraph;
21-
//Get the end placeholder paragraph in the document.
22-
WParagraph endParagraph = document.Find(findPlaceHolderWord[i].Replace("Start", "End"), true, true).GetAsOneRange().OwnerParagraph;
23-
//Get the text body.
24-
WTextBody textBody = startParagraph.OwnerTextBody;
25-
//Get the start PlaceHolder index.
26-
int startPlaceHolderIndex = textBody.ChildEntities.IndexOf(startParagraph);
27-
//Get the end PlaceHolder index.
28-
int endPlaceHolderIndex = textBody.ChildEntities.IndexOf(endParagraph);
22+
#region Insert bookmarks at placeholders
23+
//Unique ID for each bookmark.
24+
int bkmkId = 1;
25+
//Collection to hold the inserted bookmarks.
26+
List<string> bookmarks = new List<string>();
27+
//Iterate each text selection.
28+
for (int i = 0; i < textSelections.Length; i++)
29+
{
30+
#region Insert bookmark start before the placeholder
31+
//Get the placeholder as WTextRange.
32+
WTextRange textRange = textSelections[i].GetAsOneRange();
33+
//Get the index of the placeholder text.
34+
WParagraph startParagraph = textRange.OwnerParagraph;
35+
int index = startParagraph.ChildEntities.IndexOf(textRange);
36+
string bookmarkName = "Bookmark_" + bkmkId;
37+
//Add new bookmark to bookmarks collection.
38+
bookmarks.Add(bookmarkName);
39+
//Create bookmark start.
40+
BookmarkStart bkmkStart = new BookmarkStart(document, bookmarkName);
41+
//Insert the bookmark start before the start placeholder.
42+
startParagraph.ChildEntities.Insert(index, bkmkStart);
43+
//Remove the placeholder text.
44+
textRange.Text = string.Empty;
45+
#endregion
46+
47+
#region Insert bookmark end after the placeholder
48+
i++;
49+
//Get the placeholder as WTextRange.
50+
textRange = textSelections[i].GetAsOneRange();
51+
//Get the index of the placeholder text.
52+
WParagraph endParagraph = textRange.OwnerParagraph;
53+
index = endParagraph.ChildEntities.IndexOf(textRange);
54+
//Create bookmark end.
55+
BookmarkEnd bkmkEnd = new BookmarkEnd(document, bookmarkName);
56+
//Insert the bookmark end after the end placeholder.
57+
endParagraph.ChildEntities.Insert(index + 1, bkmkEnd);
58+
bkmkId++;
59+
//Remove the placeholder text.
60+
textRange.Text = string.Empty;
61+
#endregion
2962

30-
//Create a new Word document.
31-
WordDocument newDocument = new WordDocument();
32-
newDocument.AddSection();
33-
//Add the retrieved content into another new document.
34-
for (int j = startPlaceHolderIndex + 1; j < endPlaceHolderIndex; j++)
35-
newDocument.LastSection.Body.ChildEntities.Add(textBody.ChildEntities[j].Clone());
36-
//Save the Word document to file stream.
37-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result" + i + ".docx"), FileMode.Create, FileAccess.ReadWrite))
63+
}
64+
#endregion
65+
#region Split bookmark content into separate documents
66+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
67+
int fileIndex = 1;
68+
foreach (string bookmark in bookmarks)
3869
{
39-
40-
newDocument.Save(outputFileStream, FormatType.Docx);
70+
//Move the virtual cursor to the location before the end of the bookmark.
71+
bookmarksNavigator.MoveToBookmark(bookmark);
72+
//Get the bookmark content as WordDocumentPart.
73+
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
74+
//Save the WordDocumentPart as separate Word document.
75+
using (WordDocument newDocument = wordDocumentPart.GetAsWordDocument())
76+
{
77+
//Save the Word document to file stream.
78+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
79+
{
80+
newDocument.Save(outputFileStream, FormatType.Docx);
81+
}
82+
}
83+
fileIndex++;
4184
}
85+
#endregion
4286
}
4387
}
4488
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)