Skip to content

Commit 36d4750

Browse files
author
Hemalatha C
committed
Sample to generate barcode labels in .NET Core
1 parent 36d1cfa commit 36d4750

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30709.64
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generate-Barcode-labels", "Generate-Barcode-labels\Generate-Barcode-labels.csproj", "{767B845C-92BC-4112-B603-53D7E22BDC3C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{767B845C-92BC-4112-B603-53D7E22BDC3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{767B845C-92BC-4112-B603-53D7E22BDC3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{767B845C-92BC-4112-B603-53D7E22BDC3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{767B845C-92BC-4112-B603-53D7E22BDC3C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D37D89FC-B391-4802-87FD-8CB0CA37543D}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RootNamespace>Generate_Barcode_labels</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}
Binary file not shown.

0 commit comments

Comments
 (0)