Skip to content

Commit dcaacfb

Browse files
Addressed the feedbacks
1 parent 8b449af commit dcaacfb

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

Bookmarks/Maintain-source-list-format-after-replace/.NET/Maintain-source-list-format-after-replace/Program.cs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ internal class Program
77
{
88
static void Main(string[] args)
99
{
10-
// Open the source Word document for reading using a file stream.
11-
using (FileStream sourceStream = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read))
10+
using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read))
1211
{
13-
// Load the source document from the stream.
14-
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
12+
//Open the destination Word document.
13+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
1514
{
16-
// Open the destination Word document for reading using another file stream.
17-
using (FileStream destinationStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read))
15+
using (FileStream srcStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read))
1816
{
19-
// Load the destination document from the stream.
20-
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
17+
//Open the source Word document.
18+
using (WordDocument srcDocument = new WordDocument(srcStream, FormatType.Docx))
2119
{
22-
// Replace text "Text one" in the source document with content from the bookmark "bkmk1" in the destination document.
23-
DocxReplaceTextWithDocPart(sourceDocument, destinationDocument, "Text one", "bkmk1");
24-
// Replace text "Text two" in the source document with content from the bookmark "bkmk2" in the destination document.
25-
DocxReplaceTextWithDocPart(sourceDocument, destinationDocument, "Text two", "bkmk2");
26-
// Open a stream to save the modified source document as a new file.
20+
//Replace text "Text one" in the destination document with content from the bookmark "bkmk1" in the source document.
21+
DocxReplaceTextWithDocPart(destinationDocument, srcDocument, "Text one", "bkmk1");
22+
//Replace text "Text two" in the destination document with content from the bookmark "bkmk2" in the source document.
23+
DocxReplaceTextWithDocPart(destinationDocument, srcDocument, "Text two", "bkmk2");
24+
//Save the modified destination document to the output stream.
2725
using (FileStream output = new FileStream(Path.GetFullPath("Output/Output.docx"), FileMode.Create, FileAccess.Write))
2826
{
29-
// Save the modified source document to the output stream in DOCX format.
30-
sourceDocument.Save(output, FormatType.Docx);
27+
destinationDocument.Save(output, FormatType.Docx);
3128
}
3229
}
3330
}
@@ -42,49 +39,49 @@ private static void DocxReplaceTextWithDocPart(WordDocument document, WordDocume
4239
{
4340
string bookmarkRef = textBookmark + "_bm";
4441

45-
// Find the start token in the document.
42+
//Find the start token in the document.
4643
TextSelection start = document.Find(tokenToFind, true, true);
4744
if (start != null)
4845
{
4946
WTextRange startText = start.GetAsOneRange();
5047
WParagraph startParagraph = startText.OwnerParagraph;
51-
// Get the index of the start token in the paragraph.
48+
//Get the index of the start token in the paragraph.
5249
int index = startParagraph.Items.IndexOf(startText);
53-
// Remove the start token at the specified index.
50+
//Remove the start token at the specified index.
5451
startParagraph.Items.Remove(startText);
55-
// Create and insert a BookmarkStart at the index of the start token.
52+
//Create and insert a BookmarkStart at the index of the start token.
5653
BookmarkStart bookmarkStart = new BookmarkStart(document, bookmarkRef);
5754
startParagraph.Items.Insert(index, bookmarkStart);
5855
startParagraph.AppendBookmarkEnd(bookmarkRef);
5956

60-
// Check if the bookmark exists in the source document.
57+
//Check if the bookmark exists in the source document.
6158
if (sourceDoc.Bookmarks.FindByName(textBookmark) != null)
6259
{
63-
// Access the bookmark in the source document.
60+
//Access the bookmark in the source document.
6461
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(sourceDoc);
6562
bookmarksNavigator.MoveToBookmark(textBookmark);
66-
// Get the bookmark content.
63+
//Get the bookmark content.
6764
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
6865
bookmarksNavigator = new BookmarksNavigator(document);
6966
bookmarksNavigator.MoveToBookmark(bookmarkRef);
7067

71-
// Get the destination paragraph before replacing.
68+
//Get the destination paragraph before replacing.
7269
WParagraph destinationPara = bookmarksNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
73-
// Store the list style, first line indent, and left indent of the paragraph.
70+
//Store the list style, first line indent, and left indent of the paragraph.
7471
string listStyleName = destinationPara.ListFormat.CustomStyleName;
7572
float firstLineIndent = destinationPara.ParagraphFormat.FirstLineIndent;
7673
float leftIndent = destinationPara.ParagraphFormat.LeftIndent;
7774

78-
// Replace the selected text with the bookmark content from the source document.
75+
//Replace the selected text with the bookmark content from the source document.
7976
bookmarksNavigator.ReplaceContent(wordDocumentPart);
80-
// Reapply the list style and indent values after replacement.
77+
//Reapply the list style and indent values after replacement.
8178
destinationPara.ListFormat.ApplyStyle(listStyleName);
8279
destinationPara.ParagraphFormat.FirstLineIndent = firstLineIndent;
8380
destinationPara.ParagraphFormat.LeftIndent = leftIndent;
8481
}
8582
else
8683
{
87-
// If the bookmark is not found, replace the bookmark content with an empty string.
84+
//If the bookmark is not found, replace the bookmark content with an empty string.
8885
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
8986
bookmarksNavigator.MoveToBookmark(bookmarkRef);
9087
bookmarksNavigator.ReplaceBookmarkContent(string.Empty, true);

0 commit comments

Comments
 (0)