Skip to content

Commit 276a6a7

Browse files
Merge pull request #520 from SyncfusionExamples/913880_replace_existing_text_with_a_bookmark_hyperlink
913880-Added DocIO Sample for replacing existing text with bookmark hyperlink in a word document
2 parents aa949a4 + c85c453 commit 276a6a7

6 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Replace-text-with-bookmark-hyperlink/Replace-text-with-bookmark-hyperlink.csproj" />
3+
</Solution>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Replace_text_with_bookmark_hyperlink
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
11+
{
12+
//Open an existing Word document
13+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
14+
{
15+
string textToReplace = "Mexico";
16+
// Initialize text body part
17+
TextBodyPart textBodyPart = new TextBodyPart(document);
18+
// Initialize new paragraph
19+
WParagraph paragraph = new WParagraph(document);
20+
// Add the paragraph to the text body part
21+
textBodyPart.BodyItems.Add(paragraph);
22+
// Append a bookmark hyperlink to the paragraph
23+
paragraph.AppendHyperlink("bookmark", "Mexico", HyperlinkType.Bookmark);
24+
// Replace all occurrences of the target text with the text body part
25+
document.Replace(textToReplace, textBodyPart, false, true);
26+
// Clear the text body part
27+
textBodyPart.Clear();
28+
// Create the output file stream
29+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
30+
{
31+
// Save the Word document to the output stream
32+
document.Save(outputFileStream, FormatType.Docx);
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_text_with_bookmark_hyperlink</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\Result.docx">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)