【问题标题】:Displaying a bar code with iTextSharp using Chris Love's Barcode Handler (2 part)使用 Chris Love 的 Barcode Handler 使用 iTextSharp 显示条形码(2 部分)
【发布时间】:2012-02-27 19:44:20
【问题描述】:

我有一种情况,我需要根据数据库中的一组 UPC 在页面上创建多个条形码作为图像。

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

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

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

寻找一种将条形码图像呈现到页面的简单方法,我发现了这个,它看起来完全符合我的要求,但我只能让它在本地工作。当上传到服务器时,它只显示一个空图像。

所以我的问题的第 1 部分是,为什么会这样?

我已经检查并再次检查了 web.config 文件包含所需的所有内容,并且很确定 Adob​​e 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?

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

【问题讨论】:

  • 您绝对不需要服务器上的 Acrobat 或 Reader,iTextSharp 完全独立于此。当您直接访问资源而不是嵌入在 HTML 中时会发生什么? YOU_SITE/barcode.gif?code=1234567890
  • 404 Error, page cannot be found 我想知道我是否需要一个空白的 .gif 图像,或者在 Web.Config 中的 <add verb="GET" path="barcode.gif" validate="false" type="BarCodeHandler" /> 引用之外对它的引用?

标签: vb.net pdf itextsharp barcode


【解决方案1】:

如果你 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' />

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

【讨论】:

  • 这看起来很有趣。我从来没有使用过这样的东西,所以请原谅我的无知,我将如何将实际的条形码传递到该脚本中?我试过运行它,但仍然得到“找不到文件”
  • 条形码的访问方式与您提供的链接相同。首先初始化HttpRequestDim Request As HttpRequest = context.Request,然后访问QueryString属性:Request.QueryString("MY_QUERYSTRING_PARAMETER")
  • 对不起,我可能真的很笨,但我无法让它工作!我明白你在说什么,但不知道把每一行放在哪里。 Dim Request As HttpRequest = context.Request 需要进入我的页面脚本,还是在处理程序类中?我已经尝试了后者并且遇到了image not found 问题,将bc39.Code = "39" 行更改为bc39.Code = request.querystring("code"),这就是我读取要呈现的实际代码位置的方式。
  • *.ashx 文件应默认映射为开箱即用。您可以通过在此处查看您的服务器进行验证:\%SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG`. This would **NOT** be the case if you altered your web.config` 通过添加 &lt;clear /&gt; as the first node in the ` 部分 - 这有效地删除了默认值。
  • 下载visual studio express,(VB版)进入源代码目录,双击名为itextsharp.sln的文件。编辑AssemblyInfo.cs 并构建解决方案。 (在Configuration Manager 中选择ReleaseAny CPU。您说您使用的是.NET 2.0,因此您可能想看看是否可以在微软网站上找到2005/2008 版本而不是2010 版本。
猜你喜欢
  • 1970-01-01
  • 2023-02-02
  • 2016-08-02
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
相关资源
最近更新 更多