10

I am using iTextSharp in my application to generate a barcode. Though everything is working as I wanted, I need to display the value of the barcode under the barcode like the one showin in the attached image. Sample Barcode

Below is the C# code I use:

Barcode39 barcodeImg = new Barcode39();
barcodeImg.Code = barcodeValue.ToString();
barcodeImg.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White).Save(stream, ImageFormat.Png);
Mark Storer
  • 15,672
  • 3
  • 42
  • 80
acadia
  • 2,619
  • 18
  • 55
  • 72

4 Answers4

8

This is code I found while searching the net. Hope this solves your problem:

string prodCode = context.Request.QueryString.Get("code");
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;
  System.Drawing.Bitmap bm    = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
  bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);            
} 
Mark Storer
  • 15,672
  • 3
  • 42
  • 80
Prashant
  • 966
  • 9
  • 26
  • Sorry the text is not still appearing under the barcode. Thanks for trying to help. – acadia Feb 07 '11 at 13:25
  • 6
    Lol! that snippet looks just like the one on my blog! http://www.jphellemons.nl/post/Make-a-code128-barcode-with-C-sharp-and-iTextSharp.aspx – JP Hellemons Oct 19 '11 at 13:19
4

There is no direct way to display text below the barcode image using iTextSharp dll. I tried the same and had a workaround to display the text. This is not a direct solution, but will provide output similar to what is expected out of a barcode image.

To generate the barcode image, i used the inputs shared by JP Hellemons in his blog. Thanks JP Hellemons! http://www.jphellemons.nl/post/Make-a-code128-barcode-with-C-sharp-and-iTextSharp.aspx

The code that i used:

Barcode128 code128 = new Barcode128();
code128.CodeType = iTextSharp.text.pdf.Barcode.CODE128;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.StartStopText = false;
code128.Code = <<Barcode value>>;

// Create a blank image 
System.Drawing.Bitmap bmpimg = new Bitmap(120,35); // provide width and height based on the barcode image to be generated. harcoded for sample purpose

Graphics bmpgraphics = Graphics.FromImage(bmpimg);
bmpgraphics.Clear(Color.White); // Provide this, else the background will be black by default

// generate the code128 barcode
bmpgraphics.DrawImage(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new Point(0, 0));

 //generate the text below the barcode image. If you want the placement to be dynamic, calculate the point based on size of the image
bmpgraphics.DrawString(<<Barcode value>>, new System.Drawing.Font("Arial", 8, FontStyle.Regular), SystemBrushes.WindowText, new Point(15, 24));

// Save the output stream as gif. You can also save it to external file
bmpimg.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

Correct me if I was wrong..

If any of you have a direct or simpler solution, please share them..

Peter
  • 41
  • 1
  • 2
2
iTextSharp.text.pdf.Barcode128 bc = new Barcode128();
bc.TextAlignment = Element.ALIGN_CENTER;
bc.Code = "anything";
bc.StartStopText = false;
bc.CodeType = iTextSharp.text.pdf.Barcode128.CODE128;
bc.Extended = true;
//bc.Font = null;

iTextSharp.text.Image PatImage1 = bc.CreateImageWithBarcode(cb, iTextSharp.text.BaseColor.BLACK, iTextSharp.text.BaseColor.BLACK);
PatImage1.ScaleToFit(160, 20);

PdfPTable p_detail1 = new PdfPTable(1);
p_detail1.WidthPercentage = 100;

PdfPCell barcideimage = new PdfPCell(PatImage1);
//barcideimage.Colspan = 2;
barcideimage.HorizontalAlignment = 2;
barcideimage.Border = 0;
p_detail1.AddCell(barcideimage);

l1.Add(p_detail1);
user3079834
  • 2,009
  • 2
  • 31
  • 63
Aryan
  • 21
  • 1
0

Ah. You need to set the font. I recommend using FontFactory.registerDirectories() and FontFactory.getFont( name ).

registerDirectories() will enumerate all the directories OSes are known to use to store fonts. Windows (of whatever flavor) is no problem . Once that's done, you can easily get fonts from the factory. All the barcode symbologies I'm aware of should have no problem with the default encoding, so you really just need to ask for a basic font name rather than knowing a path. FontFactory tracks all the path info for you. Handy.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • Mark its not the font. I am looking for displaying the value under the barcode like the one show in the attached image. Thanks – acadia Mar 03 '11 at 21:51
  • Thats right. And to tell a barcode to display its text, you set a font. Says so right there in the [JavaDoc](http://api.itextpdf.com/com/itextpdf/text/pdf/Barcode.html) for `Barcode.setFont()`. Odd grammar, but the idea is there. Strangely enough, the default constructor for Barcode128 initializes the font to Helvetica/WinAnsiEncoding. What version of iText are you using, and what text are you trying to display? – Mark Storer Mar 03 '11 at 23:35
  • I don't think this is feasible... I've tried every option I could think of and couldn't get the desired result... if someone does manage to get this working, a real answer would be great... (@Mark Storer - it's not iText, it's iTextSharp - a port to .NET) – veljkoz Jul 18 '11 at 18:04
  • They're virtually interchangeable. "gets" often but not always become properties, and all the function names start with upper case letters. I repeat, what version of iTextSHARP are you using, and what text are you trying to display? – Mark Storer Jul 18 '11 at 18:32