Skip to content

Commit c05c897

Browse files
Merge pull request #363 from SyncfusionExamples/ES-263357-Replace-mergefields-multiple-image-sources
ES-263357- Add the sample Mail-merge-images-from-multiple-sources
2 parents 1bbee7d + 5e9955a commit c05c897

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-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}") = "Mail-merge-images-from-multiple-sources", "Mail-merge-images-from-multiple-sources\Mail-merge-images-from-multiple-sources.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
33.8 KB
Loading
26 KB
Loading

Mail-Merge/Mail-merge-images-from-multiple-sources/.NET/Mail-merge-images-from-multiple-sources/Data/base64.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
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>Mail_merge_images_from_multiple_sources</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\base64.txt">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Picture1.png">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Picture2.png">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Data\Template.docx">
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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System;
4+
using System.IO;
5+
using System.Net;
6+
7+
namespace Mail_merge_images_from_multiple_sources
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
// Open the template Word document
14+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
15+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic))
16+
{
17+
// Attach the event handler for merging image fields
18+
document.MailMerge.MergeImageField += MergeField_ProductImage;
19+
20+
// Define merge fields and corresponding image sources
21+
string[] fieldNames = { "ImageFromByteArray", "ImageFromStream", "ImageFromBase64", "ImageFromURL" };
22+
string[] fieldValues = { "Picture1.png", "Picture2.png", "base64.txt", "https://www.syncfusion.com/downloads/support/directtrac/general/AdventureCycle-1316159971.png" };
23+
24+
// Perform the mail merge operation
25+
document.MailMerge.Execute(fieldNames, fieldValues);
26+
27+
// Save the modified document to output
28+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
29+
{
30+
document.Save(outputFileStream, FormatType.Docx);
31+
}
32+
}
33+
}
34+
35+
/// <summary>
36+
/// Event handler to insert images into merge fields.
37+
/// </summary>
38+
private static void MergeField_ProductImage(object sender, MergeImageFieldEventArgs args)
39+
{
40+
string tempFilePath = null;
41+
FileStream imageStream = null;
42+
43+
if (args.FieldName == "ImageFromStream")
44+
{
45+
// Load image from file as a stream
46+
string filePath = Path.Combine("Data", args.FieldValue.ToString());
47+
imageStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
48+
}
49+
else if(args.FieldName == "ImageFromByteArray")
50+
{
51+
byte[] imageBytes = File.ReadAllBytes(@"Data/" + args.FieldValue.ToString());
52+
tempFilePath = Path.GetTempFileName();
53+
File.WriteAllBytes(tempFilePath, imageBytes);
54+
imageStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
55+
}
56+
else if (args.FieldName == "ImageFromBase64")
57+
{
58+
// Convert Base64 string to byte array and save as a temp file
59+
string base64String = File.ReadAllText(Path.Combine("Data", args.FieldValue.ToString()));
60+
byte[] imageBytes = Convert.FromBase64String(base64String);
61+
tempFilePath = Path.GetTempFileName();
62+
File.WriteAllBytes(tempFilePath, imageBytes);
63+
imageStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
64+
}
65+
else if (args.FieldName == "ImageFromURL")
66+
{
67+
// Download image and save as a temp file
68+
WebClient client = new WebClient();
69+
byte[] urlImageBytes = client.DownloadData(args.FieldValue.ToString());
70+
tempFilePath = Path.GetTempFileName();
71+
File.WriteAllBytes(tempFilePath, urlImageBytes);
72+
imageStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
73+
}
74+
75+
if (imageStream != null)
76+
{
77+
args.ImageStream = imageStream;
78+
WPicture picture = args.Picture;
79+
picture.Height = 80;
80+
picture.Width = 150;
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)