Create barcode with text image (not pdf) using itextsharp

僤鯓⒐⒋嵵緔 提交于 2019-12-06 11:49:51

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);

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

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;
        }
    }
}
Eugene

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!