C#打印文本文件實例詳解

  • C#打印文本文件是如何實現的呢?C#打印文本文件用到的類是什麼呢?C#打印文本文件實現的效果是什麼呢?那麼本文就向你介紹具體的內容。

 

這是一個C#打印文本文件的實現類庫,這個程序的功能包括:C#打印文本文件預覽、C#打印文本文件。C#文本文件的打印時可以選擇打印機,可以指定文本文件打印的頁碼範圍。調用方法非常簡單,讓我們開始吧:

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

使用 TextFilePrinter 類的以下構造函數可以指定打印時使用的字體:

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

下面測試C#打印文本文件實現程序運行時的截圖:

程序運行 

點擊“預覽”按鈕後:

點擊“預覽”按鈕 

點擊“打印”按鈕後:

點擊“打印”按鈕 

這幅圖中的打印機:“Microsoft Office Doument Image Writer”是 Microsoft Office 2003 軟件提供一個虛擬打印機,用來調試打印程序非常方便(使用“打印預覽”也可以調試打印程序,但“打印預覽”只能使用默認的打印機和默認的打印屬性,也不能設置頁碼範圍),可以設置打印屬性和頁碼範圍以及打印份數。使用它來調試打印程序,可以節省不少打印紙。爲建設節約型社會作貢獻 :)

 

這幅圖就是該虛擬打印機在屏幕上的顯示的結果。

這裏是測試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 = "預覽";  
  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#打印文本文件實現類的源代碼:

  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. }  

這些程序都相當簡當明瞭,這裏就不再解釋了。

這個類庫有個缺點:當C#文本文件中的一行不能在打印紙的一行中打印完時,該行的後半部就丟失了。

C#打印文本文件的具體內容就向你介紹到這裏,希望對你瞭解和學習C#打印文本文件有所幫助。

 

 ********************************************************************************

 

這個程序我試過了,需要將代碼複製到vs2008創建的窗體程序的相應部分,然後要將計算機服務的Print Spooler設置爲啓動才能看的打印預覽,不然會一直提示要裝打印機。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章