C #打印详解.pdf

上传人:无*** 文档编号:90897865 上传时间:2023-05-18 格式:PDF 页数:42 大小:2.38MB
返回 下载 相关 举报
C #打印详解.pdf_第1页
第1页 / 共42页
C #打印详解.pdf_第2页
第2页 / 共42页
点击查看更多>>
资源描述

《C #打印详解.pdf》由会员分享,可在线阅读,更多相关《C #打印详解.pdf(42页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、C#打印详解2010-05-07 14:59:021分类:程序开发|标签:|字号大中小订阅System.Drawing.Printing;命 名 空 中 的 廊I以落助在鹰用程序中打印文假打 印 的 基 是PrintDocument,方 法Print。畲敌勤一系列调用,最 后 用OnPrintPage。,把输出结果停送东合打印械。PrintDocument:是最重要的黑乎所有的打印 都典道彳固 有I I保。要打印一偃1文 槽,需要 PrintDocument 的一(固 例。Printcontroller:控裂打印任矜流。提供了打印 始、打印每彳固直面和打印余吉束的事件。派生於 Printcon

2、troller 的具骨嬖直有 StandardPrintController 和 PreviewPrintController.Printsettings:可以攫取打印和 置打印檄配置。圄 固 配 置 由PageSetupDialog迤行。PrintDialog:定使用哪彳固打印械迤行打印和如何配置PrinterSettings。Graphics:可以 丁印械的割菌内容,女合打印檄赞送字符串、缴蓊口曲PageSetupDialog:含殳置打印直面。PrintPreviewDialg:打印予真霓直面。其中:PrintDialog直面檬式如下圈I:08列印PrintSetupDialog直面格式如

3、下圈1:PrintPreviewDialog 直面檬式如下H I :鹰用程序必乡直 用PrintDocument的Print。方 法,改勤 用序列。因 悬PrintDocument本身她不负责打印流,所 以 由Printcontroller通强 用圜固类直的Print。方法完成打印。打印控制器现在轨行操作,通 知PrintDocument,通 谩 用OnBeginPrint。始打印。如果鹰用程序在打印任矜的 始幸入行某些操作,就必须在PrintDocument中注册一彳固事件虑理程序,造檄才曾在J1用程序类直中接到通知。如上圄中,假定注册了虑理程序OnBeginPrintO,所以圄固虑理程序J

4、B在PrintDocument类 直 中 策I用。在 始B皆 段 完 成 后,Printcontroller就 迤 入PrintLoopO,悬每彳固要打印的直面 用PrintDocument 中 的 OnPrintPage。方法。OnPrintPage(阖用所有的 PrintPage 事件虑理程序。必Z戢悔彳固直面翱亍圄固凝程序,否即就不曾打EU任何内容。可以在PrintDocumentPrintPage虑理程序中轨行打印代碣。每值I打 印 直 面 都 要 用 理程序。如果打印任矜只需要幸丸行一次打印代碣,就 必,醐 行BeginPrint和Endprint事件和虞理程序。在打印完成最后一直后

5、,Printcontroller就调用PrintDocument 中 的 OnEndPrint。也可以翱亍要f f i l 里 邮 的 鹿 理 跳如何打印(曾军打印方法)?要 打印必须有如下内容:L打印文件槽,用於打印械印械。此用PrintDocum ent例化2、被打印的内容。虑理用PintDocument中 PrintPage事件方法得到被打印内容。其方法如下:1、例 化 PrintDocument。磬明需要打印的文樟2、增加PrintDocumentPrintPage事 件,此事件的方法用於得到被打印的内容3、调 用 PrintDocumentPrintO方 法 打 印 内 容,在 打

6、印 之 前,此方法曾在PrntController 的 下 先 呼 叫 PrintPage事件的方法4、在 PrintPage事 件 中 打 印 文 本,用於得到打印内容,此方法结束后曾回到PrintDocumentPrintO方法中,幸 丸 行 打 印代碣如下:注:界面中(固多行的TextBox:textBoxEdit和一值1 打印菜罩:MiFilePrintusing System.IO;using System.Drawing.Printing;namespace SimpleEditorpublic partial class SimpleEditorForm:Form窗醴中一值1 打

7、印菜罩名耦悬MiFilePrint一彳固多行 TextBoxG:名耦 :textBoxEdit*/1、例 化 PrintDocumentPrintDocument pdDocument=new PrintDocumentO;public SimpleEditorForm()(InitializeComponent();2、UM PrintDocument.PrintPage 事件pdDocument.PrintPage+=new PnntPageEventHandler(OnPrintPage);)/常按下打印B 寺,此卷界面中的打印蝇事件/private void OnFilePrint(o

8、bject sender,Eve nt Args e)try*PrintDocument壁 寸 象 的 Print。方法在 Printcontroller的 助下律丸行 PrintPage事件。73、I用 PrintDocument.Print()方法pdDocument.Print();)catch(InvalidPrinterException ex)(MessageBox.Show(ex.Message,Simple Editor,MessageBoxButtons.OK,MessageBoxIcon.Error);throw;)/printDocument 的 PrintPage 事件

9、/private void OnPrintPage(object sender,PrintPageEventArgs e)(/*得至!J TextBox中每行的字符串辎*n 换行*r 回隼*/char param=n;string lines=textBoxEdit.Text.Split(param);int i=0;char trimParam=r;foreach(string s in lines)(删除每行中的rlinesi+=s.TrimEnd(trimParam);int x=20;int y=20;foreach(string line in lines)(/*4、把文本行赞送东台

10、打印械,其中e是PrintPageEventArgs 型 的 T 固燮量,其腐性速接到打印檄H耳箫文本中。*打印 目箭文本可以嘉到打印 上。*输出结果的位置用燮更X和Y定羲。7e.Graphics.DrawString(line,new FontCArial,10),Brushes.Black,x,y);y+=15;如果要鬻见多直打印,就要使用PrintPageEventArgs 的HasMorePages腐性。我俯封之前的代礁作 口下燮更:增 力 口 PrintDocument的BeginPrint和EndPrint事件。BeginPrint事件用於得到被打印的内容。EndPrint用於释放

11、资源。PrintDocument的PrintPage事件中 壬兄分直。基中BeginPrint的事件方法在PrintPage事件方法前被呼叫。PintPage的事件方法在EndPrintPage事件方法前被呼叫。EndPrint事件方法最后被呼叫,End Print事件方法结束后曾回到PrintDocument.Print()方法中,翱亍打印。其遇程如下:L 例化打印文槽2、U 囿事件(BeginPrint事件,用於得到被打印的内容;PinrtPage事件,用於 各彳固直内容;EndPrint事件,用於释放资源)3、调用BeginPrint事件的方法,得到打印内容4、用 PinrtPage事件

12、的方法,簿制多彳固打印直面,26根撼判断,言殳置是否迤行多直打印5、调用EndPrint事件的方法,释放资源,完成后始打印代碣如下:using System.IO;using System.Drawing.Printing;namespace SimpleEditor(public partial class SimpleEditorForm:Form(private string filename=Untitled;/l,例化打印文槽PrintDocument pdDocument=new PrintDocument();private string lines;private int li

13、nesPrinted;public SimpleEditorFormQ(InitializeComponentO;2、rr阅事件TT囿PinrtPage事件,用於簿裂各 内容pdDocument.PrintPage+=new PrintPageEventHandler(OnPrintPage);T耀BeginPrint事件,用於得到被打印的内容pdDocumentBeginPrint+=new PrintEventHandler(pdDocument_BeginPrint);/匍 囿EndPrint事件,用於释放资源pdDocument.EndPrint+=new PrintEventHand

14、ler(pdDocument_EndPrint);)private void OnFilePrint(object sender,EventArgs e)(try/癌用打印pdDocument.PrintO;*PrintDocument封 象 的Print。方 法 在Printcontroller类真的落助下,幸 丸 行PrintPage 事件。V)catch(InvalidPrinterException ex)(MessageBox.Show(ex.Message,Simple Editor,MessageBoxButtons.OK,MessageBoxIcon.Error);throw;

15、)/3、得到打印内容III每彳固打印任矜祇 用OnBeginPrint()一次。III/III void pdDocument_BeginPrint(object sender,PrintEventArgs e)char param=n;lines=textBoxEdit.Text.Split(param);int i=0;char trimParam=r;foreach(string s in lines)(linesi+=s.TrimEnd(trimParam);)/III 4、制多值I打印直面/printDocument 的 PrintPage 事件/III private void O

16、nPrintPage(object sender,PrintPageEventArgs e)/*得至!J TextBox中每彳元勺的串瞬*n操行*r回隼*/int x=20;int y=20;while(linesPrinted =e.PageBounds.Height-80)(允言午多直打印e.HasMorePages=true;*PrintPageEventArgs 的 HaeMorePages 腐性 True 日 寺,通知控件器,必乡直再次 用OnPrintPage()方 法,打印一彳固施。*PrintLoopK)有一值|用於每彳固要打印的直面的序例。如 果 HasMorePages是F

17、alse,PrintLoopO就曾停止。7return;)linesPrinted=0;簿制完成彳爰,多直打印功能e.HasMorePages=false;)III/5、End Print事件释放资源/III/void pdDocument_EndPrint(object sender,PrintEventArgs e)燮量Lines占用和引用的字符串瞬,现在释放lines=null;)要谨行版面 置需要使用PageSetupDialog类 鼠PageSetupDialog:可以配置直面大小和纸弓崽6原、方向、直谖距,因 些 都 依 赖 於 打 印 檄,所以在道他I封 框中也可以逗撵打印械。它

18、有一些腐性如下IS:表定列印格式SBAllowPagerAllo wOne ntati o n*PageSettings.Landscape4-1大小0):来源(S):列 印 方 向直印(0)-一。横印(A)PageSettings.Pape自就8取逡界(mm)上:10下(B):葭PageSettings.PapeAllo wMar ginMinMaginsPageSettings.Msnnter*-1魏 月:L直面AllowPaper:是 否 可 直 面 大 4坏口纸张来源。PageSetupDialog.PageSetting.PaperSize 腐性返回一值I PaperSize 例,用

19、它的腐性Height.Width和 PaperName可以自瓢高度、霓度和版是名蠹PaperName指定It 如 Letter和 A 4等名耦。Kind腐性返回T 固枚聚,优 中 可 獭 PaperKind枚聚的一彳固值,PaperKind枚聚包含台午多不同的纸弓是大小,例如 A3、A4、A5、Letter.LetterPlus 和 LetterRotatedoPageSetupDiaog.PageSettings.PaperSource 腐tSiS回一(固 PaperSource 例,其中可以 取打印械纸张来源和相鹰的纸弓戢型(只要打印械用言亥打印腌置迤行了正碓的配置)。2、直遏距Allow

20、Margins腐性允 用户 置打印输出的直谖距值。MinMargins可以悬用户定羲输入的最小直遏距值.PageSetupDialog.PageSettings.Margins 取 直 遏 距,值有 Bottom,Left,Right 和 Top 厩性。3方向AllowOrientation匾性定羲用户是否可以圜翱S向和横向打印方式。PageSetupDialog.PageSettings.Landscape 值可以 取iO定的值。横 向 True,向悬False4、打印檄AllowPrinter腐性指定用户是否可逗撵打印檄。PageSetupDialog 的用法使用PageSetupDial

21、og的方法比较曾军,只需1、在置例化一1 固 PageSetupDialog2、置 document周性 置 需要打印的文槽3、呼叫ShowDialogO方 法,打 版 面 置如前遏例子中加入版面 置,代礁如下:using System.IO;using System.Drawing.Printing;namespace SimpleEditorpublic partial class SimpleEditorForm:Form(private string filename=Untitled;例化打印文槽PrintDocument pdDocument=new PrintDocument()

22、;/l.打印格式 置直面PageSetupDialog dlgPageSetup=new PageSetupDialogO;private string lines;private int linesPrinted;public SimpleEditorFormO(InitializeComponentO;/福丁 事件l丁囿PinrtPage事件,用於簿裂各偃1 直内容pdDocument.PrintPage+=new PrintPageEventHandler(OnPrintPage);/BTM BeginPrint事件,用於得到被打印的内容pdDocument.BeginPrint+=ne

23、w PrintEventHandler(pdDocument_BeginPrint);/信丁 EndPrint事件,用於释放资源pdDocument.EndPrint+=new PrintEventHandler(pdDocument_EndPrint);2、直面IS置的打印文槽官殳置 需要打印的文樟dlgPageSetup.Document=pdDocument;)/3、在直面言殳置按 事件中呼叫直面者殳置界面,道 麝 擎“直面言殳置按 就曾弹出面 置界面III III III private void OnFilePageSetup(object sender,EventArgs e)dl

24、gPageSetup.ShowDialogO;)private void OnFilePrint(object sender,Eve nt Args e)/扁用打印pdDocument.Print();/*PrintDocument封 象 的 Print。方 法 在 Printcontroller 的 落 助 下,幸 丸 行PrintPage 事件。V)catch(InvalidPrinterException ex)(MessageBox.Show(ex.Message,Simple Editor,MessageBoxButtons.OK,MessageBoxIcon.Error);thro

25、w;)/得到打印内容/每彳固打印任福氏 用OnBeginPrintO一次。III III/void pdDocument_BeginPrint(object sender,PrintEventArgs e)(char param=n;lines=textBoxEdit.Text.Split(param);int i=0;char trimParam=V ;foreach(string s in lines)(linesi+=s.TrimEnd(trimParam);)/簿制多值1打印直面/printDocument 的 PrintPage 事件/private void OnPrintPage

26、(object sender,PrintPageEventArgs e)(/*得 至!I TextBox中每行的字符串嬲目*n 操行*r 回隼*/int x=20;int y=20;while(linesPrinted =e.PageBounds.Height-80)(允言午多直打印e.HasMorePages=true;/*PrintPageEventArgs类直的HaeMorePages腐性卷True畤,通知控件器,必,直再次 用OnPrintPage()方 法,打印一彳固直面。*PrintLoopK)有一彳固用於每彳固要打印的直面的序例。如 果HasMorePages是False,Pri

27、ntLoopO就曾停止。*/return;)linesPrinted=0;制 完 成 篌,昌昌朗多 打印功能e.HasMorePages=false;)/End Print事件释放资源/void pdDocument_EndPrint(object sender,PrintEventArgs e)(燮量Lines占用和引用的字符串麻,现在释放lines=null;)要想弹出 打印 窗口,可以在程式中呼叫PrintDialog界面。PrintDialog 允 rF用户优已安装的打印械中逗撵一台打印檄,逗撵打印檄份数和其它一找印言 殳 置,例如佛局和打印例K张来源。PrintDialog 相I I

28、 腐性如下Hl:列印制 月:AllowPrintToFile:允言午打印到文件逗工直AllowSelection:允前1打印逗中的文本PrintRange:允言午打印S6I1IAllowSomePage允 打印直面蒯凰PrinterSettings.FromPage 优 哪 始PrinterSettings.ToPage 到哪直余吉束注:打印任矜的始可以用 OnBeginPrintO,冏PrintDialog.PrinterSettings.PrintRage腐性3矍得用户的Selection篷怎信息。此值卷枚 型。PrintDialog 的用法以舆 PageSetupDialog 用法 似。

29、即:1、在 例化一f固 PrintDialog2、置document腐性 置卷需要打印的文槽3、呼叫ShowDialog()方法,打 打印界面。代碣如下:(我凭封前次的代碣作燮燮更,在打印按 事件中加入弹出打印”界面的代碣。)using System.IO;using System.Drawing.Printing;namespace SimpleEditor(public partial class SimpleEditorForm:Form(private string filename=Untitledn;例化打印文槽PrintDocument pdDocument=new PrintD

30、ocument();打印格式 置直面PageSetupDialog dlgPageSetup=new PageSetupDialogO;/l.例化打印直面PrintDialog dlgPrint=new PrintDialogO;private string lines;private int linesPrinted;public SimpleEditorForm()(InitializeComponentO;/rr解 件/m PinrtPage事件,用於裂各他直内容pdDocument.PrintPage+=new PrintPageEventHandler(OnPnntPage);TmB

31、eginPrint事件用於得到被打印的内容pdDocument.BeginPrint+=new PrintEventHandler(pdDocument_BeginPrint);/唐丁囿EndPrint事件,用於释放资源pdDocument.EndPrint+=new PrintEventHandler(pdDocumenVEndPrint);直面置的打印文 置悬需要打印的文槽dlgPageSetup.Document=pdDocument;2、打印界面的打印文槽碧卷被打印文槽dlgPrintDocument=pdDocument;)/在直面 置按 事件中呼叫葩as置界面,造棣罩擎 直面 置

32、按 就曾弹出 直面 置 界 面/III/private void OnFilePageSetup(object sender,EventArgs e)(dlgPageSetup.ShowDialogO;)private void OnFilePrint(object sender,EventArgs e)(try(判断是否有撵文本if(textBoxEdit.SelectedText(=)如果有圜f文 本,即可以逗雷打印圜I的靶凰”dlgPrint.AllowSelection=true;)else(dlgPrintAllowSelection=false;)3、呼叫打印界面if(dlgPri

33、nt.ShowDialogO=DialogResult.OK)(/*PrintDocument 封象的 Print。方法在 Printcontroller 的 落 助 下,轨行PrintPage 事件。*/pdDocument.Print();)catch(InvalidPrinterException ex)MessageBox.Show(ex.Message,Simple Editor,MessageBoxButtons.OK,MessageBoxIcon.Error);throw;)/得到打印内容/每彳固打印任矜祇 用OnBeginPrint。一次。III III/void pdDocu

34、ment_BeginPrint(object sender,PrintEventArgs e)(char param=n;lines=textBoxEdit.Text.Split(param);int i=0;char trimParam=r;foreach(string s in lines)(linesi+=s.TrimEnd(trimParam);)/制多彳固打印直面III printDocument 的 PrintPage 事件III III/private void OnPrintPage(object sender,PrintPageEventArgs e)(/*得 至!J Tex

35、tBox中每行的字符串蝴*n换行*r回隼*/int x=20;int y=20;while(linesPrinted=e.PageBounds.Height-80)(允言午多直打印e.HasMorePages=true;/*PrintPageEventArgs 的 HaeMorePages 圈性篇 True 畤,通知控件器,必,直再次调用OnPrintPage()方 法,打印一彳固直面。*PrintLoopI()有一彳固用於每彳固要打印的直面的序例。如 果HasMorePages是False,PrintLoop()就曾停止。*/return;linesPrinted=0;制完成彳爰,多直打印功

36、能e.HasMorePages=false;)/EndPrint事件府放资源III/void pdDocument_EndPrint(object sender,PrintEventArgs e)(燮量Lines占 用 和 引 用 的 字 符 串 瞬,现在释放lines=null;)il棣我优就可以打印界面中撵相殳定了。现在文件可以打印了,版面也可以言殳置了,在打印界面中也可 作 相 殳 定 了,可是我凭如何 霓列印呢?使用打印予直霓界面可以使用PrintPreviewDialog,Net中 打 印 可 使 用PrintPreviewControl 可以用来在窗醴中 霓文槽。但适值的提供相 菜

37、 罩。PrintPreviewDialog在 PrintPreviewControl有基磁上封装了一些控件的堂 舌框。它派生於System.Windows.Forms.Formo道里我凭用PrintPreviewDialog界面迤行打印予直霓。PrintPreviewDialog 的用法典 PageSetupDiaog 及 PrintDialog 用法类直似。即:1、在 例化一(固 PrintPreviewDialog2、置 document腐性 置卷需要打印的文槽3、呼叫ShowDialogO方 法,打 ”打印界面。注:打印 DataGridView 例 子:http:/ 加 功 能。代码如

38、下:using System.IO;using System.Drawing.Printing;namespace SimpleEditor(public partial class SimpleEditorForm:Form(private string filename=Untitled;打印文槽PrintDocument pdDocument=new PrintDocumentO;打印格式 置 面PageSetupDialog dlgPageSetup=new PageSetupDialogO;打印宜面PrintDialog dlgPrint=new PrintDialogO;/l.例化

39、打印颈霓PrintPreviewDialog dlgPrintPreview=new PrintPreviewDialogO;private string lines;private int linesPrinted;public SimpleEditorFormO(InitializeComponentO;pdDocument.PrintPage+=new PnntPageEventHandler(OnPrintPage);pdDocument.BeginPrint+=new PrintEventHandler(pdDocument_BeginPrint);pdDocument.EndPri

40、nt+=new PnntEventHandler(pdDocument_EndPnnt);直面置的打印文 置卷需要打印的文槽dlgPageSetup.Document=pdDocument;打印界面的打印文 置卷被打印文槽dlgPrint.Document=pdDocument;2、打印颈霓的打印文 置 被打印文槽dlgPrintPreview.Document=pdDocument;)/打 印 按 事 件/III param name=sender/private void OnFilePrintPreview(object sender,EventArgs e)(3、示打印予直霓界面dlg

41、PrintPreview.ShowDialogO;)/直面 置III/private void OnFilePageSetup(object sender,EventArgs e)(dlgPageSetup.ShowDialogO;)private void OnExit(object sender,EventArgs e)()/常按下打印畤,此卷界面中的打印按 事件III III /private void OnFilePrint(object sender,EventArgs e)(try(判断是否有逗撵文本if(textBoxEdit.SelectedText!=)如果有逗撵文本,即可以

42、逗撵 打印喻的轮圉”dlgPrintAllowSelection=true;)else(dlgPrintAllowSelection=false;)呼叫打印界面if(dlgPrint.ShowDialogO=DialogResult.OK)(/*PrintDocument些寸象的Print。方 法 在Printcontroller类真的落助下,轨行PrintPage 事件。*/pdDocument.Print();)catch(InvalidPrinterException ex)MessageBox.Show(ex.Message,Simple Editor,MessageBoxButton

43、s.OK,MessageBoxIcon.Error);throw;)/每彳固打印任矜祇 用OnBeginPrint()一次。III所有要打印的内容都在此 置III/void pdDocument_BeginPrint(object sender,PrintEventArgs e)(char param=n;/lines=textBoxEdit.Text.Split(param);判断是否逗取列印被i巽撵的靶圉if(dlgPrintPnnterSettings.PrintRange=PnntRange.Selection)(lines=textBoxEdit.SelectedText.Split

44、(param);elselines=textBoxEdit.Text.Split(param);)int i=0;char trimParam=r;foreach(string s in lines)(linesi+=s.TrimEnd(trimParam);)/III printDocument 的 PrintPage 事件/III/private void OnPrintPage(object sender,PrintPageEventArgs e)*得 至!I TextBox中每行的字符串翳族目*n 换行*r 回隼*/int x=e.MarginBounds.Left;int y=e.M

45、arginBounds.Top;while(linesPrinted=e.PageBounds.Height-80)(多直打印e.HasMorePages=true;/*PrintPageEventArgs 类 直 的 HaeMorePages True 日 寺,通知控,必须再次 用OnPrintPage()方法,打印一彳固直面。*PrintLoopK)有一彳固用於每彳固要打印的直面的序例。如 果 HasMorePages是False,PrintLoopO就曾停止。*/return;)linesPrinted=0;e.HasMorePages=false;)/EndPrint事件释放BeginPrint方法中估用的资源/III void pdDocument_EndPrint(object sender,PrintEventArgs e)(燮量Lines占用和引用的字符串蝴,现在释放lines=null;)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com