ASP.NET关于条形码的生成问题兼网页打印

2024-04-17 00:08

本文主要是介绍ASP.NET关于条形码的生成问题兼网页打印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

条形码是迄今为止最经济、实用的一种自动识别技术。条形码技术具有以下几个方面的优点

  A.输入速度快:与键盘输入相比,条形码输入的速度是键盘输入的5倍,并且能实现“即时数据输入”。

  B.可靠性高:键盘输入数据出错率为三百分之一,利用光学字符识别技术出错率为万分之一,而采用条形码技术误码率低于百万分之一。

  C.采集信息量大:利用传统的一维条形码一次可采集几十位字符的信息,二维条形码更可以携带数千个字符的信息,并有一定的自动纠错能力。

  D.灵活实用:条形码标识既可以作为一种识别手段单独使用,也可以和有关识别设备组成一个系统实现自动化识别,还可以和其他控制设备联接起来实现自动化管理。

  另外,条形码标签易于制作,对设备和材料没有特殊要求,识别设备操作容易,不需要特殊培训,且设备也相对便宜。

最近做了个项目需要条形码和打印条形码

这里我收集了两种制作条形码的方法。

第一就是用字符替换生成,这个简单,不需要很多编程。

代码如下:

/// <summary>
        /// 条形码生成
        /// </summary>
        /// <param name="strTemp">要生成条形码的文本</param>
        /// <param name="height">每个_和|的高度</param>
        /// <param name="width">每个_和|的宽度</param>
        /// <param name="showstrTemp">是否显示文本</param>
        /// <example>Response.Write(CreateBarCode("6911989251236", 50, 1, true));</example>
        /// <returns></returns>
        public string CreateBarCode(string text, int height, int width, bool showText)
        {
            string strTemp = text.ToLower();

            //替换各个字符
            strTemp = strTemp.Replace("0", "_|_|__||_||_|"); ;
            strTemp = strTemp.Replace("1", "_||_|__|_|_||");
            strTemp = strTemp.Replace("2", "_|_||__|_|_||");
            strTemp = strTemp.Replace("3", "_||_||__|_|_|");
            strTemp = strTemp.Replace("4", "_|_|__||_|_||");
            strTemp = strTemp.Replace("5", "_||_|__||_|_|");
            strTemp = strTemp.Replace("7", "_|_|__|_||_||");
            strTemp = strTemp.Replace("6", "_|_||__||_|_|");
            strTemp = strTemp.Replace("8", "_||_|__|_||_|");
            strTemp = strTemp.Replace("9", "_|_||__|_||_|");
            strTemp = strTemp.Replace("a", "_||_|_|__|_||");
            strTemp = strTemp.Replace("b", "_|_||_|__|_||");
            strTemp = strTemp.Replace("c", "_||_||_|__|_|");
            strTemp = strTemp.Replace("d", "_|_|_||__|_||");
            strTemp = strTemp.Replace("e", "_||_|_||__|_|");
            strTemp = strTemp.Replace("f", "_|_||_||__|_|");
            strTemp = strTemp.Replace("g", "_|_|_|__||_||");
            strTemp = strTemp.Replace("h", "_||_|_|__||_|");
            strTemp = strTemp.Replace("i", "_|_||_|__||_|");
            strTemp = strTemp.Replace("j", "_|_|_||__||_|");
            strTemp = strTemp.Replace("k", "_||_|_|_|__||");
            strTemp = strTemp.Replace("l", "_|_||_|_|__||");
            strTemp = strTemp.Replace("m", "_||_||_|_|__|");
            strTemp = strTemp.Replace("n", "_|_|_||_|__||");
            strTemp = strTemp.Replace("o", "_||_|_||_|__|");
            strTemp = strTemp.Replace("p", "_|_||_||_|__|");
            strTemp = strTemp.Replace("r", "_||_|_|_||__|");
            strTemp = strTemp.Replace("q", "_|_|_|_||__||");
            strTemp = strTemp.Replace("s", "_|_||_|_||__|");
            strTemp = strTemp.Replace("t", "_|_|_||_||__|");
            strTemp = strTemp.Replace("u", "_||__|_|_|_||");
            strTemp = strTemp.Replace("v", "_|__||_|_|_||");
            strTemp = strTemp.Replace("w", "_||__||_|_|_|");
            strTemp = strTemp.Replace("x", "_|__|_||_|_||");
            strTemp = strTemp.Replace("y", "_||__|_||_|_|");
            strTemp = strTemp.Replace("z", "_|__||_||_|_|");
            strTemp = strTemp.Replace("-", "_|__|_|_||_||");
            strTemp = strTemp.Replace("*", "_|__|_||_||_|");
            strTemp = strTemp.Replace("/", "_|__|__|_|__|");
            strTemp = strTemp.Replace("%", "_|_|__|__|__|");
            strTemp = strTemp.Replace("+", "_|__|_|__|__|");
            strTemp = strTemp.Replace(".", "_||__|_|_||_|");

            //替换字符中的_和|
            strTemp = strTemp.Replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
            strTemp = strTemp.Replace("|", "<span style='height:" + height + ";width:" + width + ";background:#000000;'></span>");

            if (showText)
            {
                return strTemp + "<br/>" + text;
            }
            else
            {
                return strTemp;
            }
        }

效果如下:

 

第二需要用到三方控件,GenCode128.dll

在一个网页codeimage。aspx通过传入的条形码号生成相应的条形码

再在需要的页面用已个IMAGE控件来呈现出来。

先列举codeimage。aspx。CS的代码:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GenCode128;

public partial class common_codeimage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

         string   num = Request["num"].ToString();
           // string num = "1231231232";
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Drawing.Image myimg = Code128Rendering.MakeBarcodeImage(num, 2, true);
        myimg.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
        Response.End();
   
    }
}

这里需要注意两点,第一,写上using GenCode128;同时在BIN文件要应用这个DLL。

接着就很简单了,在需要打印页面上通过事件传值给这个条形码生成页面,原理和验证吗一样的。

  img.ImageUrl = "~/aaa/codeimage.aspx?num=" + "Odf"+Convert.ToString(DataBinder.Eval(e.Item.DataItem, "编号"));

这个就可以看到条形码了 。

最后简单说说打印的部分

这个页面是打印页面,通过SESSION传值生成的多个条形码。

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="rdrytmdy6_list.aspx.cs" Inherits="入滇人员管理_rdrydy6_list" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>省外入滇企业执证人员条码打印</title>
            <script language="Javascript">
function preview()
{
    bdhtml=window.document.body.innerHTML;
    sprnstr="<!--startprint-->";
    eprnstr="<!--endprint-->";
    prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML=prnhtml;
    window.print();
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Button ID="btndaochu" runat="server" Text="导出数据" OnClick="btndaochu_Click" />
          <input type="button" name="print" value="预览并打印" οnclick="preview()">
        <br />
        <br />
        <br />


        <div runat="server" id="aa">
         <!--startprint-->
     <div>

        <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
            <ItemTemplate>
                姓名:<asp:Label ID="lblname" runat="server" Width="133px" Text=<%#Eval("姓名") %>></asp:Label><br />
                工作单位:<asp:Label ID="lblDeptworkname" runat="server" Text=<%#Eval("工作单位") %> Width="431px"></asp:Label><br />
                证书类别:<asp:Label ID="lbltype" runat="server" Text=<%#Eval("执证类别") %> Width="339px"></asp:Label><br />
                证书编号:<asp:Label ID="lblid" runat="server" Text=<%#Eval("执证号码") %> Width="426px"></asp:Label><br />
                <asp:Image ID="imgid" runat="server" /><br />
               &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                   <asp:Label ID="lbl" runat="server"  Width="193px"></asp:Label><br />
            </ItemTemplate>
            <SeparatorTemplate>
                <br />
            </SeparatorTemplate>
        </asp:DataList>
         </div>
     <!--endprint-->

    </div>
    </div>
    </form>
</body>
</html>

 

 

 打印出来的部分就是  <!--startprint-->
和 <!--endprint-->之间的。

顺便附下CS代码:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class 入滇人员管理_rdrydy6_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["table"] != null)
            {
                DataSet ds = (DataSet)Session["table"];
                DataList1.DataSource = ds.Tables[0].DefaultView;
                DataList1.DataBind();
            }
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image img = (Image)e.Item.FindControl("imgid");
            img.ImageUrl = "~/aaa/codeimage.aspx?num=" + "Odf"+Convert.ToString(DataBinder.Eval(e.Item.DataItem, "编号"));
            Label lbl = (Label)e.Item.FindControl("lbl");
            lbl.Text = "Odf" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "编号"));
        }
    }
    protected void btndaochu_Click(object sender, EventArgs e)
    {
        ExportData("application/ms-word", "Word.doc");
    }
    private void ExportData(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);


        //ExpertControl(this, DocumentType.Word);
       
        this.aa.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
}

 

 

希望有高手给点新的见解和指出不足,谢谢

这篇关于ASP.NET关于条形码的生成问题兼网页打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910279

相关文章

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Mysql如何解决死锁问题

《Mysql如何解决死锁问题》:本文主要介绍Mysql如何解决死锁问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录【一】mysql中锁分类和加锁情况【1】按锁的粒度分类全局锁表级锁行级锁【2】按锁的模式分类【二】加锁方式的影响因素【三】Mysql的死锁情况【1

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测