Create a barcode with a text image (not pdf) using itextsharp

I can create a barcode using the itextsharp CreateDrawingImage method. But I want to include the actual text in the image. How should I do it? or How to use the CreateImageWithBarcode method to save as an image (Jpeg / Png)?

thank

+2
itextsharp barcode
Dec 18 '10 at 7:31
source share
4 answers

Based on the same problem and after searching iTextSharp sources, it seems you cannot. Also, with CreateDrawingImage () I had problems resizing the image, it was fuzzy and unreadable.

I ended up using this library (very cool):

http://www.codeproject.com/KB/graphics/BarcodeLibrary.aspx

Code for EAN13:

System.Drawing.Image imageBarcode = BarcodeLib.Barcode.DoEncode(BarcodeLib.TYPE.EAN13, barcode, true, Color.Black, Color.White, 500, 250); 
+2
Jan 07 2018-11-11T00:
source share

This is the function I created to handle my BarCodes:

 public static Image AddBarCode(ref PdfWriter Writer, string Text, bool ShowText, float ScaleWidth, float ScaleHeight) { // http://forums.asp.net/t/1599409.aspx PdfContentByte cb = Writer.DirectContent; Barcode39 bc39 = new Barcode39(); bc39.Code = Text; // comment next line to show barcode text if (!ShowText) bc39.Font = null; Image barCodeImage = bc39.CreateImageWithBarcode(cb, null, null); barCodeImage.Alignment = PdfAppearance.ALIGN_CENTER; barCodeImage.ScalePercent(ScaleWidth, ScaleHeight); return barCodeImage; } 

To use it, you would do something like this:

 doc.Add(PDFHelper.AddBarCode(ref writer, pitch.DBLabelGroup.BarCode, true, 100, 200)); 

I initially found the code to make this function here: http://forums.asp.net/t/1599409.aspx

0
Jan 07 2018-11-11T00:
source share

I have this GetBarcode.ashx (general handler) it saves the output stream (directly on the screen) and to disk.

use it as follows:

 <img src="GetBarcode.ashx?code=yourcodehere" /> 

in your HTML and here is the handler:

 <%@ WebHandler Language="C#" Class="GetBarcode" %> using System; using System.Configuration; using System.Drawing; using System.Drawing.Imaging; using System.Web; using iTextSharp.text.pdf; using Rectangle = iTextSharp.text.Rectangle; public class GetBarcode : IHttpHandler { private Bitmap bm; public void ProcessRequest (HttpContext context) { string prodCode = context.Request.QueryString.Get("code"); string fileName = ConfigurationManager.AppSettings.Get("absolutePath") + @"bc\" + prodCode + ".gif"; // if already on disk, use it. otherwise create it. try { bm = new Bitmap(fileName); } catch { context.Response.ContentType = "image/gif"; if (prodCode.Length > 0) { Barcode128 code128 = new Barcode128(); code128.CodeType = Barcode.CODE128; code128.ChecksumText = true; code128.GenerateChecksum = true; code128.StartStopText = true; code128.Code = prodCode; bm = new Bitmap(code128.CreateDrawingImage(Color.Black, Color.White)); bm.Save(fileName); // to disk } } bm.Save(context.Response.OutputStream, ImageFormat.Gif); // to screen } public bool IsReusable { get { return false; } } } 
0
Oct 10
source share

You can simply draw text overlay on the barcode image. If you generate 1D barcodes such as (code 39, code 128, etc.), then the barcode changes its width based on the input value, but the height is a constant value.

So, you can simply calculate the Y coordinate and X coordinate for the text and draw the text with this code ( based on the code from this answer ):

  RectangleF rectf = new RectangleF(70, BarCodeBottom, 90, 50); Graphics g = Graphics.FromImage(BarCodeImage); // set text drawing modes for the high quality look g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawString("some barcode caption text", new Font("Tahoma",8), Brushes.Black, rectf); g.Flush(); 

or you can use some commercial components that automatically calculate the text position (but you can better use your own code if you already rely on iTextsharp).

Disclaimer: I work for ByteScout, the Bytecode creator of the BarCode Generator SDK .

0
Feb 16 '15 at 11:10
source share



All Articles