Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Replace-text-with-bookmark-hyperlink/Replace-text-with-bookmark-hyperlink.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Replace_text_with_bookmark_hyperlink
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open an existing Word document
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
{
string textToReplace = "Mexico";
// Initialize text body part
TextBodyPart textBodyPart = new TextBodyPart(document);
// Initialize new paragraph
WParagraph paragraph = new WParagraph(document);
// Add the paragraph to the text body part
textBodyPart.BodyItems.Add(paragraph);
// Append a bookmark hyperlink to the paragraph
paragraph.AppendHyperlink("bookmark", "Mexico", HyperlinkType.Bookmark);
// Replace all occurrences of the target text with the text body part
document.Replace(textToReplace, textBodyPart, false, true);
// Clear the text body part
textBodyPart.Clear();
// Create the output file stream
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
// Save the Word document to the output stream
document.Save(outputFileStream, FormatType.Docx);
}
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Replace_text_with_bookmark_hyperlink</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

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

</Project>
Loading