Skip to content

Commit 62edde5

Browse files
Added the using statement for file stream path
1 parent b4cd882 commit 62edde5

File tree

2 files changed

+84
-79
lines changed
  • Word-document
    • Split-by-bookmark/.NET/Split-by-bookmark
    • Split-by-placeholder-text/.NET/Split-by-placeholder-text

2 files changed

+84
-79
lines changed

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,33 @@ class Program
99
static void Main(string[] args)
1010
{
1111
//Load an existing Word document.
12-
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
13-
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
12+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
1413
{
15-
//Create the bookmark navigator instance to access the bookmark.
16-
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
17-
BookmarkCollection bookmarkCollection = document.Bookmarks;
18-
//Iterate each bookmark in Word document.
19-
foreach (Bookmark bookmark in bookmarkCollection)
14+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
2015
{
21-
//Move the virtual cursor to the location before the end of the bookmark.
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())
16+
//Create the bookmark navigator instance to access the bookmark.
17+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
18+
BookmarkCollection bookmarkCollection = document.Bookmarks;
19+
//Iterate each bookmark in Word document.
20+
foreach (Bookmark bookmark in bookmarkCollection)
2721
{
28-
//Save the Word document to file stream.
29-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
22+
//Move the virtual cursor to the location before the end of the bookmark.
23+
bookmarksNavigator.MoveToBookmark(bookmark.Name);
24+
//Get the bookmark content as WordDocumentPart.
25+
WordDocumentPart documentPart = bookmarksNavigator.GetContent();
26+
//Save the WordDocumentPart as separate Word document
27+
using (WordDocument newDocument = documentPart.GetAsWordDocument())
3028
{
31-
newDocument.Save(outputFileStream, FormatType.Docx);
29+
//Save the Word document to file stream.
30+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
31+
{
32+
newDocument.Save(outputFileStream, FormatType.Docx);
33+
}
3234
}
3335
}
3436
}
35-
}
37+
38+
}
3639
}
3740
}
3841
}

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

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,80 @@ class Program
1111
static void Main(string[] args)
1212
{
1313
//Load an existing Word document into DocIO instance.
14-
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
15-
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
14+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
1615
{
17-
18-
//Finds all the placeholder text in the Word document.
19-
TextSelection[] textSelections = document.FindAll(new Regex("<<(.*)>>"));
20-
if (textSelections != null)
16+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
2117
{
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++)
18+
19+
//Finds all the placeholder text in the Word document.
20+
TextSelection[] textSelections = document.FindAll(new Regex("<<(.*)>>"));
21+
if (textSelections != null)
2922
{
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
23+
#region Insert bookmarks at placeholders
24+
//Unique ID for each bookmark.
25+
int bkmkId = 1;
26+
//Collection to hold the inserted bookmarks.
27+
List<string> bookmarks = new List<string>();
28+
//Iterate each text selection.
29+
for (int i = 0; i < textSelections.Length; i++)
30+
{
31+
#region Insert bookmark start before the placeholder
32+
//Get the placeholder as WTextRange.
33+
WTextRange textRange = textSelections[i].GetAsOneRange();
34+
//Get the index of the placeholder text.
35+
WParagraph startParagraph = textRange.OwnerParagraph;
36+
int index = startParagraph.ChildEntities.IndexOf(textRange);
37+
string bookmarkName = "Bookmark_" + bkmkId;
38+
//Add new bookmark to bookmarks collection.
39+
bookmarks.Add(bookmarkName);
40+
//Create bookmark start.
41+
BookmarkStart bkmkStart = new BookmarkStart(document, bookmarkName);
42+
//Insert the bookmark start before the start placeholder.
43+
startParagraph.ChildEntities.Insert(index, bkmkStart);
44+
//Remove the placeholder text.
45+
textRange.Text = string.Empty;
46+
#endregion
4647

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
48+
#region Insert bookmark end after the placeholder
49+
i++;
50+
//Get the placeholder as WTextRange.
51+
textRange = textSelections[i].GetAsOneRange();
52+
//Get the index of the placeholder text.
53+
WParagraph endParagraph = textRange.OwnerParagraph;
54+
index = endParagraph.ChildEntities.IndexOf(textRange);
55+
//Create bookmark end.
56+
BookmarkEnd bkmkEnd = new BookmarkEnd(document, bookmarkName);
57+
//Insert the bookmark end after the end placeholder.
58+
endParagraph.ChildEntities.Insert(index + 1, bkmkEnd);
59+
bkmkId++;
60+
//Remove the placeholder text.
61+
textRange.Text = string.Empty;
62+
#endregion
6263

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)
69-
{
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())
64+
}
65+
#endregion
66+
#region Split bookmark content into separate documents
67+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
68+
int fileIndex = 1;
69+
foreach (string bookmark in bookmarks)
7670
{
77-
//Save the Word document to file stream.
78-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
71+
//Move the virtual cursor to the location before the end of the bookmark.
72+
bookmarksNavigator.MoveToBookmark(bookmark);
73+
//Get the bookmark content as WordDocumentPart.
74+
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
75+
//Save the WordDocumentPart as separate Word document.
76+
using (WordDocument newDocument = wordDocumentPart.GetAsWordDocument())
7977
{
80-
newDocument.Save(outputFileStream, FormatType.Docx);
78+
//Save the Word document to file stream.
79+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
80+
{
81+
newDocument.Save(outputFileStream, FormatType.Docx);
82+
}
8183
}
84+
fileIndex++;
8285
}
83-
fileIndex++;
86+
#endregion
8487
}
85-
#endregion
8688
}
8789
}
8890
}

0 commit comments

Comments
 (0)