实例代码如下:
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.IO;
using
System.Text;
using
System.Drawing.Printing;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
PrintDemo
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////<summary>
///Form1的摘要说明。
///</summary>
publicclassDemoForm:System.Windows.Forms.Form
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
privateSystem.Windows.Forms.TextBoxtxtDoc;
privateSystem.Windows.Forms.MainMenumnuMain;
privateSystem.Windows.Forms.MenuItemmnuFile;
privateSystem.Windows.Forms.MenuItemmnuFileOpen;
privateSystem.Windows.Forms.MenuItemmnuFilePrint;
privateSystem.Windows.Forms.MenuItemmnuFormat;
privateSystem.Windows.Forms.MenuItemmnuFormatFont;
privateSystem.Windows.Forms.OpenFileDialogdlgOpen;
privateSystem.Windows.Forms.FontDialogdlgFont;
privateSystem.Drawing.Printing.PrintDocumentpdoc;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
privateSystem.Windows.Forms.MenuItemmnuFilePrintPreview;
privateSystem.Windows.Forms.MenuItemmnuFilePageSetup;
privateSystem.Windows.Forms.PrintPreviewDialogppd;
privateSystem.Windows.Forms.PageSetupDialogdlgPageSetup;
privateSystem.Windows.Forms.PrintDialogdlgPrinterSetup;
privateinttotalLines;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
publicDemoForm()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////<summary>
///清理所有正在使用的资源。
///</summary>
protectedoverridevoidDispose(booldisposing)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(disposing)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(components!=null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
components.Dispose();
}
}
base.Dispose(disposing);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Windows窗体设计器生成的代码#regionWindows窗体设计器生成的代码
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Resources.ResourceManagerresources=newSystem.Resources.ResourceManager(typeof(DemoForm));
this.txtDoc=newSystem.Windows.Forms.TextBox();
this.mnuMain=newSystem.Windows.Forms.MainMenu();
this.mnuFile=newSystem.Windows.Forms.MenuItem();
this.mnuFileOpen=newSystem.Windows.Forms.MenuItem();
this.mnuFilePrint=newSystem.Windows.Forms.MenuItem();
this.mnuFormat=newSystem.Windows.Forms.MenuItem();
this.mnuFormatFont=newSystem.Windows.Forms.MenuItem();
this.dlgOpen=newSystem.Windows.Forms.OpenFileDialog();
this.dlgFont=newSystem.Windows.Forms.FontDialog();
this.pdoc=newSystem.Drawing.Printing.PrintDocument();
this.mnuFilePrintPreview=newSystem.Windows.Forms.MenuItem();
this.mnuFilePageSetup=newSystem.Windows.Forms.MenuItem();
this.ppd=newSystem.Windows.Forms.PrintPreviewDialog();
this.dlgPageSetup=newSystem.Windows.Forms.PageSetupDialog();
this.dlgPrinterSetup=newSystem.Windows.Forms.PrintDialog();
this.SuspendLayout();
//
//txtDoc
//
this.txtDoc.Dock=System.Windows.Forms.DockStyle.Fill;
this.txtDoc.Location=newSystem.Drawing.Point(0,0);
this.txtDoc.Multiline=true;
this.txtDoc.Name="txtDoc";
this.txtDoc.ScrollBars=System.Windows.Forms.ScrollBars.Both;
this.txtDoc.Size=newSystem.Drawing.Size(560,309);
this.txtDoc.TabIndex=0;
this.txtDoc.Text="";
this.txtDoc.WordWrap=false;
this.txtDoc.TextChanged+=newSystem.EventHandler(this.txtDoc_TextChanged);
//
//mnuMain
//
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.mnuMain.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]...{
this.mnuFile,
this.mnuFormat});
//
//mnuFile
//
this.mnuFile.Index=0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.mnuFile.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]...{
this.mnuFileOpen,
this.mnuFilePageSetup,
this.mnuFilePrintPreview,
this.mnuFilePrint});
this.mnuFile.Text="文件(&F)";
//
//mnuFileOpen
//
this.mnuFileOpen.Index=0;
this.mnuFileOpen.Text="打开(&O)...";
this.mnuFileOpen.Click+=newSystem.EventHandler(this.mnuFileOpen_Click);
//
//mnuFilePrint
//
this.mnuFilePrint.Index=3;
this.mnuFilePrint.Text="打印(&P)...";
this.mnuFilePrint.Click+=newSystem.EventHandler(this.mnuFilePrint_Click);
//
//mnuFormat
//
this.mnuFormat.Index=1;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.mnuFormat.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]...{
this.mnuFormatFont});
this.mnuFormat.Text="格式(&F)";
//
//mnuFormatFont
//
this.mnuFormatFont.Index=0;
this.mnuFormatFont.Text="字体(&O)";
this.mnuFormatFont.Click+=newSystem.EventHandler(this.mnuFormatFont_Click);
//
//dlgOpen
//
this.dlgOpen.Filter="文本文件|*.txt|所有文件|*.*";
//
//pdoc
//
this.pdoc.DocumentName="abc";
this.pdoc.BeginPrint+=newSystem.Drawing.Printing.PrintEventHandler(this.pdoc_BeginPrint);
this.pdoc.EndPrint+=newSystem.Drawing.Printing.PrintEventHandler(this.pdoc_EndPrint);
this.pdoc.PrintPage+=newSystem.Drawing.Printing.PrintPageEventHandler(this.pdoc_PrintPage);
//
//mnuFilePrintPreview
//
this.mnuFilePrintPreview.Index=2;
this.mnuFilePrintPreview.Text="打印预览...";
this.mnuFilePrintPreview.Click+=newSystem.EventHandler(this.mnuFilePrintPreview_Click);
//
//mnuFilePageSetup
//
this.mnuFilePageSetup.Index=1;
this.mnuFilePageSetup.Text="页面设置...";
this.mnuFilePageSetup.Click+=newSystem.EventHandler(this.mnuFilePageSetup_Click);
//
//ppd
//
this.ppd.AutoScrollMargin=newSystem.Drawing.Size(0,0);
this.ppd.AutoScrollMinSize=newSystem.Drawing.Size(0,0);
this.ppd.ClientSize=newSystem.Drawing.Size(400,300);
this.ppd.Document=this.pdoc;
this.ppd.Enabled=true;
this.ppd.Icon=((System.Drawing.Icon)(resources.GetObject("ppd.Icon")));
this.ppd.Location=newSystem.Drawing.Point(317,17);
this.ppd.MinimumSize=newSystem.Drawing.Size(375,250);
this.ppd.Name="ppd";
this.ppd.TransparencyKey=System.Drawing.Color.Empty;
this.ppd.Visible=false;
//
//dlgPageSetup
//
this.dlgPageSetup.Document=this.pdoc;
//
//dlgPrinterSetup
//
this.dlgPrinterSetup.Document=this.pdoc;
//
//DemoForm
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(560,309);
this.Controls.Add(this.txtDoc);
this.Menu=this.mnuMain;
this.Name="DemoForm";
this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text="打印";
this.ResumeLayout(false);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Application.Run(newDemoForm());
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidmnuFileOpen_Click(objectsender,System.EventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(dlgOpen.ShowDialog()==DialogResult.OK)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
StreamReadersr=null;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
sr=newStreamReader(dlgOpen.FileName,Encoding.Default,true);
txtDoc.Text=sr.ReadToEnd();
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox.Show("打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(sr!=null)sr.Close();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidmnuFormatFont_Click(objectsender,System.EventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dlgFont.Font=txtDoc.Font;
if(dlgFont.ShowDialog()==DialogResult.OK)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
txtDoc.Font=dlgFont.Font;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidpdoc_BeginPrint(objectsender,System.Drawing.Printing.PrintEventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//MessageBox.Show("开始打印啦");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidpdoc_EndPrint(objectsender,System.Drawing.Printing.PrintEventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//MessageBox.Show("打印结束");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidpdoc_PrintPage(objectsender,System.Drawing.Printing.PrintPageEventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Graphicsg=e.Graphics;
floatlineHeight=txtDoc.Font.GetHeight(g);
intlinesPerPage=(int)(e.MarginBounds.Height/lineHeight);
intcount=0;//本页已打印行数
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
while(count<linesPerPage&&totalLines<txtDoc.Lines.Length)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y+lineHeight*count);
count++;
totalLines++;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if(totalLines<txtDoc.Lines.Length)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.HasMorePages=true;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.HasMorePages=false;
totalLines=0;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidmnuFilePrint_Click(objectsender,System.EventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(dlgPrinterSetup.ShowDialog()==DialogResult.OK)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
pdoc.Print();//开始执行打印
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidtxtDoc_TextChanged(objectsender,System.EventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidmnuFilePrintPreview_Click(objectsender,System.EventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ppd.ShowDialog();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
privatevoidmnuFilePageSetup_Click(objectsender,System.EventArgse)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MarginsoldMargins=dlgPageSetup.PageSettings.Margins;
dlgPageSetup.PageSettings.Margins=newMargins((int)(oldMargins.Left*2.54),(int)(oldMargins.Right*2.54),(int)(oldMargins.Top*2.54),(int)(oldMargins.Bottom*2.54));
if(dlgPageSetup.ShowDialog()==DialogResult.Cancel)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dlgPageSetup.PageSettings.Margins=oldMargins;
}
}
}
}
这个实例存在一下缺陷,就是没有实现分行打印,不过这个问题其实比较复杂,要考虑并计算一行能打印多少个字符,尤其是在分页处还要判断上一页中分行都是否还有没有打印的文本需要有限处理,有兴趣的可以研究一下。
其实在实际开发应用中可能对文本文件打印是应用并不是太多,可以编辑、打印文本文件的工具比较多,并且功能都比较完善。