|
| 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