Skip to content

Commit e195557

Browse files
Merge pull request #330 from SyncfusionExamples/Find-and-replace-merge-field-name
ES-832337- Add the sample Find-and-replace-merge-field-name
2 parents 9c76e10 + 36abcf7 commit e195557

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-replace-merge-field-name", "Find-and-replace-merge-field-name\Find-and-replace-merge-field-name.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Find_and_replace_merge_field_name</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace Find_and_replace_merge_field_name
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
13+
{
14+
// Opens the template Word document.
15+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
16+
{
17+
// Finds all merge fields in the document.
18+
List<Entity> mergeFields = document.FindAllItemsByProperty(EntityType.MergeField, null, null);
19+
// Replaces the merge field name "first_name" with "FirstName".
20+
ReplaceMergeFieldName("Forename", "FirstName", mergeFields);
21+
// Replaces the merge field name "last_name" with "LastName".
22+
ReplaceMergeFieldName("Surname", "LastName", mergeFields);
23+
// Creates a file stream to save the modified document.
24+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
25+
{
26+
// Saves the Word document to the file stream in DOCX format.
27+
document.Save(outputFileStream, FormatType.Docx);
28+
}
29+
}
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Replaces the specified merge field name with a new name in the given list of merge fields.
35+
/// </summary>
36+
private static void ReplaceMergeFieldName(string nameToFind, string nameToReplace, List<Entity> mergeFields)
37+
{
38+
// Iterates through the list of merge fields.
39+
foreach (Entity field in mergeFields)
40+
{
41+
// Checks if the current merge field matches the name to be replaced.
42+
if ((field as WMergeField).FieldName == nameToFind)
43+
{
44+
// Updates the merge field name to the new name.
45+
(field as WMergeField).FieldName = nameToReplace;
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)