Skip to content

Commit d422538

Browse files
Optimized code
1 parent 808532e commit d422538

File tree

2 files changed

+13
-13
lines changed
  • Word-document
    • Split-by-bookmark/.NET-Standard/Split-by-bookmark
    • Split-by-placeholder-text/.NET-Standard/Split-by-placeholder-text

2 files changed

+13
-13
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ static void Main(string[] args)
1818
//Iterate each bookmark in Word document.
1919
foreach (Bookmark bookmark in bookmarkCollection)
2020
{
21-
MemoryStream memoryStream = new MemoryStream();
2221
//Move the virtual cursor to the location before the end of the bookmark.
2322
bookmarksNavigator.MoveToBookmark(bookmark.Name);
2423
//Get the bookmark content as WordDocumentPart.
2524
WordDocumentPart documentPart = bookmarksNavigator.GetContent();
2625
//Save the WordDocumentPart as separate Word document
27-
WordDocument newDocument = documentPart.GetAsWordDocument();
28-
29-
//Save the Word document to file stream.
30-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
26+
using (WordDocument newDocument = documentPart.GetAsWordDocument())
3127
{
32-
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+
}
3333
}
3434
}
3535
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ static void Main(string[] args)
2222
#region Insert bookmarks at placeholders
2323
//Unique ID for each bookmark.
2424
int bkmkId = 1;
25-
string bookmarkName = string.Empty;
2625
//Collection to hold the inserted bookmarks.
2726
List<string> bookmarks = new List<string>();
2827
//Iterate each text selection.
@@ -34,7 +33,7 @@ static void Main(string[] args)
3433
//Get the index of the placeholder text.
3534
WParagraph startParagraph = textRange.OwnerParagraph;
3635
int index = startParagraph.ChildEntities.IndexOf(textRange);
37-
bookmarkName = "Bookmark_" + bkmkId;
36+
string bookmarkName = "Bookmark_" + bkmkId;
3837
//Add new bookmark to bookmarks collection.
3938
bookmarks.Add(bookmarkName);
4039
//Create bookmark start.
@@ -68,17 +67,18 @@ static void Main(string[] args)
6867
int fileIndex = 1;
6968
foreach (string bookmark in bookmarks)
7069
{
71-
MemoryStream memoryStream = new MemoryStream();
7270
//Move the virtual cursor to the location before the end of the bookmark.
7371
bookmarksNavigator.MoveToBookmark(bookmark);
7472
//Get the bookmark content as WordDocumentPart.
7573
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
7674
//Save the WordDocumentPart as separate Word document.
77-
WordDocument newDocument = wordDocumentPart.GetAsWordDocument();
78-
//Save the Word document to file stream.
79-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
75+
using (WordDocument newDocument = wordDocumentPart.GetAsWordDocument())
8076
{
81-
newDocument.Save(outputFileStream, FormatType.Docx);
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+
}
8282
}
8383
fileIndex++;
8484
}

0 commit comments

Comments
 (0)