.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文件,代码如下
前台文件:
--></mce:style><mce:style media="print" mce_bogus="1"><!--
.noprint { DISPLAY: none }
--></mce:style><mce:style media="print" mce_bogus="1" mce_bogus="1"><!--
.noprint { DISPLAY: none }
--></mce:style><mce:style media="print" mce_bogus="1" mce_bogus="1" mce_bogus="1"><!--
.noprint { DISPLAY: none }
--></mce:style><mce:style media="print" mce_bogus="1" mce_bogus="1" mce_bogus="1" mce_bogus="1"><!--
.noprint { DISPLAY: none }
--></mce:style><mce:style media="print" mce_bogus="1" mce_bogus="1" mce_bogus="1" mce_bogus="1" mce_bogus="1"><!--
.noprint { DISPLAY: none }
--></mce:style><style media="print" mce_bogus="1" mce_bogus="1" mce_bogus="1" mce_bogus="1" mce_bogus="1" mce_bogus="1">.noprint { DISPLAY: none }
</style>
<mce:script language="javascript" src="scripts/print.js" mce_src="scripts/print.js" charset="gb2312"></mce: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" mce_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>
后台文件:
引用JS代码:
//创建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;
}
CSS代码:
调用此方法:
function PrintTXM()
{
var name = document.getElementById("TextBox_xm").value;
var txturl = document.getElementById("TextBox_txm").value;
//flxBarcode.axd?Text=11AA0A3B1487A19AB1Width=60Height=60
var imageurl = 'flxBarcode.axd?Text='+txturl+'Width=60Height=60';
var url = 'gwy_webprint.aspx?Name='+name+'Url='+imageurl;
window.open(url, "_blank");
}
// --></mce:script>
<INPUT id="BtnPrint" type="button" value="打印" onclick="PrintTXM()">