Skip to content

Commit 327a930

Browse files
Merge pull request #555 from SyncfusionExamples/42194-Mailmerge
42194-How to insert signature in Word document through mail merge?
2 parents 5c840e8 + 15e84eb commit 327a930

7 files changed

Lines changed: 125 additions & 0 deletions

File tree

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 17
4+
VisualStudioVersion = 17.14.37216.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert_Signature_in_Word_Document_through_MailMerge", "Insert_Signature_in_Word_Document_through_MailMerge\Insert_Signature_in_Word_Document_through_MailMerge.csproj", "{EE7EAFF2-30BA-F32D-1FD6-D033BF4D8200}"
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+
{EE7EAFF2-30BA-F32D-1FD6-D033BF4D8200}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EE7EAFF2-30BA-F32D-1FD6-D033BF4D8200}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EE7EAFF2-30BA-F32D-1FD6-D033BF4D8200}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EE7EAFF2-30BA-F32D-1FD6-D033BF4D8200}.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 = {D921C376-3534-4617-B8EF-5CBA4C2FC9DA}
24+
EndGlobalSection
25+
EndGlobal
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<None Update="Data\Template.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\Signature.png">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Output\.gitkeep">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Insert_Signature_in_Word_Document_through_MailMerge
5+
{
6+
class Program
7+
{
8+
9+
static void Main(string[] args)
10+
{
11+
// Load the word document
12+
using (FileStream fileStream = new FileStream(Path.GetFullPath("../../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
13+
{
14+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
15+
{
16+
string[] fieldNames = { "Signature" };
17+
string[] fieldValues = { "signature.png" };
18+
19+
document.MailMerge.MergeImageField += MailMerge_MergeSignature;
20+
//Execute mail merge in the Word document
21+
document.MailMerge.Execute(fieldNames, fieldValues);
22+
using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write))
23+
{
24+
//Saves the stream as Word file
25+
document.Save(outputStream, FormatType.Docx);
26+
}
27+
}
28+
}
29+
}
30+
/// <summary>
31+
/// Binds the image from file system and fit within text box during Mail merge process by using MergeImageFieldEventHandler.
32+
/// </summary>
33+
/// <param name="sender"></param>
34+
/// <param name="args"></param>
35+
private static void MailMerge_MergeSignature(object sender, MergeImageFieldEventArgs args)
36+
{
37+
if (args.FieldName == "Signature")
38+
{
39+
string productFileName = args.FieldValue.ToString();
40+
byte[] imageBytes = File.ReadAllBytes(@"Data/" + productFileName);
41+
MemoryStream imageStream = new MemoryStream(imageBytes);
42+
args.ImageStream = imageStream;
43+
// Get the picture to be merged
44+
WPicture picture = args.Picture;
45+
46+
WTextBox textbox = args.CurrentMergeField.OwnerParagraph.OwnerTextBody.Owner as WTextBox;
47+
// check whether the picture is inside the text box
48+
if (textbox != null)
49+
{
50+
// Get the text box format
51+
WTextBoxFormat textBoxFormat = textbox.TextBoxFormat;
52+
53+
if (textBoxFormat != null)
54+
{
55+
// Resize width
56+
if (picture.Width != textBoxFormat.Width)
57+
{
58+
float widthScale = textBoxFormat.Width / picture.Width * 100;
59+
picture.WidthScale = widthScale;
60+
}
61+
62+
// Resize height
63+
if (picture.Height != textBoxFormat.Height)
64+
{
65+
float heightScale = textBoxFormat.Height / picture.Height * 100;
66+
picture.HeightScale = heightScale;
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)