Skip to content

Commit

Permalink
Merge pull request SyncfusionExamples#274 from Suriya-Balamurugan/ES-…
Browse files Browse the repository at this point in the history
…899198-Replace-text-inside-tag

Added sample to replace text between custom tags using bookmarks
  • Loading branch information
MohanaselvamJothi authored Oct 28, 2024
2 parents cc4ad5d + c2553fe commit f4b8cb0
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Bookmarks/Replace-text-inside-tag/.NET/Replace-text-inside-tag.sln
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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("<SourceTag>(.*)</SourceTag>"));
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
/// <summary>
/// Replaces the content between specified start and end tags within the destination document using a bookmark.
/// </summary>
/// <param name="destinationDocument">The Word document where the replacement is to be performed.</param>
/// <param name="bodyPart">The content to insert between the specified start and end tags.</param>
private static void ReplaceTextBetweenTags(WordDocument destinationDocument, TextBodyPart bodyPart)
{
//Define the start and end tags to identify the content to be replaced.
string startTag = "<DestTag>";
string endTag = "</DestTag>";
//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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Replace_text_inside_tag</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\DestinationDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\SourceDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit f4b8cb0

Please sign in to comment.