|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.Drawing; |
| 4 | +using Syncfusion.Pdf.Barcode; |
| 5 | +using Syncfusion.Pdf.Graphics; |
| 6 | +using System.Data; |
| 7 | +using System.IO; |
| 8 | + |
| 9 | +namespace Generate_QRCode_from_URL |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + using (FileStream fileStreamPath = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 16 | + { |
| 17 | + //Loads an existing Word document |
| 18 | + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) |
| 19 | + { |
| 20 | + //Uses the mail merge events handler for image fields |
| 21 | + document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_ProductImage); |
| 22 | + //Gets the DataTable |
| 23 | + DataTable dataTable = GetDataTable(); |
| 24 | + //Performs mail merge |
| 25 | + document.MailMerge.Execute(dataTable); |
| 26 | + |
| 27 | + // Saves the Word document file to file system |
| 28 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) |
| 29 | + { |
| 30 | + document.Save(outputStream, FormatType.Docx); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Binds the image from QR code during Mail merge process by using MergeImageFieldEventHandler. |
| 38 | + /// </summary> |
| 39 | + private static void MergeField_ProductImage(object sender, MergeImageFieldEventArgs args) |
| 40 | + { |
| 41 | + //Binds image from QR code during mail merge |
| 42 | + if (args.FieldName == "Website") |
| 43 | + { |
| 44 | + //Initialize a new PdfQRBarcode instance |
| 45 | + PdfQRBarcode QRCode = new PdfQRBarcode(); |
| 46 | + //Set the XDimension and text for barcode |
| 47 | + QRCode.XDimension = 3; |
| 48 | + QRCode.Text = args.FieldValue as string; |
| 49 | + //Convert the QR code to image |
| 50 | + Stream barcodeImage = QRCode.ToImage(new SizeF(300, 300)); |
| 51 | + barcodeImage.Position = 0; |
| 52 | + //Sets QR code image as result |
| 53 | + args.ImageStream = barcodeImage; |
| 54 | + } |
| 55 | + } |
| 56 | + /// <summary> |
| 57 | + /// Gets the data to perform mail merge. |
| 58 | + /// </summary> |
| 59 | + /// <returns></returns> |
| 60 | + private static DataTable GetDataTable() |
| 61 | + { |
| 62 | + //Creates new DataTable instance |
| 63 | + DataTable table = new DataTable(); |
| 64 | + //Add columns in DataTable |
| 65 | + table.Columns.Add("Name"); |
| 66 | + table.Columns.Add("Website"); |
| 67 | + |
| 68 | + //Add record in new DataRow |
| 69 | + DataRow row = table.NewRow(); |
| 70 | + row["Name"] = "Google"; |
| 71 | + row["Website"] = "http://www.google.com"; |
| 72 | + table.Rows.Add(row); |
| 73 | + return table; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
0 commit comments