Skip to content

Commit d16a1d2

Browse files
Merge pull request #358 from SindhuRSF4549/930164-Add-Sample
Added Image-bytes-in-DataTable
2 parents cacff38 + e608c9c commit d16a1d2

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-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}") = "Image-bytes-in-DataTable", "Image-bytes-in-DataTable\Image-bytes-in-DataTable.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
Binary file not shown.
20.2 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Image_bytes_in_DataTable</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="Data\image1.gif">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</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 Syncfusion.Drawing;
4+
using System.Collections.Generic;
5+
using System.Data;
6+
using System.IO;
7+
8+
namespace Modify_font_during_mail_merge
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Ngo9BigBOggjHTQxAR8/V1NMaF5cXmBCf1FpRmJGdld5fUVHYVZUTXxaS00DNHVRdkdmWX1cdnRRQ2NcUkZwXUo=");
15+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
16+
{
17+
//Opens the template document.
18+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
19+
{
20+
//Gets the DataTable
21+
DataTable dataTable = GetDataTable();
22+
//Triggers the event
23+
document.MailMerge.MergeImageField += MergeField_ProductImage;
24+
//Performs mail merge
25+
document.MailMerge.Execute(dataTable);
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+
36+
#region Helper methods
37+
private static void MergeField_ProductImage(object sender, MergeImageFieldEventArgs args)
38+
{
39+
if (args.FieldName == "Photo")
40+
{
41+
args.Picture.Height = 85;
42+
args.Picture.Width = 140;
43+
}
44+
}
45+
/// <summary>
46+
/// Gets the data to perform mail merge.
47+
/// </summary>
48+
/// <returns></returns>
49+
private static DataTable GetDataTable()
50+
{
51+
//Creates new DataTable instance
52+
DataTable table = new DataTable();
53+
//Add columns in DataTable
54+
table.Columns.Add("Photo", typeof(byte[]));
55+
table.Columns.Add("ProductName");
56+
table.Columns.Add("ProductNumber");
57+
table.Columns.Add("Size");
58+
table.Columns.Add("Weight");
59+
table.Columns.Add("Price");
60+
61+
//Add record in new DataRow
62+
DataRow row = table.NewRow();
63+
byte[] imageBytes = File.ReadAllBytes(@"Data/image1.gif");
64+
row["Photo"] = imageBytes;
65+
row["ProductName"] = "Mountain-200";
66+
row["ProductNumber"] = "BK-M68B-38";
67+
row["Size"] = "38";
68+
row["Weight"] = "25";
69+
row["Price"] = "$2,294.99";
70+
table.Rows.Add(row);
71+
72+
return table;
73+
}
74+
#endregion
75+
}
76+
}

0 commit comments

Comments
 (0)