diff --git a/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag.sln b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag.sln new file mode 100644 index 00000000..36cb00fe --- /dev/null +++ b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-inside-tag", "Replace-text-inside-tag\Replace-text-inside-tag.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4} + EndGlobalSection +EndGlobal diff --git a/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Data/DestinationDocument.docx b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Data/DestinationDocument.docx new file mode 100644 index 00000000..82051c43 Binary files /dev/null and b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Data/DestinationDocument.docx differ diff --git a/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Data/SourceDocument.docx b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Data/SourceDocument.docx new file mode 100644 index 00000000..2d2956a7 Binary files /dev/null and b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Data/SourceDocument.docx differ diff --git a/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Output/.gitkeep b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Program.cs b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Program.cs new file mode 100644 index 00000000..a63cc9ba --- /dev/null +++ b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Program.cs @@ -0,0 +1,101 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using System.IO; +using System.Text.RegularExpressions; + +namespace Replace_text_inside_tag +{ + class Program + { + static void Main(string[] args) + { + using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + //Open the destination Word document. + using (WordDocument destinationDocument = new WordDocument(fileStreamPath, FormatType.Docx)) + { + using (FileStream sourceFileStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + //Open the source Word document. + using (WordDocument sourceDocument = new WordDocument(sourceFileStream, FormatType.Docx)) + { + //Get the content between the tags in the source document as body part. + TextSelection[] textSelections = sourceDocument.FindSingleLine(new Regex("(.*)")); + if (textSelections != null) + { + TextBodyPart bodyPart = new TextBodyPart(destinationDocument); + for (int i = 1; i < textSelections.Length - 1; i++) + { + WParagraph paragraph = new WParagraph(destinationDocument); + foreach (var range in textSelections[i].GetRanges()) + { + WTextRange textrange = range.Clone() as WTextRange; + paragraph.ChildEntities.Add(textrange); + } + bodyPart.BodyItems.Add(paragraph); + } + //Replace the text between specified tags in the destination document using a bookmark + //with the content from the source document. + ReplaceTextBetweenTags(destinationDocument, bodyPart); + } + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the Word document to file stream. + destinationDocument.Save(outputFileStream, FormatType.Docx); + } + } + } + } + } + } + #region Helper Methods + /// + /// Replaces the content between specified start and end tags within the destination document using a bookmark. + /// + /// The Word document where the replacement is to be performed. + /// The content to insert between the specified start and end tags. + private static void ReplaceTextBetweenTags(WordDocument destinationDocument, TextBodyPart bodyPart) + { + //Define the start and end tags to identify the content to be replaced. + string startTag = ""; + string endTag = ""; + //Create bookmark start and bookmark end. + BookmarkStart bookmarkStart = new BookmarkStart(destinationDocument, "Adventure_Bkmk"); + BookmarkEnd bookmarkEnd = new BookmarkEnd(destinationDocument, "Adventure_Bkmk"); + + //Find the start tag in the destination document. + TextSelection textSelection = destinationDocument.Find(startTag, false, false); + if (textSelection == null) return; //Exit if start tag is not found. + + //Add a bookmark start after the start tag location. + WTextRange startTagTextRange = textSelection.GetAsOneRange(); + WParagraph startTagParagraph = startTagTextRange.OwnerParagraph; + int startTagIndex = startTagParagraph.ChildEntities.IndexOf(startTagTextRange); + startTagParagraph.Items.Insert(startTagIndex + 1, bookmarkStart); + + //Find the end tag in the destination document. + textSelection = destinationDocument.Find(endTag, false, false); + if (textSelection == null) return; // Exit if end tag is not found + + //Add a bookmark end at the end tag location (before end tag). + WTextRange endTagTextRange = textSelection.GetAsOneRange(); + WParagraph endTagParagraph = endTagTextRange.OwnerParagraph; + int endTagIndex = endTagParagraph.ChildEntities.IndexOf(endTagTextRange); + endTagParagraph.Items.Insert(endTagIndex, bookmarkEnd); + + //Create the bookmark navigator instance to access the bookmark. + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(destinationDocument); + //Move the virtual cursor to the location of the bookmark "Adventure_Bkmk". + bookmarkNavigator.MoveToBookmark("Adventure_Bkmk"); + //Replace the bookmark content with body part. + bookmarkNavigator.ReplaceBookmarkContent(bodyPart); + + //Remove the bookmark from the destination document after replacing the content. + Bookmark bookmark = destinationDocument.Bookmarks.FindByName("Adventure_Bkmk"); + if (bookmark != null) + destinationDocument.Bookmarks.Remove(bookmark); + } + #endregion + } +} \ No newline at end of file diff --git a/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Replace-text-inside-tag.csproj b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Replace-text-inside-tag.csproj new file mode 100644 index 00000000..cc054de5 --- /dev/null +++ b/Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag/Replace-text-inside-tag.csproj @@ -0,0 +1,25 @@ + + + + Exe + net8.0 + Replace_text_inside_tag + + + + + + + + + Always + + + Always + + + Always + + + +