.net实现生成及打印条形码功能,下面简单总结了自己的实现方法。
1.引用dll文件
将FLX.WebControls.dll文件拷贝到顶目的bin文件夹目录中;
2.配置web.config文件
在<system.web></system.web>之间添加如下代码
<httpHandlers>
<add verb="*" path="FlxBarCode.axd" type="FLX.WebControls.FlxBarCodeHandler,FLX.WebControls" />
</httpHandlers>
3.新建aspx文件,代码如下
前台文件:
<HTML>
<HEAD>
<title>打印条形码</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="Css/baseStyle.css" type="text/css" rel="stylesheet">
<style media="print">.noprint { DISPLAY: none }
</style>
<script language="javascript" src="scripts/print.js" charset="gb2312"></script>
<STYLE> DIV.page { PAGE-BREAK-BEFORE: always } </STYLE>
</HEAD>
<body>
<form id="Form2" method="post" runat="server">
<span id="message" class="noprint"></span>
<br>
<span id="span" class="noprint"></span>
<br>
<br>
<table border="1" cellspacing="0" cellpadding="0" width="70" bordercolor="#000000">
<tr>
<td>
<table>
<tr>
<td width="100%" valign="top" align="center"><asp:Label id="LblName" runat="server" Font-Size="14px" Font-Bold="True"></asp:Label></td>
</tr>
<tr>
<td width="100%" valign="top">
<p align="right" style="PADDING-RIGHT:5px">
<asp:Image id="ImageCode" runat="server"></asp:Image>
</p>
<P><FONT face="宋体"></FONT></P>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>
protected System.Web.UI.WebControls.Image ImageCode;
protected System.Web.UI.WebControls.Label LblName;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.LblName.Text =Request.QueryString["Name"].ToString();
this.ImageCode.ImageUrl =Request.QueryString["Url"].ToString();
}
String.prototype.getQuery = function(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = this.substr(this.indexOf("/?")+1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
//创建WebBrowser控件
var createWebBrowser = function(){
document.getElementById("span").innerHTML = '<object id="WebBrowser" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
}
//创建按钮
var createButton = function(vID,vValue,eventHandler){
var btnButton = document.createElement("INPUT");
btnButton.type ="Button";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.onclick = eventHandler;
document.getElementById("span").appendChild(btnButton);
}
//创建标签
var createSpan = function(vID,vValue,eventHandler){
var htmSpan = document.createElement("span");
htmSpan.id = vID;
htmSpan.innerHTML = vValue;
if(eventHandler != null)
htmSpan.onclick = eventHandler;
document.getElementById("span").appendChild(htmSpan);
}
//创建标签
var createTextBox = function(vID,vValue,eventHandler,width){
var btnButton = document.createElement("INPUT");
btnButton.type ="text";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.width = width;
if(eventHandler != null)
btnButton.onclick = eventHandler;
document.getElementById("span").appendChild(btnButton);
}
//按钮事件
var print = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,1);
}
var directPrint = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,6);
}
var PrintSetUp = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(8,1);
}
var PrintReview = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(7,1);
}
var PrintFirst = function(){
window.location.href = window.location.href
.replace(//?PageIndex=(/d)*$/g,"").replace(//?PageIndex=(/d)*&/g,"?").replace(/&PageIndex=(/d)*&/g,"&").replace(/&PageIndex=(/d)*$/g,"")
+"&PageIndex=1";
}
var PrintLast = function(){
var last = parseInt(window.location.toString().getQuery('LastPage'));
window.location.href = window.location.href
.replace(//?PageIndex=(/d)*$/g,"").replace(//?PageIndex=(/d)*&/g,"?").replace(/&PageIndex=(/d)*&/g,"&").replace(/&PageIndex=(/d)*$/g,"")
+"&PageIndex="+last;
}
var PrintPre = function(){
var index = parseInt(window.location.toString().getQuery('PageIndex'));
if(index>1)
window.location.href = window.location.href
.replace(//?PageIndex=(/d)*$/g,"").replace(//?PageIndex=(/d)*&/g,"?").replace(/&PageIndex=(/d)*&/g,"&").replace(/&PageIndex=(/d)*$/g,"")
+"&PageIndex="+(index-1);
else alert("到头了");
}
var PrintNext = function(){
var index = parseInt(window.location.toString().getQuery('PageIndex'));
var last = parseInt(window.location.toString().getQuery('LastPage'));
if((index+1)<=last)
window.location.href = window.location.href
.replace(//?PageIndex=(/d)*$/g,"").replace(//?PageIndex=(/d)*&/g,"?").replace(/&PageIndex=(/d)*&/g,"&").replace(/&PageIndex=(/d)*$/g,"")
+"&PageIndex="+(index+1);
else alert("到头了");
}
var ShowPageIndex= function(){
}
var GoPage= function(){
var pageSize = document.getElementById('txtPageSize').value.replace(//s+/,"");
if(/^/d+$/.test(pageSize))
{
var href = window.location.href.replace(//?PageIndex=(/d)*$/g,"").replace(//?PageIndex=(/d)*&/g,"?").replace(/&PageIndex=(/d)*&/g,"&").replace(/&PageIndex=(/d)*$/g,"")
.replace(//?LastPage=(/d)*$/g,"").replace(//?LastPage=(/d)*&/g,"?").replace(/&LastPage=(/d)*&/g,"&").replace(/&LastPage=(/d)*$/g,"")
.replace(//?PageSize=(/d)*$/g,"").replace(//?PageSize=(/d)*&/g,"?").replace(/&PageSize=(/d)*&/g,"&").replace(/&PageSize=(/d)*$/g,"")
.replace(//?pagesize=(/d)*$/g,"").replace(//?pagesize=(/d)*&/g,"?").replace(/&pagesize=(/d)*&/g,"&").replace(/&pagesize=(/d)*$/g,"")
+"&PageSize="+pageSize;
window.location.href = href;
}
else
{
alert("请输入数字");
document.getElementById('txtPageSize').focus();
}
}
//设置网页打印的页眉页脚为空
function pagesetup_null(){
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER"
hkey_path="//Software//Microsoft//Internet Explorer//PageSetup//";
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="header";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
hkey_key="footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
}catch(e){}
}
window.onload = function(){
document.getElementById('span').innerHTML = "";
createWebBrowser();
createButton("btn0","打印",print);
createButton("btn1","直接打印",directPrint);
createButton("btn2","页面设置",PrintSetUp);
createButton("btn3","打印预览",PrintReview);
if(window.location.toString().getQuery('PageSize') != null || window.location.toString().getQuery('pagesize') != null)
{
createButton("btn4","第一页",PrintFirst);
createButton("btn5","上一页",PrintPre);
createButton("btn6","下一页",PrintNext);
createButton("btn7","末 页",PrintLast);
var index = parseInt(window.location.toString().getQuery('PageIndex'));
var last = parseInt(window.location.toString().getQuery('LastPage'));
createSpan("span0"," 当前第 "+index+"/"+last+" 页 ",null);
if(index==1){document.getElementById("btn5").disabled = true;document.getElementById("btn4").disabled = true;}
if(index==last){document.getElementById("btn6").disabled = true;document.getElementById("btn7").disabled = true;}
if(window.location.toString().getQuery('UserSetSize') != null || window.location.toString().getQuery('usersetsize') != null)
{
var usersetsize = "";
if(window.location.toString().getQuery('UserSetSize') != null)
usersetsize = window.location.toString().getQuery('UserSetSize');
else if(window.location.toString().getQuery('usersetsize') != null)
usersetsize = window.location.toString().getQuery('usersetsize');
if(usersetsize.toUpperCase()=='Y')
{
createSpan("span1","每页显示",null);
var pagesize = 0;
if(window.location.toString().getQuery('PageSize') != null)
pagesize = parseInt(window.location.toString().getQuery('PageSize'));
else if(window.location.toString().getQuery('pagesize') != null)
pagesize = parseInt(window.location.toString().getQuery('pagesize'));
createTextBox("txtPageSize",pagesize,null,40);
createButton("PageSizeSet","设 置",GoPage);
}
}
}
var mess = "<font color=red>如系统中的打印功能无法正常使用,请更改IE浏览器设置:<br>";
mess +="更改步骤:1、选择IE功能菜单中工具(T)—>Internet选项(O);<br>";
mess +=" 2、在弹出的“Internet选项”窗口中,选择“安全”标签;<br>";
mess +=" 3、选择“自定义级别(C)”;<br>";
mess +=" 4、在弹出的“安全设置”窗口中,找到“ActiveX 控件和插件”项;<br>";
mess +=" 5、将其下面所有项选择为“启用”,然后“确定”。</font>";
document.getElementById('message').innerHTML = mess;
}
body
{
background-color:White;
font-family:宋体;
font-size:9pt;
color:#3F3F3F;
}
td, div
{
font-family:宋体;
font-size:9pt;
}
a
{
color:#DD3409;
text-decoration:none;
}
a:hover
{
color:#DD3409;
text-decoration:underline;
}
ul
{
list-style-image: url(images/bullet.gif);
list-style-type:square;
list-style-position:outside;
margin-left:18px;
margin-top:5px;
color:#666666;
padding-left:2px;
}
ol
{
list-style-position:outside;
margin-left:25px;
margin-top:5px;
}
h1
{
font-family:宋体;
font-weight:bold;
font-size:18px;
color:#3F3F3F;
margin-top:12px;
margin-bottom:40px;
}
h2
{
font-family:宋体;
font-weight:bold;
font-size:18px;
color:#3F3F3F;
margin-top:15px;
margin-bottom:15px;
}
h3
{
font-family:宋体;
font-weight:bold;
font-size:12px;
margin-top:2px;
margin-bottom:2px;
}
.TopNavLink
{
font-size:9pt;
color:#3F3F3F;
}
.WelcomeTitle
{
font-family:宋体;
font-size:23px;
font-weight:900;
letter-spacing:-0.2mm;
margin-top:9pt;
margin-bottom:20px;
}
.MainText
{
font-size:9pt;
}
.SmallText
{
font-size:9pt;
}
.Hint
{
color:Red;
}
.DemoArea
{
border:1px silver solid;
padding:8px;
background-color:#F6F6F6;
}
.AboutBox
{
border:1px #E6E6E6 solid;
background-color:#DDDDDD;
background-image: url(images/about_background.gif);
}
.SidePanel
{
padding: 9pt;
border:1px;
border-color:#AAAAAA;
border-style: solid;
background-color:#F3F3F3;
font-size:9pt;
color:#666666;
}
.DemosNav
{
padding: 0px;
border:1px;
border-color:#AAAAAA;
border-style: solid;
background-color:#F3F3F3;
}
.DemoCategory
{
color:black;
font-family: 宋体;
font-size: 9pt;
font-weight:bold;
cursor: default;
}
.DemoCategoryOver
{
color:black;
font-family: 宋体;
font-size: 9pt;
font-weight:bold;
text-decoration:underline;
cursor: default;
}
.Demo
{
font-family: 宋体;
font-size: 9pt;
padding: 1px;
padding-left:2px;
padding-right:2px;
border:1px;
border-color:#F3F3F3;
border-style: solid;
cursor: default;
}
.DemoHover
{
font-family: 宋体;
font-size: 9pt;
padding: 1px;
padding-left: 2px;
padding-right: 2px;
background-color:#cccccc;
border:1px;
border-color:gray;
border-style: solid;
cursor: default;
}
.DemoSelected
{
font-family: 宋体;
font-size: 9pt;
padding: 1px;
padding-left: 2px;
padding-right: 2px;
background-color:#FFFFFF;
border:1px;
border-color:gray;
border-style: solid;
cursor: default;
}
input
{
font-family: 宋体;
font-size: 9pt;
}
select
{
font-family:宋体;
font-size:9pt;
}
4.调用此打印页面
<script language="javascript" type="text/javascript">
function PrintTXM()
{
var name = document.getElementById("TextBox_xm").value;
var txturl = document.getElementById("TextBox_txm").value;
//flxBarcode.axd?Text=11AA0A3B1487A19AB1&Width=60&Height=60
var imageurl = 'flxBarcode.axd?Text='+txturl+'&Width=60&Height=60';
var url = 'gwy_webprint.aspx?Name='+name+'&Url='+imageurl;
window.open(url, "_blank");
}
</script>
<INPUT id="BtnPrint" type="button" value="打印" onclick="PrintTXM()">
5.效果展示