c#打印怎么做?

c#打印怎么做?,第1张

步骤如下:

创建一个PrintDialog的实例。如下:

System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ()

创建一个PrintDocument的实例.如下:

System.Drawing.Printing.PrintDocument docToPrint =

new System.Drawing.Printing.PrintDocument()

设置打印机开始打印的事件处理函数.函数原形如下:

void docToPrint_PrintPage(object sender,

System.Drawing.Printing.PrintPageEventArgs e)

将事件处理函数添加到PrintDocument的PrintPage事件中。

docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage)

设置PrintDocument的相关属性,如:

PrintDialog1.AllowSomePages = truePrintDialog1.ShowHelp = true

把PrintDialog的Document属性设为上面配置好的PrintDocument的实例:

PrintDialog1.Document = docToPrint

调用PrintDialog的ShowDialog函数显示打印对话框:

DialogResult result = PrintDialog1.ShowDialog()

根据用户的选择,开始打印:

if (result==DialogResult.OK)

{

docToPrint.Print()

}

using System

using System.Drawing.Printing

using System.Windows.Forms

using System.IO

namespace EDImageSystem

{

/// <summary>

/// PrintService 的摘要说明。

/// </summary>

public class PrintService

{

public PrintService()

{

//

// TODO: 在此处添加构造函数逻辑

//

this.docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage)

}//将事件处理函数添加到PrintDocument的PrintPage中

// Declare the PrintDocument object.

private System.Drawing.Printing.PrintDocument docToPrint =

new System.Drawing.Printing.PrintDocument()//创建一个PrintDocument的实例

private System.IO.Stream streamToPrint

string streamType

// This method will set properties on the PrintDialog object and

// then display the dialog.

public void StartPrint(Stream streamToPrint,string streamType)

{

this.streamToPrint=streamToPrint

this.streamType=streamType

// Allow the user to choose the page range he or she would

// like to print.

System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ()//创建一个PrintDialog的实例。

PrintDialog1.AllowSomePages = true

// Show the help button.

PrintDialog1.ShowHelp = true

// Set the Document property to the PrintDocument for

// which the PrintPage Event has been handled. To display the

// dialog, either this property or the PrinterSettings property

// must be set

PrintDialog1.Document = docToPrint//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7840948.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存