I am able to create barcode using itextsharp CreateDrawingImage method. But I want to include the actual text into the image. How do I do that? or How do I use CreateImageWithBarcode method to save as image (Jpeg/Png)?
Thanks
Facing the same problem and after looking in iTextSharp sources it appears you can't. Besides with CreateDrawingImage() I had problems with image resizing, 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);
I Have this GetBarcode.ashx (generic handler) it saves to the output stream (directly on screen) and to disk.
use it like this:
<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;
}
}
}
You may just draw the text overlay over the image with barcode. If you are generating 1D barcodes like (Code 39, Code 128 etc) then barcode changes its width based on the input value but the height is the constant value.
So you may simply calculate Y coordinate and X coordinates for the text and draw the text with the code like this (based on the code from this 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 may use some commercial components which are doing the calculation of the text position automatically (but you may be better using your own code in case you are already relying on iTextsharp).
Disclaimer: I work for ByteScout, maker of ByteScout BarCode Generator SDK.
This is the function I made 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 originally found code to make this function here: http://forums.asp.net/t/1599409.aspx