|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.Pdf.Barcode; |
| 4 | +using Syncfusion.Pdf.Graphics; |
| 5 | +using System; |
| 6 | +using System.Data; |
| 7 | +using System.Drawing; |
| 8 | +using System.Drawing.Imaging; |
| 9 | +using System.IO; |
| 10 | + |
| 11 | +namespace Generate_Barcode_labels |
| 12 | +{ |
| 13 | + class Program |
| 14 | + { |
| 15 | + static void Main(string[] args) |
| 16 | + { |
| 17 | + //Opens an existing document from stream through constructor of `WordDocument` class |
| 18 | + FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 19 | + WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic); |
| 20 | + //Creates mail merge events handler for image fields |
| 21 | + document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(InsertBarcode); |
| 22 | + //Gets data to perform mail merge |
| 23 | + DataTable table = GetDataTable(); |
| 24 | + //Performs the mail merge |
| 25 | + document.MailMerge.ExecuteGroup(table); |
| 26 | + |
| 27 | + //Saves and closes the Word document instance |
| 28 | + FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); |
| 29 | + document.Save(outputStream, FormatType.Docx); |
| 30 | + outputStream.Dispose(); |
| 31 | + document.Close(); |
| 32 | + } |
| 33 | + |
| 34 | + #region Helper methods |
| 35 | + /// <summary> |
| 36 | + /// Inserts barcode in the Word document |
| 37 | + /// </summary> |
| 38 | + private static void InsertBarcode(object sender, MergeImageFieldEventArgs args) |
| 39 | + { |
| 40 | + if (args.FieldName == "Barcode") |
| 41 | + { |
| 42 | + //Generates barcode image for field value. |
| 43 | + Stream imageStream= GenerateBarcodeImage(args.FieldValue.ToString()); |
| 44 | + //Sets barcode image stream for merge field |
| 45 | + args.ImageStream = imageStream; |
| 46 | + } |
| 47 | + } |
| 48 | + /// <summary> |
| 49 | + /// Generates barcode image stream. |
| 50 | + /// </summary> |
| 51 | + /// <param name="barcodeText">Barcode text</param> |
| 52 | + /// <returns>Barcode image stream</returns> |
| 53 | + private static Stream GenerateBarcodeImage(string barcodeText) |
| 54 | + { |
| 55 | + //Initialize a new PdfCode39Barcode instance |
| 56 | + PdfCode39Barcode barcode = new PdfCode39Barcode(); |
| 57 | + //Set the height and text for barcode |
| 58 | + barcode.BarHeight = 45; |
| 59 | + barcode.Text = barcodeText; |
| 60 | + //Convert the barcode to image |
| 61 | + Image barcodeImage = barcode.ToImage(new System.Drawing.SizeF(145, 45)); |
| 62 | + //Converts image to stream |
| 63 | + MemoryStream stream = new MemoryStream(); |
| 64 | + barcodeImage.Save(stream, ImageFormat.Png); |
| 65 | + return stream; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Gets the data to perform mail merge |
| 70 | + /// </summary> |
| 71 | + /// <returns></returns> |
| 72 | + private static DataTable GetDataTable() |
| 73 | + { |
| 74 | + // List of products name. |
| 75 | + string[] products = { "Apple Juice", "Grape Juice", "Hot Soup", "Tender Coconut", "Vennila", "Strawberry", "Cherry", "Cone", |
| 76 | + "Butter", "Milk", "Cheese", "Salt", "Honey", "Soap", "Chocolate", "Edible Oil", "Spices", "Paneer", "Curd", "Bread", "Olive oil", "Vinegar", "Sports Drinks", |
| 77 | + "Vegetable Juice", "Sugar", "Flour", "Jam", "Cake", "Brownies", "Donuts", "Egg", "Tooth Brush", "Talcum powder", "Detergent Soap", "Room Spray", "Tooth paste"}; |
| 78 | + |
| 79 | + DataTable table = new DataTable("Product_PriceList"); |
| 80 | + |
| 81 | + // Add fields to the Product_PriceList table. |
| 82 | + table.Columns.Add("ProductName"); |
| 83 | + table.Columns.Add("Price"); |
| 84 | + table.Columns.Add("Barcode"); |
| 85 | + DataRow row; |
| 86 | + |
| 87 | + int Id = 10001; |
| 88 | + // Inserting values to the tables. |
| 89 | + foreach (string product in products) |
| 90 | + { |
| 91 | + row = table.NewRow(); |
| 92 | + row["ProductName"] = product; |
| 93 | + switch (product) |
| 94 | + { |
| 95 | + case "Apple Juice": |
| 96 | + row["Price"] = "$12.00"; |
| 97 | + break; |
| 98 | + case "Grape Juice": |
| 99 | + case "Milk": |
| 100 | + row["Price"] = "$15.00"; |
| 101 | + break; |
| 102 | + case "Hot Soup": |
| 103 | + row["Price"] = "$20.00"; |
| 104 | + break; |
| 105 | + case "Tender coconut": |
| 106 | + case "Cheese": |
| 107 | + row["Price"] = "$10.00"; |
| 108 | + break; |
| 109 | + case "Vennila Ice Cream": |
| 110 | + row["Price"] = "$15.00"; |
| 111 | + break; |
| 112 | + case "Strawberry": |
| 113 | + case "Butter": |
| 114 | + row["Price"] = "$18.00"; |
| 115 | + break; |
| 116 | + case "Cherry": |
| 117 | + case "Salt": |
| 118 | + row["Price"] = "$25.00"; |
| 119 | + break; |
| 120 | + default: |
| 121 | + row["Price"] = "$20.00"; |
| 122 | + break; |
| 123 | + } |
| 124 | + //Add barcode text |
| 125 | + row["Barcode"] = Id.ToString(); |
| 126 | + table.Rows.Add(row); |
| 127 | + Id++; |
| 128 | + } |
| 129 | + return table; |
| 130 | + } |
| 131 | + #endregion |
| 132 | + } |
| 133 | +} |
0 commit comments