1 /// <summary>这个是在WINFORM中实现的代码
/// <summary>
/// Winform下显示打印纸张设置、打印机设置、打印预览对话框
/// </summary>
public class WinPriterPageSetting:IPriterPageSetting
{
//把PrintPage委托声明为类的一个成员变量
private PrintPageDelegate _printPage;
//打印文档
private PrintDocument _printDocument;
#region 构造函数
public WinPriterPageSetting()
: this(null)
{
}
//使用printDocument来初始化类的新实例,当printDocument为null的时候自动创建一个printDocument的实例
public WinPriterPageSetting(PrintDocument printDocument)
{
if (printDocument != null)
{
_printDocument = printDocument;
}
else
{
_printDocument = new PrintDocument();
}
}
#endregion
#region IPriterPageSetting 成员
/// <summary>
/// 获取或设置打印文档
/// </summary>
public PrintDocument PrintDocument
{
get
{
return this._printDocument;
}
set
{
this._printDocument = value;
}可是我现在想在WEB中实现啊,这个要怎么写啊;下面是我自己写的好像写错了,可是又不知道这个错出现在那儿!!! 各位帮我看看嘛
/// <summary>
/// 在Web中显示打印纸张设置、打印机设置、打印预览对话框
/// </summary>
public class WebPriterPageSetting:IPriterPageSetting
{
//把PrintPage委托声明为类的一个成员变量
private PrintPageDelegate _printPage;
//打印文档
private PrintDocument _printDocument;
#region 构造函数
public WebPriterPageSetting()
: this(null)
{
}
//使用printDocument来初始化类的新实例,当printDocument为null的时候自动创建一个printDocument的实例
public WebPriterPageSetting(PrintDocument printDocument)
{
if (printDocument != null)
{
_printDocument = printDocument;
}
else
{
_printDocument = new PrintDocument();
}
}
#endregion
#region IPriterPageSetting 成员
/// <summary>
/// 获取或设置打印文档
/// </summary>
public PrintDocument PrintDocument
{
get
{
return this._printDocument;
}
set
{
this._printDocument = value;
}
}
/// <summary>
/// 一定要实例化此类后在调用打印/预览之前设置此属性,使之关联一个方法,目的是让具体的打印由实例化者来操作
/// </summary>
public PrintPageDelegate PrintPage
{
set
{
//初始委托变量关联方法
_printPage = value;
if (_printDocument == null)
{
throw new Exception("关联的打印文档不能为空!");
}
_printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this._printPage);
}
get
{
return _printPage;
}
}
/// <summary>
/// 显示页面设置对话框
/// </summary>
/// <returns>返回PageSettings</returns>
public PageSettings ShowPageSetupDialog()
{
return ShowPageSetupDialog();
}
/// <summary>
/// 显示打印机设置对话框
/// </summary>
/// <returns>PrinterSettings</returns>
public PrinterSettings ShowPrintSetupDialog()
{
return ShowPrintSetupDialog();
}
/// <summary>
/// 显示打印预览对话框
/// </summary>
public void ShowPrintPreviewDialog()
{
ShowPrintPreviewDialog();
}
#endregion
}
}
/// <summary>
/// 一定要实例化此类后在调用打印/预览之前设置此属性,使之关联一个方法,目的是让具体的打印由实例化者来操作
/// </summary>
public PrintPageDelegate PrintPage
{
set
{
//初始委托变量关联方法
_printPage = value;
if (_printDocument == null)
{
throw new Exception("关联的打印文档不能为空!");
}
_printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this._printPage);
}
get
{
return _printPage;
}
}
/// <summary>
/// 显示页面设置对话框
/// </summary>
/// <returns>返回PageSettings</returns>
public PageSettings ShowPageSetupDialog()
{
return ShowPageSetupDialog(this._printDocument);
}
/// <summary>
/// 显示打印机设置对话框
/// </summary>
/// <returns>PrinterSettings</returns>
public PrinterSettings ShowPrintSetupDialog()
{
return ShowPrintSetupDialog(this._printDocument);
}
/// <summary>
/// 显示打印预览对话框
/// </summary>
public void ShowPrintPreviewDialog()
{
ShowPrintPreviewDialog(this._printDocument);
}
#endregion
//*************以下的几个对话框方法可以独立使用****************
#region 页面设置对话框
protected virtual PageSettings ShowPageSetupDialog(PrintDocument printDocument)
{
PageSettings ps = new PageSettings();
if (PrintDocument == null)
{
throw new Exception("关联的打印文档不能为空");
}
try
{
//声明并实例化PageSetupDialog
PageSetupDialog psDlg = new PageSetupDialog();
//相关文档及文档默认页面设置
psDlg.Document = printDocument;
psDlg.PageSettings = printDocument.DefaultPageSettings;
//显示对话框
DialogResult result = psDlg.ShowDialog();
if (result == DialogResult.OK)
{
ps = psDlg.PageSettings;
printDocument.DefaultPageSettings = psDlg.PageSettings;
}
}
catch (System.Drawing.Printing.InvalidPrinterException e)
{
MessageBox.Show("未安装打印机,请进入系统控制面板安装打印机", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return ps;
}
#endregion
#region 打印设置对话框
protected virtual PrinterSettings ShowPrintSetupDialog(PrintDocument printDocument)
{
PrinterSettings ps = new PrinterSettings();
if (printDocument == null)
{
throw new Exception("关联的打印文档不能为空!");
}
try
{
//声明并实例化PrintDialog
PrintDialog pDlg = new PrintDialog();
//可以选定页
pDlg.AllowSomePages = true;
//指定打印文档
pDlg.Document = printDocument;
//显示对话框
DialogResult result = new DialogResult();
if (result == DialogResult.OK)
{
//保存打印设置
ps = pDlg.PrinterSettings;
//打印
printDocument.Print();
}
}
catch (System.Drawing.Printing.InvalidPrinterException e)
{
MessageBox.Show("未安装打印机,请进入系统控制面板安装打印机", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return ps;
}
#endregion
#region 打印浏览对话框
protected virtual void ShowPrintPreviewDialog(PrintDocument printDocument)
{
if (printDocument == null)
{
throw new Exception("关联的打印文档不能为空");
}
try
{
PrintPreviewDialog ppDlg = new PrintPreviewDialog();
//指定打印文档
ppDlg.Document = printDocument;
//显示对话框
DialogResult result = new DialogResult();
if (result == DialogResult.OK)
{
//.....
}
}
catch (System.Drawing.Printing.InvalidPrinterException e)
{
MessageBox.Show("未安装打印机,请进入系统控制面板安装打印机", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
}
2 /// PrintDocument.PrintPage的委托定义
3 /// </summary>
4 /// <param name="obj"></param>
5 /// <param name="ev"></param>
6 public delegate void PrintPageDelegate(object obj,System.Drawing.Printing.PrintPageEventArgs ev);
7
8 /// <summary>
9 /// IPriterPageSetting的接口,显示打印纸张设置、打印机设置、打印预览对话框
10 /// </summary>
11 interface IPriterPageSetting
12 {
13 /// <summary>
14 /// 获取或设置打印文档
15 /// </summary>
16 System.Drawing.Printing.PrintDocument PrintDocument
17 {
18 get;
19 set;
20 }
21
22 /// <summary>
23 /// 关联一个方法,目地是让具体的打印由实例化者来操作
24 /// </summary>
25 PrintPageDelegate PrintPage
26 {
27 get;
28 set;
29 }
30
31 /// <summary>
32 /// 显示页面设置对话框
33 /// </summary>
34 /// <returns>PageSettings</returns>
35 System.Drawing.Printing.PageSettings ShowPageSetupDialog();
36
37 /// <summary>
38 /// 显示打印机设置对话框
39 /// </summary>
40 /// <returns>PrinterSettings</returns>
41 System.Drawing.Printing.PrinterSettings ShowPrintSetupDialog();
42
43 /// <summary>
44 /// 显示打印预览对话框
45 /// </summary>
46 void ShowPrintPreviewDialog();
47 }
现在就是指的是在WEB中实现的那个页面中的代码我不知道要怎么用了,期盼各位大佬了!!
web里面实现打印不是在后台写代码的,你应该写脚本或者借助一些插件来实现它,最常用的是IE的WebBrowser和javascript,例如这样(不过这只能支持一般的IE打印,如果需要更加强大的功能的话最好是找一个合适的插件)
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" id=wb name=wb ></OBJECT>
<input type='button' value='打印' onclick='javascript:wb.ExecWB(6,1)'/>
<input type='button' value='打印预览' onclick='javascript:wb.ExecWB(7,1)'/>
<input type='button' value='页面设置' onclick='javascript:wb.ExecWB(8,1)'/>
楼上说的好,就是那样,顺便加一句,注意浏览器的安全性