使用Chris Love的条形码处理程序使用iTextSharp显示条形码(2部分)

时间:2012-02-06 09:54:04

标签: vb.net pdf itextsharp barcode

我的情况是我需要在数据库中从一组UPC中创建一些条形码作为图像。

此外,我还希望能够生成可以作为标签打印到Dymo LabelMaker的条形码。

谷歌搜索出现了几个选项,但似乎对于LabelMaker来说,将条形码生成为PDF是明智的吗?

所以,我开始研究iTextSharp看起来不错(而且免费!)

看一下将条形码图像渲染到页面的简单方法,我找到了这个,它看起来正是我想要的,但我只能让它在本地工作。上传到服务器时,它只显示一个空图像。

因此,问题的第一部分是,为什么会这样?

我已检查并仔细检查了web.config文件包含所需的所有内容,并且非常确定服务器上已安装Adobe Reader(如链接中的其他帖子所示)。链接中有一篇文章说

Hi, Great article, many thanks. I just wanted to put a small update for those running IIS7, if everything works fine when running locally in VS debug mode, but you get a red x when accessing it remotly, you may need to add the handler in the section as well as/or the i.e.

听起来好像是在回答这个问题......但是没有!!

http://professionalaspnet.com/archive/2008/11/09/A-Quick-and-Dirty-Bar-Code-Image-httpHandler.aspx

我的问题的第2部分是,我是否使用iTextSharp向右下方打印单个条形码到LabelMaker?

您知道,我在VB中使用.NET 2.0和编码

1 个答案:

答案 0 :(得分:0)

如果你use a .ashx HttpHandler,你不必弄乱网络配置。它完全一样:

<%@ WebHandler Language="VB" Class="barcodeToGif" %>
Imports System
Imports System.Web
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class barcodeToGif : Implements IHttpHandler
  Public Sub ProcessRequest(ByVal context As HttpContext) _
  Implements IHttpHandler.ProcessRequest
    context.Response.ContentType = "image/gif"
    Dim Request = context.Request
    Dim barcode As String = Request.QueryString("barcode")
    If barcode Is Nothing Then
      barcode = "39"
    End If
    Dim bc39 As New Barcode39()
    bc39.Code = barcode
    Dim bc As System.Drawing.Image = bc39.CreateDrawingImage( _
      System.Drawing.Color.Black, System.Drawing.Color.White _
    )
    bc.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
  End Sub

  Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
      Return False
    End Get
  End Property
End Class

.html/.aspx文件中:

<img src='barcodeToGif.ashx?barcode=12345689' />

src标记的img属性必须与HTTP处理程序具有相同的名称,在本例中为barcodeToGif.ashx