使用 itextsharp 创建带有文本图像(不是 pdf)的条形码

发布于 2024-10-08 13:31:49 字数 130 浏览 0 评论 0原文

我可以使用 itextsharp CreateDrawingImage 方法创建条形码。但我想将实际文本包含到图像中。我该怎么做?或如何使用 CreateImageWithBarcode 方法保存为图像(Jpeg/Png)?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽清梦 2024-10-15 13:31:49

面对同样的问题,在查看 iTextSharp 源代码后,看来你不能。除了使用 CreateDrawingImage() 之外,我在调整图像大小方面也遇到了问题,它很模糊且不可读。

我最终使用了这个库(非常酷):

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

EAN13 代码:

System.Drawing.Image imageBarcode = BarcodeLib.Barcode.DoEncode(BarcodeLib.TYPE.EAN13, barcode, true, Color.Black, Color.White, 500, 250);

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);
川水往事 2024-10-15 13:31:49

这是我用来处理条形码的函数:

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

要使用它,您需要执行以下操作:

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

我最初在这里找到了实现此函数的代码:
http://forums.asp.net/t/1599409.aspx

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

你的往事 2024-10-15 13:31:49

我有这个 GetBarcode.ashx (通用处理程序)
它保存到输出流(直接在屏幕上)和磁盘。

像这样使用它:

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

在 HTML 中,这是处理程序:

<%@ 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;
        }
    }
}

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;
        }
    }
}
ㄖ落Θ余辉 2024-10-15 13:31:49

您可以只用条形码在图像上绘制文本叠加层。如果您生成一维条形码,例如(Code 39、Code 128 等),则条形码会根据输入值更改其宽度,但高度是恒定值。

因此,您可以简单地计算文本的 Y 坐标和 X 坐标,并使用如下代码绘制文本(基于此中的代码答案):

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

或者您可以使用一些自动计算文本位置的商业组件(但如果您已经依赖 iTextsharp,您可能最好使用自己的代码)。

免责声明:我在 ByteScout 工作,该公司是 ByteScout BarCode Generator SDK 的制造商.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文