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