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

这些程序都相当简当明了,这里就不再解释了。

这个类库有个缺点:当C#文本文件中的一行不能在打印纸的一行中打印完时,该行的后半部就丢失了。

C#打印文本文件的具体内容就向你介绍到这里,希望对你了解和学习C#打印文本文件有所帮助。

网页标题:C#打印文本文件实例详解
分享链接:http://www.36103.cn/qtweb/news49/37549.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联