Click here to Skip to main content
15,881,804 members
Please Sign up or sign in to vote.
3.40/5 (4 votes)
See more:
Hi, I'm working on Window application , where one of my requirement is to print Invoice by clicking print button, but it's giving me the print out of whole window form instead of giving only Controls in group box.
Posted
Comments
Prerak Patel 24-May-11 2:34am    
Share some code.

First off: without see the code, it is very difficult to help you much. However, if you are printing screen controls to the printer, stop it now!

Look at setting up a PrintDocument[^] instead, and handling it that way - you will get much better results! The link has a simple example.
 
Share this answer
 
You need to use the class System.Drawing.Printing.PrintDocument. The idea of using it is: you obtain an instance of the class System.Drawing.Graphics and render all the graphical primitive you need.

See the code sample here:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(v=VS.100).aspx[^], it explains everything.

Also, look at this article:
Simplified .NET Printing in C#[^].

—SA
 
Share this answer
 
Its working, try this code :)
private void button1_Click(object sender, EventArgs e)
       {
           PrintDocument doc = new PrintDocument();
           doc.PrintPage += this.Doc_PrintPage;
           PrintDialog dlgSettings = new PrintDialog();
           dlgSettings.Document = doc;
           if (dlgSettings.ShowDialog() == DialogResult.OK)
           {
               doc.Print();
           }
       }

private void Doc_PrintPage(object sender, PrintPageEventArgs e)
        {
             float x = e.MarginBounds.Left;
              float y = e.MarginBounds.Top;
            Bitmap bmp = new Bitmap(this.groupBox1.Width, this.groupBox1.Height);
            this.groupBox1.DrawToBitmap(bmp, new Rectangle(0, 0, this.groupBox1.Width, this.groupBox1.Height));
            e.Graphics.DrawImage((Image)bmp, x, y);
        }
 
Share this answer
 
Comments
Corvus Corax 20-Jul-11 16:08pm    
This sends a screen shot of the control to the printer, and is not the way to print an invoice. You want to draw the data contained in your invoice to the PrintDocument, laid out and rendered to fit the page. Follow SAKryukov's advice.
shaikh-adil 19-Nov-12 0:23am    
+5
ambarishtv 19-Nov-12 0:49am    
thanks shaikh-adil
shaikh-adil 19-Nov-12 0:57am    
your welcome sir
Niranjan dotnet 7-Sep-15 7:32am    
Thanks Very Much!!!!!,
Just drag the Printform tool from the toolbox , it comes under visual power pack,and then in your onclick event add these simple code


{
groupbox.visible="True" // depends on our requirement
this.printForm.print();
}

And if you want to see the print preview just Right click on printform tool > set its print property to "Print to preview"
 
Share this answer
 
Comments
CHill60 11-Feb-16 20:40pm    
Please note the date of the question - 4.5 years ago. The post already has an accepted solution. It's best to avoid answering old posts unless you have something really new to add to the conversation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900