|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.Pdf.Barcode; |
| 4 | +using Syncfusion.Pdf.Graphics; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using SizeF = Syncfusion.Drawing.SizeF; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System; |
| 10 | +using Syncfusion.DocIORenderer; |
| 11 | +using Syncfusion.Pdf; |
| 12 | + |
| 13 | +namespace Replace_DISPLAYBARCODE_to_image |
| 14 | +{ |
| 15 | + internal class Program |
| 16 | + { |
| 17 | + static void Main(string[] args) |
| 18 | + { |
| 19 | + //Open the Word document from a file stream |
| 20 | + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) |
| 21 | + { |
| 22 | + //Load the Word document |
| 23 | + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) |
| 24 | + { |
| 25 | + //Replace specific barcode fields in the document with generated barcode images |
| 26 | + ReplaceFieldwithImage(document); |
| 27 | + |
| 28 | + //Save the modified document |
| 29 | + using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) |
| 30 | + { |
| 31 | + document.Save(outputStream1, FormatType.Docx); |
| 32 | + } |
| 33 | + //Create a DocIORenderer instance to convert the Word document to a PDF |
| 34 | + using (DocIORenderer render = new DocIORenderer()) |
| 35 | + { |
| 36 | + //Convert the Word document to a PDF |
| 37 | + using (PdfDocument pdfDocument = render.ConvertToPDF(document)) |
| 38 | + { |
| 39 | + //Save the generated PDF to a file |
| 40 | + using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) |
| 41 | + { |
| 42 | + pdfDocument.Save(outputStream1); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Replaces fields with barcode images based on specific field codes in the document. |
| 52 | + /// </summary> |
| 53 | + /// <param name="document">The Word document object</param> |
| 54 | + private static void ReplaceFieldwithImage(WordDocument document) |
| 55 | + { |
| 56 | + // Find all fields in the document |
| 57 | + List<Entity> fields = document.FindAllItemsByProperty(EntityType.Field, "FieldType", "FieldUnknown"); |
| 58 | + |
| 59 | + // Iterate over all found fields |
| 60 | + foreach (WField field in fields) |
| 61 | + { |
| 62 | + if (field != null) |
| 63 | + { |
| 64 | + // Get the owner paragraph of the field |
| 65 | + WParagraph ownerParagraph = field.OwnerParagraph as WParagraph; |
| 66 | + // Get the index of the field within the paragraph |
| 67 | + int index = ownerParagraph.ChildEntities.IndexOf(field); |
| 68 | + // Split the field code to identify the type of barcode |
| 69 | + string[] components = field.FieldCode.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 70 | + |
| 71 | + // If the field is a QR code (check for "displaybarcode" and "qr") |
| 72 | + if (components[0].ToLower() == "displaybarcode" && components[2].ToLower() == "qr") |
| 73 | + { |
| 74 | + // Get the text to encode into the QR barcode |
| 75 | + string qrBarcodeText = components[1]; |
| 76 | + |
| 77 | + // Initialize default values for optional parameters |
| 78 | + int qValue = -1; |
| 79 | + float sValue = -1; |
| 80 | + float size = 90; |
| 81 | + |
| 82 | + // Extract \q (error correction level) value |
| 83 | + var qMatch = Regex.Match(field.FieldCode, @"\\q (\d+)"); |
| 84 | + if (qMatch.Success) |
| 85 | + { |
| 86 | + qValue = int.Parse(qMatch.Groups[1].Value); |
| 87 | + } |
| 88 | + |
| 89 | + // Extract \s (size) value |
| 90 | + var sMatch = Regex.Match(field.FieldCode, @"\\s (\d+)"); |
| 91 | + if (sMatch.Success) |
| 92 | + { |
| 93 | + sValue = int.Parse(sMatch.Groups[1].Value); |
| 94 | + size = sValue * 1.05f; // Scale the size slightly |
| 95 | + } |
| 96 | + |
| 97 | + // Generate the QR barcode image as a byte array |
| 98 | + byte[] qrCode = GenerateQRBarcodeImage(qrBarcodeText, sValue, qValue, size); |
| 99 | + |
| 100 | + // Create a new picture object to hold the barcode image |
| 101 | + WPicture picture = new WPicture(document); |
| 102 | + picture.LoadImage(qrCode); |
| 103 | + |
| 104 | + // Replace the original field with the picture (QR code) |
| 105 | + ownerParagraph.ChildEntities.Remove(field); |
| 106 | + ownerParagraph.ChildEntities.Insert(index, picture); |
| 107 | + } |
| 108 | + // If the field is a Code39 barcode (check for "displaybarcode" and "code39") |
| 109 | + else if (components[0].ToLower() == "displaybarcode" && components[2].ToLower() == "code39") |
| 110 | + { |
| 111 | + // Get the text to encode into the Code39 barcode |
| 112 | + string qrBarcodeText = components[1]; |
| 113 | + |
| 114 | + // Initialize flags for optional parameters |
| 115 | + bool addText = false; |
| 116 | + bool addCharacter = false; |
| 117 | + |
| 118 | + // Check for the \t option (whether to display text below the barcode) |
| 119 | + var tabMatch = Regex.IsMatch(field.FieldCode, @"\\t"); |
| 120 | + if (tabMatch) |
| 121 | + { |
| 122 | + addText = true; |
| 123 | + } |
| 124 | + |
| 125 | + // Check for the \d option (whether to include start/stop characters) |
| 126 | + var digitMatch = Regex.IsMatch(field.FieldCode, @"\\d"); |
| 127 | + if (digitMatch) |
| 128 | + { |
| 129 | + addCharacter = true; |
| 130 | + } |
| 131 | + |
| 132 | + // Generate the Code39 barcode image as a byte array |
| 133 | + byte[] qrCode = GenerateCODE39Image(qrBarcodeText, addCharacter, addText); |
| 134 | + |
| 135 | + // Create a new picture object to hold the barcode image |
| 136 | + WPicture picture = new WPicture(document); |
| 137 | + picture.LoadImage(qrCode); |
| 138 | + |
| 139 | + // Replace the original field with the picture (Code39 barcode) |
| 140 | + ownerParagraph.ChildEntities.Remove(field); |
| 141 | + ownerParagraph.ChildEntities.Insert(index, picture); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Generates a QR barcode image and converts it to a byte array. |
| 149 | + /// </summary> |
| 150 | + /// <param name="qrBarcodeText">The text to be encoded in the QR code</param> |
| 151 | + /// <param name="sSwitchValue">The size value (\s option) for the QR code</param> |
| 152 | + /// <param name="qSwitchValue">The error correction level (\q option) for the QR code</param> |
| 153 | + /// <param name="size">The size of the QR code image</param> |
| 154 | + /// <returns>A byte array representing the QR code image</returns> |
| 155 | + private static byte[] GenerateQRBarcodeImage(string qrBarcodeText, float sSwitchValue, int qSwitchValue, float size) |
| 156 | + { |
| 157 | + // Create a new QR barcode instance |
| 158 | + PdfQRBarcode qrBarCode = new PdfQRBarcode(); |
| 159 | + |
| 160 | + // Set the text to be encoded |
| 161 | + qrBarCode.Text = qrBarcodeText; |
| 162 | + |
| 163 | + // Set the size if provided |
| 164 | + if (sSwitchValue != -1) |
| 165 | + { |
| 166 | + qrBarCode.XDimension = sSwitchValue; |
| 167 | + } |
| 168 | + |
| 169 | + // Set the error correction level based on the \q switch value |
| 170 | + if (qSwitchValue != -1) |
| 171 | + { |
| 172 | + switch (qSwitchValue) |
| 173 | + { |
| 174 | + case 0: |
| 175 | + qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Low; |
| 176 | + break; |
| 177 | + case 1: |
| 178 | + qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Medium; |
| 179 | + break; |
| 180 | + case 2: |
| 181 | + qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Quartile; |
| 182 | + break; |
| 183 | + case 3: |
| 184 | + qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High; |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Generate the QR code image and return it as a byte array |
| 190 | + Stream barcodeImage = qrBarCode.ToImage(new SizeF(size, size)); |
| 191 | + byte[] byteArray; |
| 192 | + using (MemoryStream ms = new MemoryStream()) |
| 193 | + { |
| 194 | + barcodeImage.CopyTo(ms); |
| 195 | + byteArray = ms.ToArray(); |
| 196 | + } |
| 197 | + return byteArray; |
| 198 | + } |
| 199 | + |
| 200 | + /// <summary> |
| 201 | + /// Generates a Code39 barcode image and converts it to a byte array. |
| 202 | + /// </summary> |
| 203 | + /// <param name="qrBarcodeText">The text to be encoded in the Code39 barcode</param> |
| 204 | + /// <param name="dSwitch">Whether to include start/stop characters (\d option)</param> |
| 205 | + /// <param name="tSwitch">Whether to display the text below the barcode (\t option)</param> |
| 206 | + /// <returns>A byte array representing the Code39 barcode image</returns> |
| 207 | + private static byte[] GenerateCODE39Image(string qrBarcodeText, bool dSwitch, bool tSwitch) |
| 208 | + { |
| 209 | + // Create a new Code39 barcode instance |
| 210 | + PdfCode39Barcode barcode = new PdfCode39Barcode(); |
| 211 | + |
| 212 | + // Configure the barcode based on the provided options |
| 213 | + if (!tSwitch) |
| 214 | + { |
| 215 | + // If \t is not specified, don't display text |
| 216 | + barcode.Text = Regex.Replace(qrBarcodeText.ToUpper(), @"[^A-Z0-9\-\.\ \$\/\+\%]", ""); |
| 217 | + barcode.TextDisplayLocation = TextLocation.None; |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + // If \t is specified, display the barcode text below the image |
| 222 | + barcode.Text = Regex.Replace(qrBarcodeText.ToUpper(), @"[^A-Z0-9\-\.\ \$\/\+\%]", ""); |
| 223 | + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 15); |
| 224 | + barcode.Font = font; |
| 225 | + } |
| 226 | + |
| 227 | + // Generate the barcode image and return it as a byte array |
| 228 | + Stream barcodeImage = barcode.ToImage(new SizeF(40, 40)); |
| 229 | + byte[] byteArray; |
| 230 | + using (MemoryStream ms = new MemoryStream()) |
| 231 | + { |
| 232 | + barcodeImage.CopyTo(ms); |
| 233 | + byteArray = ms.ToArray(); |
| 234 | + } |
| 235 | + return byteArray; |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments