-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from SyncfusionExamples/Find-and-replace-merg…
…e-field-name ES-832337- Add the sample Find-and-replace-merge-field-name
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...-and-Replace/Find-and-replace-merge-field-name/.NET/Find-and-replace-merge-field-name.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") = "Find-and-replace-merge-field-name", "Find-and-replace-merge-field-name\Find-and-replace-merge-field-name.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 added
BIN
+14.1 KB
...nd-and-replace-merge-field-name/.NET/Find-and-replace-merge-field-name/Data/Template.docx
Binary file not shown.
22 changes: 22 additions & 0 deletions
22
...ield-name/.NET/Find-and-replace-merge-field-name/Find-and-replace-merge-field-name.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>Find_and_replace_merge_field_name</RootNamespace> | ||
</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> | ||
</ItemGroup> | ||
|
||
</Project> |
1 change: 1 addition & 0 deletions
1
.../Find-and-replace-merge-field-name/.NET/Find-and-replace-merge-field-name/Output/.gitkeep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
50 changes: 50 additions & 0 deletions
50
...place/Find-and-replace-merge-field-name/.NET/Find-and-replace-merge-field-name/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Syncfusion.DocIO; | ||
using Syncfusion.DocIO.DLS; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace Find_and_replace_merge_field_name | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
// Opens the template Word document. | ||
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic)) | ||
{ | ||
// Finds all merge fields in the document. | ||
List<Entity> mergeFields = document.FindAllItemsByProperty(EntityType.MergeField, null, null); | ||
// Replaces the merge field name "first_name" with "FirstName". | ||
ReplaceMergeFieldName("Forename", "FirstName", mergeFields); | ||
// Replaces the merge field name "last_name" with "LastName". | ||
ReplaceMergeFieldName("Surname", "LastName", mergeFields); | ||
// Creates a file stream to save the modified document. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) | ||
{ | ||
// Saves the Word document to the file stream in DOCX format. | ||
document.Save(outputFileStream, FormatType.Docx); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Replaces the specified merge field name with a new name in the given list of merge fields. | ||
/// </summary> | ||
private static void ReplaceMergeFieldName(string nameToFind, string nameToReplace, List<Entity> mergeFields) | ||
{ | ||
// Iterates through the list of merge fields. | ||
foreach (Entity field in mergeFields) | ||
{ | ||
// Checks if the current merge field matches the name to be replaced. | ||
if ((field as WMergeField).FieldName == nameToFind) | ||
{ | ||
// Updates the merge field name to the new name. | ||
(field as WMergeField).FieldName = nameToReplace; | ||
} | ||
} | ||
} | ||
} | ||
} |