-2

i want to print a file in my windows application / windows forms using C#. Anyone know? i have been searching in the internet, but it comes with print a file that already been saved to the computer. What i want is, directly print when program is running and when the CTRL + P has been clicked by user.

I have a file like this when i run the program:

enter image description here

And i want it to be printed when i click CTRL + P (Without saving the file first) on the A4 Paper and it come out like this in the A4:

enter image description here

I want it like this web: http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument%28printer%29.aspx

Kaoru
  • 2,853
  • 14
  • 34
  • 68
  • 2
    Printing is very complicated operation. You would precise what you want to print. There is special class for Print dialogs in .NET. Have you googled it? If it comes on the shortcuts keys in Windows Form, look at this: http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application. – pt12lol Sep 22 '13 at 09:37
  • You want to print a file that does not exists on the hard drive? How do you represent the file in your application ? – Omri Btian Sep 22 '13 at 09:40
  • Maybe this question and its answers give some ideas http://stackoverflow.com/questions/12510811/printing-quality-winform/16885844#16885844 – rene Sep 22 '13 at 09:46
  • @Omribitan: i mean, like microsoft word, when i type something like `asd` in the paper, and when i click CTRL + P, it does not ask me to save first, it is just show the print dialog directly. I want it like that. That's what i mean by print a file directly without save first – Kaoru Sep 22 '13 at 09:46
  • what i want is like this page. But the paper is not being save in the computer, it is just click CTRL + P and it print a current file. http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument%28printer%29.aspx – Kaoru Sep 22 '13 at 09:50
  • 1
    Write code to draw on the printer. Or get a third party printing library. – David Heffernan Sep 22 '13 at 09:59

2 Answers2

4

I'll assume you already know about the PrintDocument class, given that you linked its article. And that you implemented its PrintPage event as shown in the MSDN article. Drop a PrintDialog from the toolbox onto your form. Set its Document property to your PrintDocument.

You then just need to activate the dialog when the user presses Ctrl+P. Like this:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == (Keys.Control | Keys.P)) {
            printDialog1.ShowDialog(this);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I am sorry sir, i don't know anything about the Print. The link that i mentioned in the question above (MSDN article) is just wanted to tell you guys that is the Print looks like (when i click the CTRL + P in the program), not fully even not little bit understand the Printing class. But, thanks for your suggestion, i will look to it – Kaoru Sep 23 '13 at 10:27
0

Printing from your own application required a formatted output to the .Net printing classes see PrintDocument Class

Aternatively, have a look at reporting tools, which can be used to format documents for print and export, ie outputting to the printer, Excel or PDF, where viewing and printing can be done with those applications.

Mark Redman
  • 24,079
  • 20
  • 92
  • 147