c#打印文本文件简单实例

实例代码如下:

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;

namespace PrintDemo
... {
/**////<summary>
///Form1的摘要说明。
///</summary>

publicclassDemoForm:System.Windows.Forms.Form
...{
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;
/**////<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;

publicDemoForm()
...{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();

//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}


/**////<summary>
///清理所有正在使用的资源。
///</summary>

protectedoverridevoidDispose(booldisposing)
...{
if(disposing)
...{
if(components!=null)
...{
components.Dispose();
}

}

base.Dispose(disposing);
}


Windows窗体设计器生成的代码#regionWindows窗体设计器生成的代码
/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>

privatevoidInitializeComponent()
...{
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
//
this.mnuMain.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]...{
this.mnuFile,
this.mnuFormat}
);
//
//mnuFile
//
this.mnuFile.Index=0;
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;
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);

}

#endregion


/**////<summary>
///应用程序的主入口点。
///</summary>

[STAThread]
staticvoidMain()
...{
Application.Run(
newDemoForm());
}


privatevoidmnuFileOpen_Click(objectsender,System.EventArgse)
...{
if(dlgOpen.ShowDialog()==DialogResult.OK)
...{
StreamReadersr
=null;
try
...{
sr
=newStreamReader(dlgOpen.FileName,Encoding.Default,true);
txtDoc.Text
=sr.ReadToEnd();
}

catch
...{
MessageBox.Show(
"打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

finally
...{
if(sr!=null)sr.Close();
}

}

}


privatevoidmnuFormatFont_Click(objectsender,System.EventArgse)
...{
dlgFont.Font
=txtDoc.Font;
if(dlgFont.ShowDialog()==DialogResult.OK)
...{
txtDoc.Font
=dlgFont.Font;
}

}


privatevoidpdoc_BeginPrint(objectsender,System.Drawing.Printing.PrintEventArgse)
...{
//MessageBox.Show("开始打印啦");
}


privatevoidpdoc_EndPrint(objectsender,System.Drawing.Printing.PrintEventArgse)
...{
//MessageBox.Show("打印结束");
}


privatevoidpdoc_PrintPage(objectsender,System.Drawing.Printing.PrintPageEventArgse)
...{
Graphicsg
=e.Graphics;
floatlineHeight=txtDoc.Font.GetHeight(g);
intlinesPerPage=(int)(e.MarginBounds.Height/lineHeight);
intcount=0;//本页已打印行数

while(count<linesPerPage&&totalLines<txtDoc.Lines.Length)
...{
g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y
+lineHeight*count);
count
++;
totalLines
++;
}


if(totalLines<txtDoc.Lines.Length)
...{
e.HasMorePages
=true;
}

else
...{
e.HasMorePages
=false;
totalLines
=0;
}

}


privatevoidmnuFilePrint_Click(objectsender,System.EventArgse)
...{
if(dlgPrinterSetup.ShowDialog()==DialogResult.OK)
...{
pdoc.Print();
//开始执行打印
}

}


privatevoidtxtDoc_TextChanged(objectsender,System.EventArgse)
...{

}


privatevoidmnuFilePrintPreview_Click(objectsender,System.EventArgse)
...{
ppd.ShowDialog();
}


privatevoidmnuFilePageSetup_Click(objectsender,System.EventArgse)
...{
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)
...{
dlgPageSetup.PageSettings.Margins
=oldMargins;
}

}

}

}

这个实例存在一下缺陷,就是没有实现分行打印,不过这个问题其实比较复杂,要考虑并计算一行能打印多少个字符,尤其是在分页处还要判断上一页中分行都是否还有没有打印的文本需要有限处理,有兴趣的可以研究一下。

其实在实际开发应用中可能对文本文件打印是应用并不是太多,可以编辑、打印文本文件的工具比较多,并且功能都比较完善。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值