Displaying a bar code with iTextSharp using Chris Love's Barcode Handler (2 part)

不打扰是莪最后的温柔 提交于 2020-01-24 23:50:59

问题


I have a situation where I need to create a number of barcodes as images on a page from a set of UPCs from a database.

Additionally, I also want to be able to produce barcodes which can print as labels to a Dymo LabelMaker.

Googling turned up several options, but it seems that for the LabelMaker it'd be wise to produce the barcodes as PDF?

So, I started looking into iTextSharp which seems good (and free!)

Looking fora simple way to render a Barcode Image to a page, I found this, which looks to do exactly what I want, but I can only get it to work locally. When uploaded to the server it just shows an empty image.

So part 1 of my questions is, why is this?

I have checked and double checked the web.config file contains everything required and am pretty sure that Adobe Reader is installed on the server (as was suggested by another post in the link). There's a post in the link which says

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.

Which sounds like it's answering the problem... but doesn't!!

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

Part 2 of my question is, am I heading down the right line to print the individual bar codes to a LabelMaker by using iTextSharp?

Just so you know, I am using .NET 2.0 and coding in VB


回答1:


If you use a .ashx HttpHandler you don't have to mess with web config. It's exactly the same:

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

And in your .html/.aspx file:

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

The src attribute of the img tag must have the same name as the HTTP Handler, in this case barcodeToGif.ashx.



来源:https://stackoverflow.com/questions/9158324/displaying-a-bar-code-with-itextsharp-using-chris-loves-barcode-handler-2-part

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