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

相关文章

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

idea突然报错Malformed \uxxxx encoding问题及解决

《idea突然报错Malformeduxxxxencoding问题及解决》Maven项目在切换Git分支时报错,提示project元素为描述符根元素,解决方法:删除Maven仓库中的resolv... 目www.chinasem.cn录问题解决方式总结问题idea 上的 maven China编程项目突然报错,是

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

前端导出Excel文件出现乱码或文件损坏问题的解决办法

《前端导出Excel文件出现乱码或文件损坏问题的解决办法》在现代网页应用程序中,前端有时需要与后端进行数据交互,包括下载文件,:本文主要介绍前端导出Excel文件出现乱码或文件损坏问题的解决办法,... 目录1. 检查后端返回的数据格式2. 前端正确处理二进制数据方案 1:直接下载(推荐)方案 2:手动构造

k8s admin用户生成token方式

《k8sadmin用户生成token方式》用户使用Kubernetes1.28创建admin命名空间并部署,通过ClusterRoleBinding为jenkins用户授权集群级权限,生成并获取其t... 目录k8s admin用户生成token创建一个admin的命名空间查看k8s namespace 的

C#利用Free Spire.XLS for .NET复制Excel工作表

《C#利用FreeSpire.XLSfor.NET复制Excel工作表》在日常的.NET开发中,我们经常需要操作Excel文件,本文将详细介绍C#如何使用FreeSpire.XLSfor.NET... 目录1. 环境准备2. 核心功能3. android示例代码3.1 在同一工作簿内复制工作表3.2 在不同

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe