Home  >  Article  >  Backend Development  >  How to use asp.net print control

How to use asp.net print control

高洛峰
高洛峰Original
2017-01-11 10:26:151845browse

Although the printing effect and controllability are not very good, it can be used reluctantly and can handle general printing.
The code is as follows:
Code

//调用PrintControl.ExecWB(?,?)实现直接打印和打印预览功能。(直接用系统提供的print()方法打印无法隐藏某些区域) 
//preview:是否显示预览。null/false:不显示,true:显示 
function printPage(preview) 
{ 
try 
{ 
var content=window.document.body.innerHTML; 
var oricontent=content; 
while(content.indexOf("{$printhide}")>=0) content=content.replace("{$printhide}","style='display:none'"); 
if(content.indexOf("ID=\"PrintControl\"")<0) content=content+"<OBJECT ID=\"PrintControl\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>"; 
window.document.body.innerHTML=content; 
//PrintControl.ExecWB(7,1)打印预览,(1,1)打开,(4,1)另存为,(17,1)全选,(10,1)属性,(6,1)打印,(6,6)直接打印,(8,1)页面设置 
if(preview==null||preview==false) PrintControl.ExecWB(6,1); 
else PrintControl.ExecWB(7,1); //OLECMDID_PRINT=7; OLECMDEXECOPT_DONTPROMPTUSER=6/OLECMDEXECOPT_PROMPTUSER=1 
window.document.body.innerHTML=oricontent; 
} 
catch(ex){ alert("执行Javascript脚本出错。"); } 
} 
function printConten(preview, html) 
{ 
try 
{ 
var content=html; 
var oricontent=window.document.body.innerHTML; 
while(content.indexOf("{$printhide}")>=0) content=content.replace("{$printhide}","style=&#39;display:none&#39;"); 
if(content.indexOf("ID=\"PrintControl\"")<0) content=content+"<OBJECT ID=\"PrintControl\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>"; 
window.document.body.innerHTML=content; 
//PrintControl.ExecWB(7,1)打印预览,(1,1)打开,(4,1)另存为,(17,1)全选,(10,1)属性,(6,1)打印,(6,6)直接打印,(8,1)页面设置 
if(preview==null||preview==false) PrintControl.ExecWB(6,1); 
else PrintControl.ExecWB(7,1); //OLECMDID_PRINT=7; OLECMDEXECOPT_DONTPROMPTUSER=6/OLECMDEXECOPT_PROMPTUSER=1 
window.document.body.innerHTML=oricontent; 
} 
catch(ex){ alert("执行Javascript脚本出错。"); } 
}

The above two functions are placed in a Js file. In the page content, the specified part can be printed by applying the script file and calling the further encapsulated function. Content:

<script language="javascript"> 
function Print(preview) { 
var text = document.getElementById("content").innerHTML; 
printConten(preview, text); 
}

The printing effect is roughly as shown in the figure below. If the printed page is in a frame page, you need to select the option "Print selected frame only".

asp.net 打印控件使用方法

Using this method, there is no need to install any controls, and it has good compatibility advantages. However, the report content seems to be more troublesome to control, especially for some The printing of reports requires the output of complex content, which also has certain shortcomings, but overall, it is also a better choice.

Later, when I needed to do some work on document printing, this control could not do it, so I needed a method or control that could better handle the printing.

Inadvertently, I found a better printing control that supports printing in various formats. It also has the ID printing function that I care about. It is powerful and easy to use. It is highly recommended.

Applying this control, the printing effect of ordinary reports is as follows:

asp.net 打印控件使用方法

The printing of the above two reports is actually similar, both are printing part of the HTML content, but the latter looks better and provides very complete report function settings.

The code is roughly as follows.
Code

<script language="javascript"> 
function Print(preview) { 
var text = document.getElementById("content").innerHTML; 
printConten(preview, text); 
} 
 
 
  
 

Many times, the content we don’t have is controlled by CSS, so sometimes we print part of the HTML. Without these styles, the Table format and fonts will appear. , may all change, and it won’t look good. Then you need to set the HTML style.

asp.net 打印控件使用方法

#If you set a style for the printed content, the interface effect will be much better.

asp.net 打印控件使用方法

The code to set the style is as follows.
Code

<script language="javascript" type="text/javascript"> 
function Preview() {//打印预览 
CreateLicenseData(); 
LODOP.SET_SHOW_MODE("PREVIEW_IN_BROWSE", 1); 
LODOP.PREVIEW(); 
}; 
function CreateLicenseData() { 
LODOP.PRINT_INIT("申请处理单"); 
var strBodyStyle = "<link type=&#39;text/css&#39; rel=&#39;stylesheet&#39; href=&#39;http://www.cnblogs.com/Themes/Default/style.css&#39; /><style><!--table { border:1;background-color: #CBCBCC } td {background-color:#FFFFFE;border: 1; } th { background-color:#F1F1F3;padding-left:5px;border:1}--></style>"; 
var strFormHtml = strBodyStyle + "<body>" + document.getElementById("content").innerHTML + "</body>"; 
LODOP.ADD_PRINT_HTM(20, 40, 610, 900, strFormHtml); 
LODOP.PREVIEW(); 
} 
</script>

For more articles related to how to use asp.net print control, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn