打印文档 [英] Printing a Document

查看:40
本文介绍了打印文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印我的 TextBox,这是我的代码:

I want to print my TextBox and here is my code:

private void MenuItemPrint()
{
        if (FileName != "")
        {
            PrintDocument document = new PrintDocument();
            document.PrinterSettings.PrintFileName = FileName;
            document.Print();
        }
}

它不起作用.我该怎么办?

and it doesn't work. What should I do?

推荐答案

试试这个:

private void MenuItemPrint()
{
   if (!FileName.Trim().Equals(""))
   {                        
     using(PrintDocument pd = new PrintDocument())
     {
        using(PrintDialog printDialog=new PrintDialog())
        {
          if(printDialog.ShowDialog()==DialogResult.Yes)
          {
          pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);                        
          pd.Print();
          }
         }
      }
    }
 }
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
  ev.Graphics.DrawString(FileName, new Font("Arial", 10), Brushes.Black,
                       ev.MarginBounds.Left, 0, new StringFormat());
 }

这篇关于打印文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆