Skip to content

Commit 795dbb9

Browse files
addressed the feedbacks
1 parent edd8d15 commit 795dbb9

File tree

4 files changed

+43
-48
lines changed

4 files changed

+43
-48
lines changed

Bookmarks/Maintain-source-list-format-after-replace/.NET/Maintain-source-list-format-after-replace/Maintain-source-list-format-after-replace.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="24.1.41" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

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

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,90 @@ internal class Program
77
{
88
static void Main(string[] args)
99
{
10-
FileStream inputOne = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read);
11-
WordDocument inputOneDoc = new WordDocument(inputOne, FormatType.Docx);
12-
FileStream inputTwo = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read);
13-
WordDocument inputTwoDoc = new WordDocument(inputTwo, FormatType.Docx);
14-
DocxReplaceTextWithDocPart(inputOneDoc, inputTwoDoc, "Text one", "bkmk1");
15-
DocxReplaceTextWithDocPart(inputOneDoc, inputTwoDoc, "Text two", "bkmk2");
16-
FileStream output = new FileStream(Path.GetFullPath("Output/Output.docx"), FileMode.Create, FileAccess.Write);
17-
inputOneDoc.Save(output, FormatType.Docx);
18-
inputOneDoc.Close();
19-
inputTwoDoc.Close();
20-
inputOne.Close();
21-
inputTwo.Close();
22-
output.Close();
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))
12+
{
13+
// Load the source document from the stream.
14+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
15+
{
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))
18+
{
19+
// Load the destination document from the stream.
20+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
21+
{
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.
27+
using (FileStream output = new FileStream(Path.GetFullPath("Output/Output.docx"), FileMode.Create, FileAccess.Write))
28+
{
29+
// Save the modified source document to the output stream in DOCX format.
30+
sourceDocument.Save(output, FormatType.Docx);
31+
}
32+
}
33+
}
34+
}
35+
}
2336
}
2437

38+
/// <summary>
39+
/// Replaces specific text in a Word document with bookmarked content from another document, maintaining formatting.
40+
/// </summary>
2541
private static void DocxReplaceTextWithDocPart(WordDocument document, WordDocument sourceDoc, string tokenToFind, string textBookmark)
2642
{
2743
string bookmarkRef = textBookmark + "_bm";
2844

29-
//Find the start token.
45+
// Find the start token in the document.
3046
TextSelection start = document.Find(tokenToFind, true, true);
3147
if (start != null)
3248
{
3349
WTextRange startText = start.GetAsOneRange();
3450
WParagraph startParagraph = startText.OwnerParagraph;
35-
//Get the item index of the start token in the paragraph
51+
// Get the index of the start token in the paragraph.
3652
int index = startParagraph.Items.IndexOf(startText);
37-
//Remove the start token at the specified index
53+
// Remove the start token at the specified index.
3854
startParagraph.Items.Remove(startText);
39-
//Create and insert a Bookmark start at the index of the start token
55+
// Create and insert a BookmarkStart at the index of the start token.
4056
BookmarkStart bookmarkStart = new BookmarkStart(document, bookmarkRef);
4157
startParagraph.Items.Insert(index, bookmarkStart);
4258
startParagraph.AppendBookmarkEnd(bookmarkRef);
4359

44-
//Open the document that contains the text to replace
45-
//For instance, the document contains Bookmark named "DocIO" and the contents of that
46-
//bookmark should replace the content in above document
47-
//Creates the bookmark navigator instance to access the bookmark
60+
// Check if the bookmark exists in the source document.
4861
if (sourceDoc.Bookmarks.FindByName(textBookmark) != null)
4962
{
63+
// Access the bookmark in the source document.
5064
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(sourceDoc);
51-
//Moves the virtual cursor to the location before the end of the bookmark "DocIO"
5265
bookmarksNavigator.MoveToBookmark(textBookmark);
53-
//Gets the bookmark content
66+
// Get the bookmark content.
5467
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
55-
//Creates the bookmark navigator instance to access the bookmark
5668
bookmarksNavigator = new BookmarksNavigator(document);
57-
//Moves the virtual cursor to the location before the end of the bookmark "Bookmark"
5869
bookmarksNavigator.MoveToBookmark(bookmarkRef);
5970

60-
//Get the destination para before replacing
71+
// Get the destination paragraph before replacing.
6172
WParagraph destinationPara = bookmarksNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
62-
//Get the list style name
73+
// Store the list style, first line indent, and left indent of the paragraph.
6374
string listStyleName = destinationPara.ListFormat.CustomStyleName;
64-
//Get the first line indent value
6575
float firstLineIndent = destinationPara.ParagraphFormat.FirstLineIndent;
66-
//Get the left indent value
6776
float leftIndent = destinationPara.ParagraphFormat.LeftIndent;
6877

69-
//Replace the selected text with another Word document content
78+
// Replace the selected text with the bookmark content from the source document.
7079
bookmarksNavigator.ReplaceContent(wordDocumentPart);
71-
//Apply the list style, first line indent and left indent values after replacing
80+
// Reapply the list style and indent values after replacement.
7281
destinationPara.ListFormat.ApplyStyle(listStyleName);
7382
destinationPara.ParagraphFormat.FirstLineIndent = firstLineIndent;
7483
destinationPara.ParagraphFormat.LeftIndent = leftIndent;
7584
}
7685
else
7786
{
78-
//Creates the bookmark navigator instance to access the bookmark
87+
// If the bookmark is not found, replace the bookmark content with an empty string.
7988
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
80-
//Moves the virtual cursor to the location before the end of the bookmark "Bookmark"
8189
bookmarksNavigator.MoveToBookmark(bookmarkRef);
82-
//Replace the selected text with another Word document content
8390
bookmarksNavigator.ReplaceBookmarkContent(string.Empty, true);
8491
}
8592
}
8693
}
94+
8795
}
8896
}

Content-Controls/Remove-content-control-not-content/.NET/Remove-content-control-not-content/Program.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
33

4-
// Register Syncfusion license for the application.
5-
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mgo+DSMBMAY9C3t2UlhhQlNHfV5DQmBWfFN0QXNYfVRwdF9GYEwgOX1dQl9nSXZTc0VlWndfcXNSQWc=");
6-
7-
84
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
95
{
106
//Load the Word document from the FileStream.

Tables/Replace-Cell-Content/.NET/Replace-Cell-Content/Program.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
11
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
33

4-
//Register Syncfusion license
5-
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mgo+DSMBMAY9C3t2UlhhQlNHfV5DQmBWfFN0QXNYfVRwdF9GYEwgOX1dQl9nSXZTc0VlWndfcXNSQWc=");
6-
74
// Load the existing Word document.
85
using (FileStream inputFileStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
96
{
107
// Open the document.
118
WordDocument document = new WordDocument(inputFileStream, FormatType.Docx);
12-
139
// Retrieve the first section of the document.
1410
IWSection section = document.LastSection;
15-
1611
// Get the first table in the section by 0th index.
1712
WTable table = section.Body.Tables[0] as WTable;
18-
1913
// Access the specific cells by their indices (for example, (0, 0) and (1, 1)).
2014
WTableCell cell1 = table[1, 1]; // First row, first column.
2115
WTableCell cell2 = table[2, 2]; // Second row, second column.
22-
2316
// Clear the contents of the first cell.
2417
cell1.ChildEntities.Clear();
2518
// Add a new paragraph with content to the first cell.
2619
cell1.AddParagraph().AppendText("New Content for Cell 6");
27-
2820
// Clear the contents of the second cell.
2921
cell2.ChildEntities.Clear();
3022
// Add a new paragraph with content to the second cell.
3123
cell2.AddParagraph().AppendText("New Content for Cell 11");
32-
3324
// Save the modified document.
3425
using (FileStream outputFileStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
3526
{

0 commit comments

Comments
 (0)