分享

C#打印文本文件实例详解 - 51CTO.COM

 coding 2010-08-01
    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#打印文本文件有所帮助。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约