以下示例将在默认打印机上打印名为 C:\My Documents\MyFile.txt 的文件。public class PrintingExample : System.Windows.Forms.Form { private System.ComponentModel.Container components; private System.Windows.Forms.Button printButton; private Font printFont; private StreamReader streamToPrint; public PrintingExample() : base() { // The Windows Forms Designer requires the following call. InitializeComponent(); } // The Click event is raised when the user clicks the Print button. private void printButton_Click(object sender, EventArgs e) { try { streamToPrint = new StreamReader ("C:\\My Documents\\MyFile.txt"); try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage); pd.Print(); } finally { streamToPrint.Close(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } // The PrintPage event is raised for each page to be printed. private void pd_PrintPage(object sender, PrintPageEventArgs ev) { float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; // Calculate the number of lines per page. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print each line of the file. while(count < linesPerPage && ((line=streamToPrint.ReadLine()) != null)) { yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } // If more lines exist, print another page. if(line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } // The Windows Forms Designer requires the following procedure. private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.printButton = new System.Windows.Forms.Button(); this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(504, 381); this.Text = "Print Example"; printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; printButton.Location = new System.Drawing.Point(32, 110); printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; printButton.TabIndex = 0; printButton.Text = "Print the file."; printButton.Size = new System.Drawing.Size(136, 40); printButton.Click += new System.EventHandler(printButton_Click); this.Controls.Add(printButton); } // This is the main entry point for the application. public static void Main(string[] args) { Application.Run(new PrintingExample()); } }
看看 msdn 上的 System.Drawing.Printing 命名空间, 特别是 PrintDocument 类, 对你会有所帮助的.
System.Drawing.Printing 命名空间提供与打印相关的服务。通常,您会创建 PrintDocument 类的新实例,设置对打印内容进行描述的属性,然后调用 Print 方法来实际打印文档。使用 PrintPageEventArgs.Graphics 属性可指定要打印的输出内容。如果您正在打印文本文件,请按如下方式使用 StreamReader:从输出流中一次读取一行,然后调用 DrawString 方法绘制图形对象中的行。有关此过程的更多信息,请参见 Graphics 和 StreamReader 类。当在派生类中实现后,PrintController 控制如何打印 PrintDocument。PrintDocument.Print 方法调用打印控制器的 OnStartPrint、OnEndPrint、OnStartPage 和 OnEndPage 方法,这些方法指示打印机如何打印文档。有关打印对话框的更多信息,请参见 PrintDialog 和 PageSetupDialog。打印预览进程使用专用的打印控制器、对话框和控件。有关这种打印控制器和对话框的示例,请参见 PreviewPrintController、PrintPreviewDialog 和 PrintPreviewControl。
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint; public PrintingExample() : base()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
} // The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
} // The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null; // Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics); // Print each line of the file.
while(count < linesPerPage &&
((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
} // If more lines exist, print another page.
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button(); this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example"; printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click); this.Controls.Add(printButton);
} // This is the main entry point for the application.
public static void Main(string[] args)
{
Application.Run(new PrintingExample());
}
}