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>

    public class DemoForm : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.TextBox txtDoc;
        
private System.Windows.Forms.MainMenu mnuMain;
        
private System.Windows.Forms.MenuItem mnuFile;
        
private System.Windows.Forms.MenuItem mnuFileOpen;
        
private System.Windows.Forms.MenuItem mnuFilePrint;
        
private System.Windows.Forms.MenuItem mnuFormat;
        
private System.Windows.Forms.MenuItem mnuFormatFont;
        
private System.Windows.Forms.OpenFileDialog dlgOpen;
        
private System.Windows.Forms.FontDialog dlgFont;
        
private System.Drawing.Printing.PrintDocument pdoc;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private System.Windows.Forms.MenuItem mnuFilePrintPreview;
        
private System.Windows.Forms.MenuItem mnuFilePageSetup;
        
private System.Windows.Forms.PrintPreviewDialog ppd;
        
private System.Windows.Forms.PageSetupDialog dlgPageSetup;
        
private System.Windows.Forms.PrintDialog dlgPrinterSetup;
        
private int totalLines;

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

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


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

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

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

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new DemoForm());
        }


        
private void mnuFileOpen_Click(object sender, System.EventArgs e)
        
{
            
if (dlgOpen.ShowDialog()==DialogResult.OK)
            
{
                StreamReader sr
=null;
                
try
                
{
                    sr
=new StreamReader(dlgOpen.FileName,Encoding.Default,true);
                    txtDoc.Text
=sr.ReadToEnd();
                }

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

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

            }

        }


        
private void mnuFormatFont_Click(object sender, System.EventArgs e)
        
{
            dlgFont.Font
=txtDoc.Font;
            
if (dlgFont.ShowDialog()==DialogResult.OK)
            
{
                txtDoc.Font
=dlgFont.Font;
            }

        }


        
private void pdoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        
{
            
//MessageBox.Show("开始打印啦");
        }


        
private void pdoc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        
{
            
//MessageBox.Show("打印结束");
        }


        
private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        
{
            Graphics g
=e.Graphics;
            
float lineHeight=txtDoc.Font.GetHeight(g);
            
int linesPerPage=(int)(e.MarginBounds.Height/lineHeight);
            
int count=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;
            }

        }


        
private void mnuFilePrint_Click(object sender, System.EventArgs e)
        
{
            
if (dlgPrinterSetup.ShowDialog()==DialogResult.OK)
            
{
                pdoc.Print();  
//开始执行打印
            }

        }


        
private void txtDoc_TextChanged(object sender, System.EventArgs e)
        
{
        
        }


        
private void mnuFilePrintPreview_Click(object sender, System.EventArgs e)
        
{
            ppd.ShowDialog();
        }


        
private void mnuFilePageSetup_Click(object sender, System.EventArgs e)
        
{
            Margins oldMargins
=dlgPageSetup.PageSettings.Margins;
            dlgPageSetup.PageSettings.Margins
=new Margins((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、付费专栏及课程。

余额充值