日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C#打印文本文件實(shí)例詳解

這是一個(gè)C#打印文本文件的實(shí)現(xiàn)類(lèi)庫(kù),這個(gè)程序的功能包括:C#打印文本文件預(yù)覽、C#打印文本文件。C#文本文件的打印時(shí)可以選擇打印機(jī),可以指定文本文件打印的頁(yè)碼范圍。調(diào)用方法非常簡(jiǎn)單,讓我們開(kāi)始吧:

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),陽(yáng)春企業(yè)網(wǎng)站建設(shè),陽(yáng)春品牌網(wǎng)站建設(shè),網(wǎng)站定制,陽(yáng)春網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,陽(yáng)春網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

 
 
 
 
  1. TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);  
  2. p.View();// 打印預(yù)覽  
  3. p.Print(); // 打印文件 

使用 TextFilePrinter 類(lèi)的以下構(gòu)造函數(shù)可以指定打印時(shí)使用的字體:

 
 
 
 
  1. TextFilePrinter(string fileName,   
  2.  
  3. Encoding theEncode, Font theFont)  

下面測(cè)試C#打印文本文件實(shí)現(xiàn)程序運(yùn)行時(shí)的截圖:

點(diǎn)擊“預(yù)覽”按鈕后:

點(diǎn)擊“打印”按鈕后:

這幅圖中的打印機(jī):“Microsoft Office Doument Image Writer”是 Microsoft Office 2003 軟件提供一個(gè)虛擬打印機(jī),用來(lái)調(diào)試打印程序非常方便(使用“打印預(yù)覽”也可以調(diào)試打印程序,但“打印預(yù)覽”只能使用默認(rèn)的打印機(jī)和默認(rèn)的打印屬性,也不能設(shè)置頁(yè)碼范圍),可以設(shè)置打印屬性和頁(yè)碼范圍以及打印份數(shù)。使用它來(lái)調(diào)試打印程序,可以節(jié)省不少打印紙。為建設(shè)節(jié)約型社會(huì)作貢獻(xiàn) ????

這幅圖就是該虛擬打印機(jī)在屏幕上的顯示的結(jié)果。

這里是測(cè)試C#打印文本文件程序的源代碼:

 
 
 
 
  1. // PrintFile.cs - 文件打印程序  
  2. // 編譯方法: csc /t:winexe PrintFile.cs TextFilePrinter.cs  
  3.  
  4. using System;  
  5. using System.Drawing;  
  6. using System.Windows.Forms;  
  7. using Skyiv.Util;  
  8.  
  9. namespace Skyiv.Ben.Test  
  10. {  
  11. class PrintFileForm : Form  
  12. {  
  13. TextBox tbxFileName;  
  14.  
  15. public PrintFileForm()  
  16. {  
  17. SuspendLayout();  
  18.  
  19. Button btnFileName = new Button();  
  20. btnFileName.Text = "文件名";  
  21. btnFileName.Location = new Point(10, 10);  
  22. btnFileName.Size = new Size(60, 24);  
  23. btnFileName.Click += new EventHandler(BtnFileName_Click);  
  24.  
  25. Button btnPrint = new Button();  
  26. btnPrint.Text = "打印";  
  27. btnPrint.Location = new Point(75, 10);  
  28. btnPrint.Size = new Size(60, 24);  
  29. btnPrint.Click += new EventHandler(BtnPrint_Click);  
  30.  
  31. Button btnView = new Button();  
  32. btnView.Text = "預(yù)覽";  
  33. btnView.Location = new Point(140, 10);  
  34. btnView.Size = new Size(60, 24);  
  35. btnView.Click += new EventHandler(BtnView_Click);  
  36.  
  37. tbxFileName = new TextBox();  
  38. tbxFileName.Text = "PrintFile.cs";  
  39. tbxFileName.Location = new Point(10, 44);  
  40. tbxFileName.Size = new Size(190, 20);  
  41. tbxFileName.ReadOnly = true;  
  42. tbxFileName.Anchor = (  
  43. AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);  
  44.  
  45. Controls.AddRange(new Control[]{  
  46. btnFileName, btnPrint, btnView, tbxFileName});  
  47. Text = "文本文件打印程序";  
  48. ClientSize = new Size(210, 80);  
  49.  
  50. ResumeLayout(false);  
  51. }  
  52.  
  53. void BtnFileName_Click(object sender, EventArgs e)  
  54. {  
  55. OpenFileDialog dlg = new OpenFileDialog();  
  56. if(dlg.ShowDialog() != DialogResult.OK) return;  
  57. tbxFileName.Text = dlg.FileName;  
  58. }  
  59.  
  60. void BtnPrint_Click(object sender, EventArgs e)  
  61. {  
  62. TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);  
  63. p.Print();  
  64. }  
  65.  
  66. void BtnView_Click(object sender, EventArgs e)  
  67. {  
  68. TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);  
  69. p.View();  
  70. }  
  71.  
  72. static void Main()  
  73. {  
  74. Application.Run(new PrintFileForm());  
  75. }  
  76. }  
  77. }  

這里是C#打印文本文件實(shí)現(xiàn)類(lèi)的源代碼:

 
 
 
 
  1. using System;  
  2. using System.Drawing;  
  3. using System.Drawing.Printing;  
  4. using System.Windows.Forms;  
  5. using System.IO;  
  6. using System.Text;  
  7.  
  8. namespace Skyiv.Util  
  9. {  
  10. sealed class TextFilePrinter  
  11. {  
  12. string fileName;  
  13. Encoding theEncode;  
  14. Font theFont;  
  15. StreamReader srToPrint;  
  16. int currPage;  
  17.  
  18. public TextFilePrinter(string fileName)  
  19. : this(fileName,   
  20. Encoding.GetEncoding("GB18030"), new Font("新宋體", 10))  
  21. {  
  22. }  
  23.  
  24. public TextFilePrinter(string fileName,   
  25. Encoding theEncode, Font theFont)  
  26. {  
  27. this.fileName = fileName;  
  28. this.theEncode = theEncode;  
  29. this.theFont = theFont;  
  30. }  
  31.  
  32. public void Print()  
  33. {  
  34. using (srToPrint =   
  35. new StreamReader(fileName, theEncode))  
  36. {  
  37. PrintDialog dlg = new PrintDialog();  
  38. dlg.Document = GetPrintDocument();  
  39. dlg.AllowSomePages = true;  
  40. dlg.AllowPrintToFile = false;  
  41. if (dlg.ShowDialog() ==   
  42. DialogResult.OK) dlg.Document.Print();  
  43. }  
  44. }  
  45.  
  46. public void View()  
  47. {  
  48. using (srToPrint =   
  49. new StreamReader(fileName, theEncode))  
  50. {  
  51. PrintPreviewDialog dlg = new PrintPreviewDialog();  
  52. dlg.Document = GetPrintDocument();  
  53. dlg.ShowDialog();  
  54. }  
  55. }  
  56.  
  57. PrintDocument GetPrintDocument()  
  58. {  
  59. currPage = 1;  
  60. PrintDocument doc = new PrintDocument();  
  61. doc.DocumentName = fileName;  
  62. doc.PrintPage +=   
  63. new PrintPageEventHandler(PrintPageEvent);  
  64. return doc;  
  65. }  
  66.  
  67. void PrintPageEvent(object sender,   
  68. PrintPageEventArgs ev)  
  69. {  
  70. string line = null;  
  71. float linesPerPage =   
  72. ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics);  
  73. bool isSomePages =   
  74. ev.PageSettings.PrinterSettings.PrintRange ==   
  75. PrintRange.SomePages;  
  76. if (isSomePages)  
  77. {  
  78. while (currPage   
  79. < ev.PageSettings.PrinterSettings.FromPage)  
  80. {  
  81. for (int count = 0; count   
  82. < linesPerPage; count++)  
  83. {  
  84. line = srToPrint.ReadLine();  
  85. if (line == null) break;  
  86. }  
  87. if (line == null) return;  
  88. currPage++;  
  89. }  
  90. if (currPage >   
  91. ev.PageSettings.PrinterSettings.ToPage) return;  
  92. }  
  93. for (int count = 0; count < linesPerPage; count++)  
  94. {  
  95. line = srToPrint.ReadLine();  
  96. if (line == null) break;  
  97. ev.Graphics.DrawString(line,  
  98.  theFont, Brushes.Black, ev.MarginBounds.Left,  
  99. ev.MarginBounds.Top + (  
  100. count * theFont.GetHeight(ev.Graphics)),   
  101. new StringFormat());  
  102. }  
  103. currPage++;  
  104. if (isSomePages &&  
  105.  currPage > ev.PageSettings.PrinterSettings.ToPage) return;  
  106. if (line != null) ev.HasMorePages = true;  
  107. }  
  108. }  
  109. }  

這些程序都相當(dāng)簡(jiǎn)當(dāng)明了,這里就不再解釋了。

這個(gè)類(lèi)庫(kù)有個(gè)缺點(diǎn):當(dāng)C#文本文件中的一行不能在打印紙的一行中打印完時(shí),該行的后半部就丟失了。

C#打印文本文件的具體內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#打印文本文件有所幫助。


網(wǎng)站欄目:C#打印文本文件實(shí)例詳解
標(biāo)題路徑:http://www.5511xx.com/article/cogosdp.html