private void button2_Click(object sender, System.EventArgs e)
{
StreamReader streamToPrint = new StreamReader ("sdf.Txt",System.Text.Encoding.Default);
try 
{
TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); if (storedPageSettings != null) 
{
pd.DefaultPageSettings = storedPageSettings ;
} PrintDialog dlg = new PrintDialog() ;
dlg.Document = pd;
DialogResult result = dlg.ShowDialog(); if (result == DialogResult.OK)
{ PrintPreviewDialog pdlg = new PrintPreviewDialog() ;
pdlg.Document = pd;
pdlg.ShowDialog();
} } 
finally 
{
streamToPrint.Close() ;
} }
}
public class TextFilePrintDocument : PrintDocument 
{ private Font printFont           = null ;
private StreamReader streamToPrint = null ; public TextFilePrintDocument(StreamReader streamToPrint) : base ()  
{
this.streamToPrint = streamToPrint ;
} //Override OnBeginPrint to set up the font we are going to use
protected override void OnBeginPrint(PrintEventArgs ev) 
{
base.OnBeginPrint(ev) ;
printFont = new Font("宋体", 10);
}
//Override the OnPrintPage to provide the printing logic for the document
protected override void OnPrintPage(PrintPageEventArgs ev) 
{ base.OnPrintPage(ev) ; float lpp = 0 ;
float yPos =  0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line=null; //Work out the number of lines per page 
//Use the MarginBounds on the event to do this 
lpp = ev.MarginBounds.Height  / printFont.GetHeight(ev.Graphics) ; //Now iterate over the file printing out each line
//NOTE WELL: This assumes that a single line is not wider than the page width
//Check count first so that we don't read line that we won't print
while (count < lpp && ((line=streamToPrint.ReadLine()) != null)) 
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); //Print Preview control will not work.
ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin,
yPos, new StringFormat()); count++;
} //If we have more lines then print another page
if (line != null)
ev.HasMorePages = true ;
else
{
ev.HasMorePages = false ;
}
} }