How to add QR code to word using C#, VB.NET.docx
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies.
Using this library, you can add a QR code to a Word document in C# and VB.NET. Below steps should be followed:
- Design your template Word document with the required layout and merge fields using Microsoft Word
- Using Syncfusion PDF library you can generate a QR code.
- Insert the generated QR code as an image into the Word document with MergeImageField event using Essential® DocIO.
Steps to add a QR code to a Word document using C# and VB.NET:
- Create a new C# console application project.
- Install the Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.Pdf.Barcode; using Syncfusion.Pdf.Graphics;
VB
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS Imports Syncfusion.Pdf.Barcode Imports Syncfusion.Pdf.Graphics
- Use the following code to add a QR code to a Word document.
C#
// Open an existing document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Template.docx"), FormatType.Automatic))
{
// Create mail merge events handler for image fields
document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(InsertQRBarcode);
// Get data to perform mail merge
DataTable table = GetDataTable();
// Perform the mail merge
document.MailMerge.ExecuteGroup(table);
// Save and close the Word document instance
document.Save("Result.docx", FormatType.Docx);
}
System.Diagnostics.Process.Start("Result.docx");
VB
' Open an existing document.
Using document As WordDocument = New WordDocument(Path.GetFullPath("../../Template.docx"), Syncfusion.DocIO.FormatType.Automatic)
' Create mail merge events handler for image fields
AddHandler document.MailMerge.MergeImageField, AddressOf InsertQRBarcode
Dim table As DataTable = GetDataTable()
' Perform the mail merge
document.MailMerge.ExecuteGroup(table)
' Save and close the Word document instance
document.Save("Result.docx", Syncfusion.DocIO.FormatType.Docx)
End Using
System.Diagnostics.Process.Start("Result.docx")
- Include the following helper method to insert QR code image using MergeImageField event.
C#
private static void InsertQRBarcode(object sender, MergeImageFieldEventArgs args)
{
WParagraph paragraph = new WParagraph(args.Document);
if (args.FieldName == "QRBarcode")
{
// Generate barcode image for field value.
Image barcodeImage = GenerateQRBarcodeImage(args.FieldValue.ToString());
// Set barcode image for merge field
args.Image = barcodeImage;
}
}
VB
Private Sub InsertQRBarcode(ByVal sender As Object, ByVal args As MergeImageFieldEventArgs)
Dim paragraph As WParagraph = New WParagraph(args.Document)
If (args.FieldName = "QRBarcode") Then
' Generate barcode image for field value.
Dim barcodeImage As Image = GenerateQRBarcodeImage(args.FieldValue.ToString)
' Set barcode image for merge field
args.Image = barcodeImage
End If
End Sub
- Include the following helper method to generate a QR code image using the Syncfusion PDF library.
C#
private static Image GenerateQRBarcodeImage(string qrBarcodeText)
{
// Drawing QR Barcode
PdfQRBarcode barcode = new PdfQRBarcode();
// Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
// Set XDimension
barcode.XDimension = 3;
barcode.Text = qrBarcodeText;
// Convert the barcode to image
Image barcodeImage = barcode.ToImage(new SizeF(125.5f, 135.5f));
return barcodeImage;
}
VB
Private Function GenerateQRBarcodeImage(ByVal qrBarcodeText As String) As Image
' Drawing QR Barcode
Dim barcode As PdfQRBarcode = New PdfQRBarcode
' Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
' Set XDimension
barcode.XDimension = 3
barcode.Text = qrBarcodeText
' Convert the barcode to image
Dim barcodeImage As Image = barcode.ToImage(New SizeF(125.5F, 135.5F))
Return barcodeImage
End Function
- Include the following helper method to get data for Mail merge execution process.
C#
private static DataTable GetDataTable() { // List of product names. string[] products = { "Apple Juice", "Grape Juice", "Hot Soup", "Tender Coconut", "Vennila", "Strawberry", "Cherry", "Cone", "Butter", "Milk", "Cheese", "Salt", "Honey", "Soap", "Chocolate", "Edible Oil", "Spices", "Paneer", "Curd", "Bread", "Olive oil", "Vinegar", "Sports Drinks", "Vegetable Juice", "Sugar", "Flour", "Jam", "Cake", "Brownies", "Donuts", "Egg", "Tooth Brush", "Talcum powder", "Detergent Soap", "Room Spray", "Tooth paste", "Apple Juice", "Grape Juice", "Hot Soup", "Tender Coconut", "Vennila", "Strawberry", "Cherry", "Cone", "Butter", "Milk", "Cheese", "Salt", "Honey", "Soap", "Chocolate", "Edible Oil", "Spices", "Paneer", "Curd", "Bread", "Olive oil", "Vinegar", "Sports Drinks", "Vegetable Juice", "Sugar", "Flour", "Jam", "Cake", "Brownies", "Donuts", "Egg", "Tooth Brush", "Talcum powder", "Detergent Soap", "Room Spray", "Tooth paste"}; DataTable table = new DataTable("Product_PriceList"); // Add fields to the Product_PriceList table. table.Columns.Add("ProductName"); table.Columns.Add("Price"); table.Columns.Add("QRBarcode"); DataRow row; int Id = 10001; // Insert values into the table. foreach (string product in products) { row = table.NewRow(); row["ProductName"] = product; switch (product) { case "Apple Juice": row["Price"] = "$12.00"; break; case "Grape Juice": case "Milk": row["Price"] = "$15.00"; break; case "Hot Soup": row["Price"] = "$20.00"; break; case "Tender Coconut": case "Cheese": row["Price"] = "$10.00"; break; case "Vennila Ice Cream": row["Price"] = "$15.00"; break; case "Strawberry": case "Butter": row["Price"] = "$18.00"; break; case "Cherry": case "Salt": row["Price"] = "$25.00"; break; default: row["Price"] = "$20.00"; break; } //Add barcode text row["QRBarcode"] = Id.ToString(); table.Rows.Add(row); Id++; } return table; }
VB
Private Function GetDataTable() As DataTable ' List of product names. Dim products() As String = New String() { "Apple Juice", "Grape Juice", "Hot Soup", "Tender Coconut", "Vennila", "Strawberry", "Cherry", "Cone", "Butter", "Milk", "Cheese", "Salt", "Honey", "Soap", "Chocolate", "Edible Oil", "Spices", "Paneer", "Curd", "Bread", "Olive oil", "Vinegar", "Sports Drinks", "Vegetable Juice", "Sugar", "Flour", "Jam", "Cake", "Brownies", "Donuts", "Egg", "Tooth Brush", "Talcum powder", "Detergent Soap", "Room Spray", "Tooth paste", "Apple Juice", "Grape Juice", "Hot Soup", "Tender Coconut", "Vennila", "Strawberry", "Cherry", "Cone", "Butter", "Milk", "Cheese", "Salt", "Honey", "Soap", "Chocolate", "Edible Oil", "Spices", "Paneer", "Curd", "Bread", "Olive oil", "Vinegar", "Sports Drinks", "Vegetable Juice", "Sugar", "Flour", "Jam", "Cake", "Brownies", "Donuts", "Egg", "Tooth Brush", "Talcum powder", "Detergent Soap", "Room Spray", "Tooth paste" } Dim table As DataTable = New DataTable("Product_PriceList") ' Add fields to the Product_PriceList table. table.Columns.Add("ProductName") table.Columns.Add("Price") table.Columns.Add("QRBarcode") Dim row As DataRow Dim Id As Integer = 10001 ' Insert values into the table. For Each product As String In products row = table.NewRow() row("ProductName") = product Select Case (product) Case "Apple Juice" row("Price") = "$12.00" Case "Grape Juice", "Milk" row("Price") = "$15.00" Case "Hot Soup" row("Price") = "$20.00" Case "Tender Coconut", "Cheese" row("Price") = "$10.00" Case "Vennila Ice Cream" row("Price") = "$15.00" Case "Strawberry", "Butter" row("Price") = "$18.00" Case "Cherry", "Salt" row("Price") = "$25.00" Case Else row("Price") = "$20.00" End Select 'Add barcode text row("QRBarcode") = Id.ToString table.Rows.Add(row) Id = (Id + 1) Next Return table End Function
A complete working example of how to add a QR code to a Word document using C# can be downloaded from add QR code to Word.zip
Input Word document with merge fields as follows.
By executing the program, you will get the output Word document with QR code labels as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a 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 a trial message.
I hope you enjoyed learning about how to add a QR code to Word using C#, VB.NET.
You can refer to our WinForms word feature tour page to know about its other groundbreaking features and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms word 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 or feedback portal. We are always happy to assist you!