Skip to content

Commit d909794

Browse files
Set-uniform-image-size
1 parent 983f660 commit d909794

File tree

9 files changed

+115
-0
lines changed

9 files changed

+115
-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}") = "Set-uniform-image-size", "Set-uniform-image-size\Set-uniform-image-size.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
10.4 KB
Loading
13.5 KB
Loading
7.42 KB
Loading
9.69 KB
Loading
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Set_uniform_image_size
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
12+
{
13+
//Opens the template document.
14+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
15+
{
16+
//Uses the mail merge events handler for image fields.
17+
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_ProductImage);
18+
//Specifies the field names and field values.
19+
string[] fieldNames = new string[] { "Logo" , "Picture1", "Picture2", "Picture3"};
20+
string[] fieldValues = new string[] { "Logo.png", "Picture1.gif", "Picture2.gif","Picture3.gif" };
21+
//Executes the mail merge with groups.
22+
document.MailMerge.Execute(fieldNames, fieldValues);
23+
//Creates file stream.
24+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
25+
{
26+
//Saves the Word document to file stream.
27+
document.Save(outputStream, FormatType.Docx);
28+
}
29+
}
30+
}
31+
}
32+
33+
#region Helper methods
34+
/// <summary>
35+
/// Represents the method that handles MergeImageField event.
36+
/// </summary>
37+
private static void MergeField_ProductImage(object sender, MergeImageFieldEventArgs args)
38+
{
39+
//Binds image from file system during mail merge.
40+
if (args.FieldName == "Logo" || args.FieldName == "Picture1" || args.FieldName == "Picture2" || args.FieldName == "Picture3")
41+
{
42+
string ProductFileName = args.FieldValue.ToString();
43+
//Gets the image from file system
44+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/" + ProductFileName), FileMode.Open, FileAccess.Read);
45+
args.ImageStream = imageStream;
46+
//Gets the picture, to be merged for image merge field
47+
WPicture picture = args.Picture;
48+
//Resizes the picture
49+
picture.Height = 50;
50+
picture.Width = 100;
51+
}
52+
}
53+
#endregion
54+
}
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Set_uniform_image_size</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Logo.png">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Picture1.gif">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Picture2.gif">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Data\Picture3.gif">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
<None Update="Data\Template.docx">
27+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
</None>
29+
<None Update="Output\.gitkeep">
30+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
31+
</None>
32+
</ItemGroup>
33+
34+
</Project>

0 commit comments

Comments
 (0)