C #打印详解.pdf
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- #打印详解 打印 详解
限制150内