自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#打印文本文件實(shí)例詳解

開發(fā) 后端
C#打印文本文件是如何實(shí)現(xiàn)的呢?C#打印文本文件用到的類是什么呢?C#打印文本文件實(shí)現(xiàn)的效果是什么呢?那么本文就向你介紹具體的內(nèi)容。

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

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

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

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

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

程序運(yùn)行 

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

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

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

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

這幅圖中的打印機(jī):“Microsoft Office Doument Image Writer”是 Microsoft Office 2003 軟件提供一個(gè)虛擬打印機(jī),用來調(diào)試打印程序非常方便(使用“打印預(yù)覽”也可以調(diào)試打印程序,但“打印預(yù)覽”只能使用默認(rèn)的打印機(jī)和默認(rèn)的打印屬性,也不能設(shè)置頁(yè)碼范圍),可以設(shè)置打印屬性和頁(yè)碼范圍以及打印份數(shù)。使用它來調(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)類的源代碼:

  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 == nullbreak;  
  86. }  
  87. if (line == nullreturn;  
  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 == nullbreak;  
  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è)類庫(kù)有個(gè)缺點(diǎn):當(dāng)C#文本文件中的一行不能在打印紙的一行中打印完時(shí),該行的后半部就丟失了。

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

【編輯推薦】

  1. C#實(shí)現(xiàn)打印功能實(shí)例詳解
  2. 淺析C#打印和C#打印預(yù)覽的實(shí)現(xiàn)
  3. 全面解析C#實(shí)現(xiàn)打印功能
  4. 實(shí)現(xiàn)C#打印窗體實(shí)例詳解
  5. 實(shí)現(xiàn)C#打印文檔實(shí)例詳解
責(zé)任編輯:仲衡 來源: 博客園
相關(guān)推薦

2009-08-19 17:44:15

C#操作文本文件

2009-08-20 10:17:27

C#操作文本文件

2009-08-06 18:33:45

C#處理文本文件

2009-09-02 19:13:08

C#處理文本文件

2009-08-20 09:58:06

C#操作文本文件

2009-09-02 19:08:03

C#實(shí)現(xiàn)讀取文本文件

2009-08-20 09:15:20

C#操作文本文件

2009-08-12 17:59:48

C#讀取文本文

2009-08-20 09:26:14

C#操作文本文件

2009-08-12 17:42:57

C#讀文本文件

2010-02-01 14:26:50

C++讀寫文本文件

2010-04-30 17:38:31

Unix文本

2009-08-26 09:22:44

C#實(shí)現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-08-26 11:32:37

C#打印文檔

2021-11-29 09:46:11

FileReaderJava開發(fā)

2009-08-26 14:31:08

C#打印文件

2010-01-15 10:05:35

VB.NET文件對(duì)象

2009-09-04 15:56:35

寫入文本文件

2009-08-18 17:05:08

C#操作xml文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)