博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net套打
阅读量:5311 次
发布时间:2019-06-14

本文共 7189 字,大约阅读时间需要 23 分钟。

1、利用IE直接打印,这种方式最方便,但是控制能力较差。

2、利用ACTIVE控件,可以自己做一个IE的控件来控制打印。

3、水晶报表等第三方工具,购买价格昂贵。

    根据我的需求情况,用IE直接打印的方式可以满足。我们的目的就是在一张申请表格上对应位置打印上相应的项目,表格也只有一页,不涉及分页的问题。

   先来看一下表格,目的就是在一个WEBFORM里面对应的TEXTBOX内容放到对应的位置打印出来。

    先来说一下我的思路:程序自动生成一个要打印的网页,将对应的字段值传递给这个网页,每个字段的位置事先量好,利用<div style="   ">来控制对应字段在页面中的位置。具体要到了下面技术:

1、自定义一个打印类,用来自动生成要打印的网页。

using System; using System.Data; using System.Configuration; 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.Text; using System.IO; using System.Collections;

/// /// MyPrint 的摘要说明 /// public class MyPrint : System.Web.UI.Page {     public static string pageUrlBase;//Page基本的URL     public string name;     public string sex;     public string brithday;     public string country;     public string CardName;     public string CardNum;     public string address;     public string touchAddress;     public string tel;     public string postCode;     public string type;     public string reg;     public string oldReg;     ///     /// 恢复打印模板     ///     ///    ///

  public void ClearView()     {         StringBuilder htmltext = new StringBuilder();         try         {             if (!File.Exists(Server.MapPath(Context.Request.ApplicationPath) + "//printwindow.htm"))             {                 FileStream fs = File.Create(Server.MapPath(Context.Request.ApplicationPath) + "//printwindow.htm");                 fs.Close();             }             using (StreamWriter sw = new StreamWriter(Server.MapPath(Context.Request.ApplicationPath) + "))             {                 string sTableTitle = "";                 sTableTitle = sTableTitle + "<html>/n";                 sTableTitle = sTableTitle + "<head>/n";                 sTableTitle = sTableTitle + "<Title></Title>/n";                 sTableTitle = sTableTitle + "<meta http-equiv=/"Content-Type/" content=/"text/html; charset=GB2312/">/n";                 sTableTitle = sTableTitle + "<script lanage=/"javascript/">";                 sTableTitle = sTableTitle + "function preview()";                 sTableTitle = sTableTitle + "{";                 sTableTitle = sTableTitle + "window.print();window.close()}</script>";                 sTableTitle = sTableTitle + "</head>";

                sTableTitle = sTableTitle + "<body style=/"background-position: left top; background-image: url(Images/申请表.jpg); left: 0px; width: 735px; position: absolute; top: 0px; height: 1050px;/" ms_positioning=/"GridLayout/"    topmargin=/"0/" bottommargin=/"0/"  οnlοad=/"preview();/">/n";                 //sTableTitle = sTableTitle + "Model/n";                 //正式页面内容部分                 sTableTitle = sTableTitle + "<font face=/"宋体/">";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 100; left: 138px; position: absolute; top: 176px/">" + name + "</div>";                 //sTableTitle = sTableTitle + "<img src=/"Images/1234.jpg/" Style=/"z-index: 116; left: 119px; position: absolute; top: 439px/"  Width=/"8px/" Height=/"8px/" />";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 101; left: 395px; position: absolute; top: 176px/">" + sex + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 102; left: 488px; position: absolute; top: 176px/">" + brithday + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 103; left: 640px; position: absolute; top: 176px/">" + country + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 104; left: 138px; position: absolute; top: 200px/">" + CardName + "</div>";                 //sTableTitle = sTableTitle + "<div  Style=/"z-index: 105; left: 272px; position: absolute; top: 200px/">" + CardNum + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 106; left: 136px; position: absolute; top: 256px/">" + address + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 107; left: 136px; position: absolute; top: 288px/">" + touchAddress + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 108; left: 136px; position: absolute; top: 320px/">" + tel + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 109; left: 472px; position: absolute; top: 320px/">" + postCode + "</div>";                 sTableTitle = sTableTitle + "<div  Style=/"z-index: 110; left: 352px; position: absolute; top: 352px/">" + reg + "</div>";                 //sTableTitle = sTableTitle + "<div  Style=/"z-index: 112; left: 119px; position: absolute; top: 358px/">姓名</div>";                 //判断是否是初次申领还是什么地                 if(type=="初次申领")                     sTableTitle = sTableTitle + "<img src=/"Images/1234.jpg/" Style=/"z-index: 112; left: 128px; position: absolute; top: 358px/"  Width=/"8px/" Height=/"8px/" />";                 else if (type == "增加准驾车型")                 {                     sTableTitle = sTableTitle + "<img src=/"Images/1234.jpg/" Style=/"z-index: 115; left: 118px; position: absolute; top: 384px/"  Width=/"8px/" Height=/"8px/" />";                     sTableTitle = sTableTitle + "<div  Style=/"z-index: 111; left: 600px; position: absolute; top: 352px/">" + oldReg + "</div>";                 }                 else if (type == "持军警驾驶证")                 {                     sTableTitle = sTableTitle + "<img src=/"Images/1234.jpg/" Style=/"z-index: 114; left: 118px; position: absolute; top: 409px/"  Width=/"8px/" Height=/"8px/" />";                     sTableTitle = sTableTitle + "<div  Style=/"z-index: 111; left: 600px; position: absolute; top: 352px/">" + oldReg + "</div>";

                }                 else                 {                     sTableTitle = sTableTitle + "<img src=/"Images/1234.jpg/" Style=/"z-index: 116; left: 119px; position: absolute; top: 439px/"  Width=/"8px/" Height=/"8px/" />";                     sTableTitle = sTableTitle + "<div  Style=/"z-index: 111; left: 600px; position: absolute; top: 352px/">" + oldReg + "</div>";

                }                 //输出身份证                 int length = CardNum.Length;                 int start = 272;                 for (int i = 0; i < length; i++)                 {                     sTableTitle = sTableTitle + "<div  Style=/"z-index: 105; left: " + start.ToString() +  "px; position: absolute; top: 200px/">" + CardNum.Substring(i,1) + "</div>";                     start = start + 18;                 }                 sTableTitle = sTableTitle + "</body>/n";                 sTableTitle = sTableTitle + "</html>/n";                 sw.WriteLine(sTableTitle);                 sw.Flush();                 sw.Close();             }         }         catch         {             Response.Write("<script>alert(&apos;读取文件出错!&apos;)</script>");         }     }public MyPrint() { // // TODO: 在此处添加构造函数逻辑 //         try         {             string urlSuffix = Context.Request.Url.Host;             urlSuffix = urlSuffix + (Context.Request.Url.Port.ToString() == "" ? "" : ":" + Context.Request.Url.Port);             urlSuffix = urlSuffix + (Context.Request.ApplicationPath.ToString() == "/" ? "" : Context.Request.ApplicationPath);             pageUrlBase = @"http://" + urlSuffix;         }         catch         {

        } } }

     ClearView函数是用来自动生成网页。里面输出身份证的时候,为了和表格对应精确采用了按位输出。
2.调用的方法
   print.name = this.TextBox2.Text;         if (RadioButton1.Checked)             print.sex = "男";         else             print.sex = "女";         print.brithday = this.TextBox4.Text;         print.tel = this.TextBox5.Text;         print.country = this.DropDownList2.Text;         print.type = this.DropDownList3.Text;         print.oldReg = this.DropDownList4.Text;         print.reg = this.DropDownList6.Text;         print.address = this.TextBox13.Text;         print.touchAddress = this.TextBox14.Text;         print.CardName = "身份证";         print.CardNum = this.TextBox3.Text;
        print.ClearView();         string showOpen = "<script lanage=&apos;javascript&apos;>window.open(&apos;printwindow.htm&apos;,&apos;_blank&apos;);</script>";         ScriptManager.RegisterClientScriptBlock(this.Button4, this.GetType(), "verygood", showOpen, false);       这是提交页面上的一个“打印”按钮的响应事件。首先把页面上对应的字段传给打印类,然后调用ClearView来生成网页。最后,通过注册脚本的方式在新窗口中打开网页。我这里了AJAX所以用到了
    ScriptManager.RegisterClientScriptBlock(this.Button4, this.GetType(), "verygood", showOpen, false);这句,如果没用AJAX用这个就可以了
    RegisterClientScriptBlock("clientScript", showOpen);
3、用到的JAVASCRIPT
    <script language="Javascript">     function preview()     {         window.print();        self.close();     }     </script>
    打印之后能够自动关闭窗口。

转载于:https://www.cnblogs.com/chenbg2001/archive/2013/02/27/2935708.html

你可能感兴趣的文章
lua语言入门之Sublime Text设置lua的Build System
查看>>
vue.js基础
查看>>
电脑的自带图标的显示
查看>>
[转载] redis 的两种持久化方式及原理
查看>>
C++ 删除字符串的两种实现方式
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Java抽象类和接口的比较
查看>>
开发进度一
查看>>
MyBaits学习
查看>>
管道,数据共享,进程池
查看>>
CSS
查看>>
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
程序集的混淆及签名
查看>>
判断9X9数组是否是数独的java代码
查看>>
00-自测1. 打印沙漏
查看>>
UNITY在VS中调试
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>