C#打印文本文件實(shí)例詳解
這是一個(gè)C#打印文本文件的實(shí)現(xiàn)類庫(kù),這個(gè)程序的功能包括:C#打印文本文件預(yù)覽、C#打印文本文件。C#文本文件的打印時(shí)可以選擇打印機(jī),可以指定文本文件打印的頁(yè)碼范圍。調(diào)用方法非常簡(jiǎn)單,讓我們開始吧:
- TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
- p.View();// 打印預(yù)覽
- p.Print(); // 打印文件
使用 TextFilePrinter 類的以下構(gòu)造函數(shù)可以指定打印時(shí)使用的字體:
- TextFilePrinter(string fileName,
- 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ī),用來調(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#打印文本文件程序的源代碼:
- // PrintFile.cs - 文件打印程序
- // 編譯方法: csc /t:winexe PrintFile.cs TextFilePrinter.cs
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using Skyiv.Util;
- namespace Skyiv.Ben.Test
- {
- class PrintFileForm : Form
- {
- TextBox tbxFileName;
- public PrintFileForm()
- {
- SuspendLayout();
- Button btnFileName = new Button();
- btnFileName.Text = "文件名";
- btnFileName.Location = new Point(10, 10);
- btnFileName.Size = new Size(60, 24);
- btnFileName.Click += new EventHandler(BtnFileName_Click);
- Button btnPrint = new Button();
- btnPrint.Text = "打印";
- btnPrint.Location = new Point(75, 10);
- btnPrint.Size = new Size(60, 24);
- btnPrint.Click += new EventHandler(BtnPrint_Click);
- Button btnView = new Button();
- btnView.Text = "預(yù)覽";
- btnView.Location = new Point(140, 10);
- btnView.Size = new Size(60, 24);
- btnView.Click += new EventHandler(BtnView_Click);
- tbxFileName = new TextBox();
- tbxFileName.Text = "PrintFile.cs";
- tbxFileName.Location = new Point(10, 44);
- tbxFileName.Size = new Size(190, 20);
- tbxFileName.ReadOnly = true;
- tbxFileName.Anchor = (
- AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
- Controls.AddRange(new Control[]{
- btnFileName, btnPrint, btnView, tbxFileName});
- Text = "文本文件打印程序";
- ClientSize = new Size(210, 80);
- ResumeLayout(false);
- }
- void BtnFileName_Click(object sender, EventArgs e)
- {
- OpenFileDialog dlg = new OpenFileDialog();
- if(dlg.ShowDialog() != DialogResult.OK) return;
- tbxFileName.Text = dlg.FileName;
- }
- void BtnPrint_Click(object sender, EventArgs e)
- {
- TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
- p.Print();
- }
- void BtnView_Click(object sender, EventArgs e)
- {
- TextFilePrinter p = new TextFilePrinter(tbxFileName.Text);
- p.View();
- }
- static void Main()
- {
- Application.Run(new PrintFileForm());
- }
- }
- }
這里是C#打印文本文件實(shí)現(xiàn)類的源代碼:
- using System;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.Windows.Forms;
- using System.IO;
- using System.Text;
- namespace Skyiv.Util
- {
- sealed class TextFilePrinter
- {
- string fileName;
- Encoding theEncode;
- Font theFont;
- StreamReader srToPrint;
- int currPage;
- public TextFilePrinter(string fileName)
- : this(fileName,
- Encoding.GetEncoding("GB18030"), new Font("新宋體", 10))
- {
- }
- public TextFilePrinter(string fileName,
- Encoding theEncode, Font theFont)
- {
- this.fileName = fileName;
- this.theEncode = theEncode;
- this.theFont = theFont;
- }
- public void Print()
- {
- using (srToPrint =
- new StreamReader(fileName, theEncode))
- {
- PrintDialog dlg = new PrintDialog();
- dlg.Document = GetPrintDocument();
- dlg.AllowSomePages = true;
- dlg.AllowPrintToFile = false;
- if (dlg.ShowDialog() ==
- DialogResult.OK) dlg.Document.Print();
- }
- }
- public void View()
- {
- using (srToPrint =
- new StreamReader(fileName, theEncode))
- {
- PrintPreviewDialog dlg = new PrintPreviewDialog();
- dlg.Document = GetPrintDocument();
- dlg.ShowDialog();
- }
- }
- PrintDocument GetPrintDocument()
- {
- currPage = 1;
- PrintDocument doc = new PrintDocument();
- doc.DocumentName = fileName;
- doc.PrintPage +=
- new PrintPageEventHandler(PrintPageEvent);
- return doc;
- }
- void PrintPageEvent(object sender,
- PrintPageEventArgs ev)
- {
- string line = null;
- float linesPerPage =
- ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics);
- bool isSomePages =
- ev.PageSettings.PrinterSettings.PrintRange ==
- PrintRange.SomePages;
- if (isSomePages)
- {
- while (currPage
- < ev.PageSettings.PrinterSettings.FromPage)
- {
- for (int count = 0; count
- < linesPerPage; count++)
- {
- line = srToPrint.ReadLine();
- if (line == null) break;
- }
- if (line == null) return;
- currPage++;
- }
- if (currPage >
- ev.PageSettings.PrinterSettings.ToPage) return;
- }
- for (int count = 0; count < linesPerPage; count++)
- {
- line = srToPrint.ReadLine();
- if (line == null) break;
- ev.Graphics.DrawString(line,
- theFont, Brushes.Black, ev.MarginBounds.Left,
- ev.MarginBounds.Top + (
- count * theFont.GetHeight(ev.Graphics)),
- new StringFormat());
- }
- currPage++;
- if (isSomePages &&
- currPage > ev.PageSettings.PrinterSettings.ToPage) return;
- if (line != null) ev.HasMorePages = true;
- }
- }
- }
這些程序都相當(dāng)簡(jiǎn)當(dāng)明了,這里就不再解釋了。
這個(gè)類庫(kù)有個(gè)缺點(diǎn):當(dāng)C#文本文件中的一行不能在打印紙的一行中打印完時(shí),該行的后半部就丟失了。
C#打印文本文件的具體內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#打印文本文件有所幫助。
【編輯推薦】