How To Generate Multiple Barcode On Single Sheet

In this article, we will learn to generate multiple barcodes on a single sheet using Zen barcode 128 types.

Let’s begin.

Now, Install “Zen.Barcode.Rendering.Framework.Web” in your application

Right-Click of the project name and open the Manage Nuget Packages.

And Install “Zen.Barcode.Rendering.Framework.Web

Install the “iTextSharp” After “Zen.Barcode.Rendering.Framework.Web“.

After installing the Packages and Add DLL in the References.

Download DLL Here.

Extract The File “OnBarcode.BarcodeGenerator.DotnetSuite.Zip” and Add OnBarcode.BarcodeGenerator.DotnetSuite => dll => Net40  => OnBarcode.Barcode.ASPNET.dll  in Refences.

Right-Click of the References and open the Add Reference.

After Open The Browse And Select OnBarcode.BarcodeGenerator.DotnetSuite => dll => Net40  => OnBarcode.Barcode.ASPNET.dll  

And Click Add And Ok Button.

C# Code Example

Open the HomeController.cs  file and add the below code in it.

public ActionResult MultipleBarcodeGenerateonSheet(string barcodeName, int barcodeNumber)
 {

     Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
     Size size = new Size(140, 50);
     var metrics = barcode.GetPrintMetrics(size, size, barcodeName.Length); //6 will replace with string length
     var image = barcode.Draw(barcodeName, metrics);

     var resultImage1 = new Bitmap(image.Width, image.Height + 10);
     using (var graphics1 = Graphics.FromImage(resultImage1))
     using (var font = new System.Drawing.Font("Consolas", 7))
     using (var brush = new SolidBrush(Color.Black))
     using (var format = new StringFormat()
     {
         Alignment = StringAlignment.Center, // Also, horizontally centered text, as in your example of the expected output
         LineAlignment = StringAlignment.Far
     })
     {
         graphics1.Clear(Color.White);
         graphics1.DrawImage(image, 0, 0);
         graphics1.DrawString(barcodeName, font, brush, resultImage1.Width / 2, resultImage1.Height, format);



     }

     var uploadRootFolderInput = AppDomain.CurrentDomain.BaseDirectory + "\\Uploads";
     Directory.CreateDirectory(uploadRootFolderInput);
     var directoryFullPathInput = uploadRootFolderInput;
     string filepath = barcodeName + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Millisecond + ".jpg";
     string fullpath = Path.Combine(directoryFullPathInput, filepath);
     resultImage1.Save(Path.Combine(directoryFullPathInput, filepath), System.Drawing.Imaging.ImageFormat.Png);
     byte[] imageBytes = System.IO.File.ReadAllBytes(fullpath);
     string base64 = Convert.ToBase64String(imageBytes);
     string fileName = System.DateTime.UtcNow.ToString("yyyyMMddHHmmssfff") + ".png";
 
     byte[] bytes = null;
     if (!string.IsNullOrEmpty(base64))
     {
         int index = base64.IndexOf(',');
         if (index != -1) base64 = base64.Substring(index + 1);

         bytes = Convert.FromBase64String(base64);
     }

     MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);

     // Convert byte[] to Image
     ms.Write(bytes, 0, bytes.Length);
     System.Drawing.Image image1 = System.Drawing.Image.FromStream(ms, true);
     //image.Save(Server.MapPath("~/Content/Barcode/" + fileName), System.Drawing.Imaging.ImageFormat.Png);

     iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(fullpath);
     img.ScaleAbsolute(176f, 59f);
     img.SpacingAfter = 22f;

     // Open a new PDF document ****************************************************************************
     const int pageMargin = 40;
     const int pageRows = 10;
     const int pageCols = 3;

     var doc = new Document();
     doc.SetMargins(15, -10, pageMargin, pageMargin);
     var memoryStream = new MemoryStream();

     var pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
     doc.Open();

     // Create the Label table
     PdfPTable table = new PdfPTable(pageCols);
     table.WidthPercentage = 100f;
     table.DefaultCell.Border = 0;

     var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
     for (int i = 1; i <= barcodeNumber; i++)
     {
         #region Label Construction

         PdfPCell cell = new PdfPCell(img);
         cell.Border = 0;
         cell.FixedHeight = (doc.PageSize.Height - 5 - (pageMargin * 2)) / pageRows;
         cell.VerticalAlignment = Element.ALIGN_CENTER;

         //var contents = new Paragraph();
         //contents.Alignment = Element.ALIGN_CENTER;

         //contents.Add(new Chunk(string.Format("Thing #{0}\n", "ThingId_" + i ), new Font(baseFont, 11f, Font.BOLD)));
         //contents.Add(new Chunk(string.Format("Thing Name: {0}\n", "ThingName_" + i), new Font(baseFont, 8f)));

         //cell.AddElement(contents);
         table.AddCell(cell);

         #endregion
     }

     table.CompleteRow();
     doc.Add(table);

     // Close PDF document and send

     pdfWriter.CloseStream = false;
     doc.Close();
     memoryStream.Position = 0;

     return File(memoryStream, "application/pdf");

 }

After Open the Home.cshtml and add the below code in it.

<form method="post" action="/Home/MultipleBarcodeGenerateonSheet" target="_blank">
    <input type="text" name="barcodeName" placeholder="Name" required />
    <input type="number" name="barcodeNumber" placeholder="Number" required />
    <input type="submit" class="btn btn-primary" value="Generate"/>
</form>

Output :

if you have any questions or issues about this article, please let me know.

1 Comment

  1. Yogesh Murgude

    It works wonderful. However the text blurs after converting to image to PDF.

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories