Skip to content

Commit 0b5731c

Browse files
Merge pull request #388 from SyncfusionExamples/ES-643078-Multiple-images-in-single-merge-field
ES-643078- Add the sample Multiple-images-in-single-merge-field
2 parents 5f10647 + 6443846 commit 0b5731c

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-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}") = "Multiple-images-in-single-merge-field", "Multiple-images-in-single-merge-field\Multiple-images-in-single-merge-field.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
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+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.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 = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
78.9 KB
Loading
20.2 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Multiple_images_in_single_merge_field</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Logo1.png">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Logo2.jpg">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Template.docx">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Output\.gitkeep">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace Multiple_images_in_single_merge_field
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
13+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
14+
{
15+
//Loads an existing Word document into DocIO instance.
16+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
17+
{
18+
//Uses the mail merge events handler for image fields.
19+
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_ProductImage);
20+
//Specifies the field names and field values.
21+
string[] fieldNames = new string[] { "Logo" , "Name", "Company" };
22+
string[] fieldValues = new string[] { "AdventureImages", "Nancy Davilo", "Syncfusion" };
23+
//Performs the mail merge
24+
document.MailMerge.Execute(fieldNames, fieldValues);
25+
26+
//Creates file stream.
27+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
28+
{
29+
//Saves the Word document to file stream.
30+
document.Save(outputStream, FormatType.Docx);
31+
}
32+
}
33+
}
34+
}
35+
/// <summary>
36+
/// Represents the method that handles MergeImageField event.
37+
/// </summary>
38+
private static void MergeField_ProductImage(object sender, MergeImageFieldEventArgs args)
39+
{
40+
//Binds image from file system during mail merge
41+
if (args.FieldName == "Logo")
42+
{
43+
//Gets the current merge field owner paragraph.
44+
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
45+
//Gets the current merge field index in the current paragraph.
46+
int mergeFieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
47+
//Gets the folder name from the field value.
48+
string ProductFolderName = args.FieldValue.ToString();
49+
// Define allowed image extensions
50+
string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff" };
51+
// Gets the image names from the Data folder
52+
string[] imageNames = Directory.GetFiles(Path.GetFullPath(@"Data/"))
53+
.Where(file => imageExtensions.Contains(Path.GetExtension(file).ToLower()))
54+
.ToArray();
55+
//Loops through the image names.
56+
foreach (string imageName in imageNames)
57+
{
58+
//Gets the image from file system
59+
FileStream imageStream = new FileStream(imageName, FileMode.Open, FileAccess.Read);
60+
//Creates a new picture.
61+
WPicture picture = new WPicture(paragraph.Document);
62+
//Loads the image into picture.
63+
picture.LoadImage(imageStream);
64+
//Resizes the picture
65+
picture.Height = 50;
66+
picture.Width = 50;
67+
//Inserts the picture at the current merge field index.
68+
paragraph.ChildEntities.Insert(mergeFieldIndex, picture);
69+
mergeFieldIndex++;
70+
}
71+
//Set field value as empty.
72+
args.Text = string.Empty;
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)