document.execCommand('print','true','true')是打印当前页,出现打印对话框的模式,如果不需要出现打印对话框,如何实现!
<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0 VIEWASTEXT></OBJECT>
<input type=button value=打开 οnclick=document.all.WebBrowser.ExecWB(1,1)>
<input type=button value=另存为 οnclick=document.all.WebBrowser.ExecWB(4,1)>
<input type=button value=全选 οnclick=document.all.WebBrowser.ExecWB(17,1)>
<input type=button value=属性 οnclick=document.all.WebBrowser.ExecWB(10,1)>
<input type=button value=关闭窗口 οnclick=document.all.WebBrowser.ExecWB(45,1)>
<input type=button value=打印 οnclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button value=直接打印 οnclick=document.all.WebBrowser.ExecWB(6,6)>
<input type=button value=页面设置 οnclick=document.all.WebBrowser.ExecWB(8,1)>
<input type=button value=打印预览 οnclick=document.all.WebBrowser.ExecWB(7,1)>
由于项目需要,从网上找有关于Web打印设置的文章。从google里搜一下,看得比较眼花,遂整理一下,以便以后使用。
目前,Web打印设置的解决方案大致有四种:
1、需要打印的时候,直接在页面设置里先设置好,然后打印。虽然有点傻,但是很实用不用涉及到编写代码的问题。
2、使用系统原有的IEWebBrowser控件,使用方法如下:
2. 调用方法。
WebBrowser.ExecWB nCmdID, nCmdExecOpt, [pvaIn], [pvaOut]
3. 参数说明。
(a).nCmdID
OLECMDID_OPEN = 1,
OLECMDID_NEW = 2,
OLECMDID_SAVE = 3,
OLECMDID_SAVEAS = 4,
OLECMDID_SAVECOPYAS = 5,
OLECMDID_PRINT = 6,
OLECMDID_PRINTPREVIEW = 7,
OLECMDID_PAGESETUP = 8,
OLECMDID_SPELL = 9,
OLECMDID_PROPERTIES = 10,
OLECMDID_CUT = 11,
OLECMDID_COPY = 12,
OLECMDID_PASTE = 13,
OLECMDID_PASTESPECIAL = 14,
OLECMDID_UNDO = 15,
OLECMDID_REDO = 16,
OLECMDID_SELECTALL = 17,
OLECMDID_CLEARSELECTION = 18,
OLECMDID_ZOOM = 19,
OLECMDID_GETZOOMRANGE = 20
OLECMDID_UPDATECOMMANDS = 21
OLECMDID_REFRESH = 22
OLECMDID_STOP = 23
OLECMDID_HIDETOOLBARS = 24
OLECMDID_SETPROGRESSMAX = 25
OLECMDID_SETPROGRESSPOS = 26
OLECMDID_SETPROGRESSTEXT = 27
OLECMDID_SETTITLE = 28
OLECMDID_SETDOWNLOADSTATE = 29
OLECMDID_STOPDOWNLOAD = 30
上面的关键词都可以在浏览器的菜单里面找到对应的选项﹐大家一看就明白的﹗
(b).nCmdExecOpt
OLECMDEXECOPT_DODEFAULT = 0,
OLECMDEXECOPT_PROMPTUSER = 1,
LECMDEXECOPT_DONTPROMPTUSER = 2,
OLECMDEXECOPT_SHOWHELP = 3
对于这个参数﹐一般来说﹐选1就可以了。
这是调用IE的”另存为”功能的示例﹕
< object id ="WebBrowser" width =0 height =0 classid ="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" ></ object >
< A href ="javascript:WebBrowser.ExecWB(4,1);" > Save-存储 </ A >
顺便把IE功能详解贴一下,转贴自:http://blog.csdn.net/hansbj/archive/2004/09/24/115549.aspx。
< INPUT onclick =document.all.WebBrowser.ExecWB(4,1) type =button value =另存为 name =Button2 >
< INPUT onclick =document.all.WebBrowser.ExecWB(10,1) type =button value =属性 name =Button3 >
< INPUT onclick =document.all.WebBrowser.ExecWB(6,1) type =button value =打印 name =Button >
< INPUT onclick =document.all.WebBrowser.ExecWB(8,1) type =button value =页面设置 name =Button4 >
< INPUT onclick =window.location.reload() type =button value =刷新 name =refresh >
< INPUT onclick ="window.external.ImportExportFavorites(true,'');" type =button value =导入收藏夹 name =Button5 >
< INPUT onclick ="window.external.ImportExportFavorites(false,'');" type =button value =导出收藏夹 name =Button32 >
< INPUT onclick ="window.external.AddFavorite(location.href, document.title)" type =button value =加入收藏夹 name =Button22 >
< INPUT onclick ="window.external.ShowBrowserUI('OrganizeFavorites', null)" type =button value =整理收藏夹 name =Submit2 >
< INPUT onclick ='window.location="view-source:" + window.location.href' type =button value =查看源文件 name =Button7 >
< INPUT onclick ="window.external.ShowBrowserUI('LanguageDialog', null)" type =button value =语言设置 name =Button6 >
< INPUT onclick ="document.execCommand('Cut')" type =button value =剪切 >
< INPUT onclick ="document.execCommand('Copy')" type =button value =拷贝 >
< INPUT onclick ="document.execCommand('Paste')" type =button value =粘贴 >
< INPUT onclick ="document.execCommand('Undo')" type =button value =撤消 >
< INPUT onclick ="document.execCommand('Delete')" type =button value =删除 >
< INPUT onclick ="document.execCommand('Bold')" type =button value =黑体 >
< INPUT onclick ="document.execCommand('Italic')" type =button value =斜体 >
< INPUT onclick ="document.execCommand('Underline')" type =button value =下划线 >
< INPUT onclick ="document.execCommand('stop')" type =button value =停止 >
< INPUT onclick ="document.execCommand('SaveAs')" type =button value =保存 >
< INPUT onclick ="document.execCommand('Saveas',false,'c://test.htm')" type =button value =另存为 >
< INPUT onclick ="document.execCommand('FontName',false,fn)" type =button value =字体 >
< INPUT onclick ="document.execCommand('FontSize',false,fs)" type =button value =字体大小 >
< INPUT onclick ="document.execCommand('refresh',false,0)" type =button value =刷新 >
< INPUT onclick =window.location.reload() type =button value =刷新 >
< INPUT onclick =history.go(1) type =button value =前进 >
< INPUT onclick =history.go(-1) type =button value =后退 >
< INPUT onclick =history.forward() type =button value =前进 >
< INPUT onclick =history.back() type =button value =后退 >
< INPUT onclick ='window.external.ImportExportFavorites(true,"http://localhost");' type =button value =导入收藏夹 >
< INPUT onclick ='window.external.ImportExportFavorites(false,"http://localhost");' type =button value =导出收藏夹 >
< INPUT onclick ="window.external.ShowBrowserUI('OrganizeFavorites', null)" type =button value =整理收藏夹 >
< INPUT onclick ="window.location = 'view-source:'+ window.location.href" type =button value =查看源文件 >
< INPUT onclick ="window.external.ShowBrowserUI('LanguageDialog', null)" type =button value =语言设置 >
< INPUT onclick ="window.external.AddFavorite('http://www.cnblogs.com/hbzxf/', 阿好空间)" type =button value =加入收藏夹 >
< INPUT onclick ="window.external.addChannel('http://www.cnblogs.com/hbzxf/')" type =button value =加入到频道 >
< INPUT onclick ="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.cnblogs.com/hbzxf/')" type =button value =设成主页 >
3、通过写注册表,来设置Header、Footer和Margin等等。也许在过去是个好方法,但是从目前来看,这个方法似乎已经尘封了许久,不过还是拿过来亮一下。
dim hkey_root,hkey_path,hkey_key
hkey_root = " hkey_current_user "
hkey_path = " /software/microsoft/internet explorer/pagesetup "
' 设置网页打印的页眉页脚为空
function pagesetup_null()
on error resume next
set regwsh = createobject ( " wscript.shell " )
hkey_key = " /header "
regwsh.regwrite hkey_root + hkey_path + hkey_key, ""
hkey_key = " /footer "
regwsh.regwrite hkey_root + hkey_path + hkey_key, ""
end function
' 设置网页打印的页眉页脚为默认值
function pagesetup_default()
on error resume next
set regwsh = createobject ( " wscript.shell " )
hkey_key = " /header "
regwsh.regwrite hkey_root + hkey_path + hkey_key, " &w&bPage &p of &P "
hkey_key = " /footer "
regwsh.regwrite hkey_root + hkey_path + hkey_key, " &u&b&d "
end function
</ script >
4、通过第三方控件来实现。使用ScritptX.cab或者smsx.cab,具体的介绍http://www.meadroid.com/scriptx/。
< object id = " factory " style = " display:none " viewastext classid = " clsid:1663ed61-23eb-11d2-b92f-008048fdd814 " codebase = " http://www.meadroid.com/scriptx/ScriptX.cab#Version=5,60,0,360 " ></ object >
// 控件使用教本:
< script defer >
function SetPrintSettings() {
// -- advanced features ,未曾使用过,有待确认。
factory.printing.SetMarginMeasure(2) // measure margins in inches
factory.SetPageRange(false, 1, 3) // need pages from 1 to 3
factory.printing.printer = "HP DeskJet 870C"
factory.printing.copies = 2
factory.printing.collate = true
factory.printing.paperSize = "A4"
factory.printing.paperSource = "Manual feed"
// -- basic features 使用过
factory.printing.header = "This is MeadCo"
factory.printing.footer = "Advanced Printing by ScriptX"
factory.printing.portrait = false //方向,true
factory.printing.leftMargin = 1.0
factory.printing.topMargin = 1.0
factory.printing.rightMargin = 1.0
factory.printing.bottomMargin = 1.0
factory.printing.Print(false) //直接打印,true ,好像不起作用,总是弹出选择打印机窗口
factory.printing.PageSetup() //打印设置
factory.printing.Preview() //打印预览
}
</ script >