Articles in this section
Category / Section

How to replace DISPLAYBARCODE field with an image in Word to PDF conversion?

11 mins read

Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can replace DISPLAYBARCODE field with an image in Word to PDF conversion using C#.

To achieve this, first find the DISPLAYBARCODE field in a Word document, retrieve the barcode type (QR or Code39) and its field code, and then create a DISPLAYBARCODE based on its type and field values using Essential® PDF. Next, convert it to an image and insert it in place of the DISPLAYBARCODE field in the Word document. Finally, perform the Word-to-PDF conversion.

Steps to replace DISPLAYBARCODE field with an image in Word to PDF conversion:

  1. Create a new .NET Core console application project.

    Create console application in Visual Studio

  2. Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your .NET Core application from NuGet.org.

    Install DocIORenderer NuGet package

  3. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your project from NuGet.org.

    Add Syncfusion.Pdf.Imaging.Net.Core NuGet package as reference

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.

  1. Include the following namespaces in Program.cs file
    C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
using SizeF = Syncfusion.Drawing.SizeF;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
  1. Use the following code example to replace DISPLAYBARCODE field with an image in Word to PDF conversion.
    C#
//Open the Word document from a file stream
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
   //Load the Word document
   using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
   {
       //Replace barcode fields in the document with generated barcode images
       ReplaceFieldwithImage(document);
       //Create a DocIORenderer instance to convert the Word document to a PDF
       using (DocIORenderer render = new DocIORenderer())
       {
           //Convert the Word document to a PDF
           using (PdfDocument pdfDocument = render.ConvertToPDF(document))
           {
               //Save the generated PDF to a file
               using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
               {
                   pdfDocument.Save(outputStream1);
               }
           }
       }
   }
}
  1. Use the following code example to replaces fields with barcode images based on specific field codes in the document.
    C#
private static void ReplaceFieldwithImage(WordDocument document)
{
    // Find all fields in the document
    List<Entity> fields = document.FindAllItemsByProperty(EntityType.Field, "FieldType", "FieldUnknown");

    // Iterate over all found fields
    foreach (WField field in fields)
    {
        if (field != null)
        {
            // Get the owner paragraph of the field
            WParagraph ownerParagraph = field.OwnerParagraph as WParagraph;
            // Get the index of the field within the paragraph
            int index = ownerParagraph.ChildEntities.IndexOf(field);
            // Split the field code to identify the type of barcode
            string[] components = field.FieldCode.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            // If the field is a QR code (check for "displaybarcode" and "qr")
            if (components[0].ToLower() == "displaybarcode" && components[2].ToLower() == "qr")
            {
                // Get the text to encode into the QR barcode
                string qrBarcodeText = components[1];
                // Initialize default values for optional parameters
                int qValue = -1;
                float sValue = -1;
                float size = 90;
                // Extract \q (error correction level) value
                var qMatch = Regex.Match(field.FieldCode, @"\\q (\d+)");
                if (qMatch.Success)
                {
                    qValue = int.Parse(qMatch.Groups[1].Value);
                }
                // Extract \s (size) value
                var sMatch = Regex.Match(field.FieldCode, @"\\s (\d+)");
                if (sMatch.Success)
                {
                    sValue = int.Parse(sMatch.Groups[1].Value);
                    size = sValue * 1.05f;  // Scale the size slightly
                }
                // Generate the QR barcode image as a byte array
                byte[] qrCode = GenerateQRBarcodeImage(qrBarcodeText, sValue, qValue, size);
                // Create a new picture object to hold the barcode image
                WPicture picture = new WPicture(document);
                picture.LoadImage(qrCode);
                // Replace the original field with the picture (QR code)
                ownerParagraph.ChildEntities.Remove(field);
                ownerParagraph.ChildEntities.Insert(index, picture);
            }
            // If the field is a Code39 barcode (check for "displaybarcode" and "code39")
            else if (components[0].ToLower() == "displaybarcode" && components[2].ToLower() == "code39")
            {
                // Get the text to encode into the Code39 barcode
                string qrBarcodeText = components[1];
                // Initialize flags for optional parameters
                bool addText = false;
                // Check for the \t option (whether to display text below the barcode)
                var tabMatch = Regex.IsMatch(field.FieldCode, @"\\t");
                if (tabMatch)
                {
                    addText = true;
                }
                // Generate the Code39 barcode image as a byte array
                byte[] qrCode = GenerateCODE39Image(qrBarcodeText, addText);
                // Create a new picture object to hold the barcode image
                WPicture picture = new WPicture(document);
                picture.LoadImage(qrCode);
                // Replace the original field with the picture (Code39 barcode)
                ownerParagraph.ChildEntities.Remove(field);
                ownerParagraph.ChildEntities.Insert(index, picture);
            }
        }
    }
}
  1. Use the following code example to generates a QR barcode image and converts it to a byte array
    C#
/// <summary>
/// Generates a QR barcode image and converts it to a byte array.
/// </summary>
/// <param name="qrBarcodeText">The text to be encoded in the QR code</param>
/// <param name="sSwitchValue">The size value (\s option) for the QR code</param>
/// <param name="qSwitchValue">The error correction level (\q option) for the QR code</param>
/// <param name="size">The size of the QR code image</param>
/// <returns>A byte array representing the QR code image</returns>
private static byte[] GenerateQRBarcodeImage(string qrBarcodeText, float sSwitchValue, int qSwitchValue, float size)
{
   // Create a new QR barcode instance
   PdfQRBarcode qrBarCode = new PdfQRBarcode();
   // Set the text to be encoded
   qrBarCode.Text = qrBarcodeText;

   // Set the size if provided
   if (sSwitchValue != -1)
   {
       qrBarCode.XDimension = sSwitchValue;
   }
   // Set the error correction level based on the \q switch value
   if (qSwitchValue != -1)
   {
       switch (qSwitchValue)
       {
           case 0:
               qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Low;
               break;
           case 1:
               qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Medium;
               break;
           case 2:
               qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.Quartile;
               break;
           case 3:
               qrBarCode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
               break;
       }
   }
   // Generate the QR code image and return it as a byte array
   Stream barcodeImage = qrBarCode.ToImage(new SizeF(size, size));
   byte[] byteArray;
   using (MemoryStream ms = new MemoryStream())
   {
       barcodeImage.CopyTo(ms);
       byteArray = ms.ToArray();
   }
   return byteArray;
} 
  1. Use the following code example to generates a Code39 barcode image and converts it to a byte array.
    C#
/// <summary>
/// Generates a Code39 barcode image and converts it to a byte array.
/// </summary>
/// <param name="qrBarcodeText">The text to be encoded in the Code39 barcode</param>
/// <param name="tSwitch">Whether to display the text below the barcode (\t option)</param>
/// <returns>A byte array representing the Code39 barcode image</returns>
private static byte[] GenerateCODE39Image(string qrBarcodeText, bool tSwitch)
{
   // Create a new Code39 barcode instance
   PdfCode39Barcode barcode = new PdfCode39Barcode();
   // Configure the barcode based on the provided options
   if (!tSwitch)
   {
       // If \t is not specified, don't display text
       barcode.Text = Regex.Replace(qrBarcodeText.ToUpper(), @"[^A-Z0-9\-\.\ \$\/\+\%]", "");
       barcode.TextDisplayLocation = TextLocation.None;
   }
   else
   {
       // If \t is specified, display the barcode text below the image
       barcode.Text = Regex.Replace(qrBarcodeText.ToUpper(), @"[^A-Z0-9\-\.\ \$\/\+\%]", "");
       PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 15);
       barcode.Font = font;
   }
   // Generate the barcode image and return it as a byte array
   Stream barcodeImage = barcode.ToImage(new SizeF(40, 40));
   byte[] byteArray;
   using (MemoryStream ms = new MemoryStream())
   {
       barcodeImage.CopyTo(ms);
       byteArray = ms.ToArray();
   }
   return byteArray;
} 

You can download a complete working sample to replace DISPLAYBARCODE field with an image in Word to PDF conversion from the GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion

I hope you enjoyed learning about how to replace DISPLAYBARCODE field with an image in Word to PDF conversion in .NET Core Word document.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied