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

相关文章

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明