Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a Document

Tags:

c#

printing

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?

like image 980
Melody Hajian Avatar asked Dec 20 '22 21:12

Melody Hajian


1 Answers

Try This:

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());
 }
like image 102
Sudhakar Tillapudi Avatar answered Dec 22 '22 11:12

Sudhakar Tillapudi