Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a document using PrintDialog in C#

Tags:

c#

printing

Here is my Sample code . But its printing an Empty page

            printDocument1.DocumentName = "C:\a.pbf";// PrintDocument printDocument1
            printDialog1.Document = printDocument1;
            printDialog1.AllowPrintToFile = true;
            printDialog1.AllowSelection = true;
            printDialog1.AllowSomePages = true;
            printDialog1.PrintToFile = true;
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();

Whats wrong with this?. Please help me

like image 944
Thomas Anderson Avatar asked Nov 23 '10 07:11

Thomas Anderson


People also ask

How to use PrintDialog in c# Windows application?

You can have a standard print dialog with this: var printDialog = new PrintDialog(); printDialog. ShowDialog();

Which of the following is used to create and show the Print dialogue box?

It is used to display the PrintDialog box in an application. It is an important dialog control that allows the user to select sections of a document and then select a printer to print pages from the Windows Forms application.

What is entered regarding the printing process in range and copies part of Print dialog box?

The Print dialog box lets the user select options for a particular print job. For example, the user can specify the printer to use, the range of pages to print, and the number of copies.


2 Answers

You need to handle the PrintPage event to actually provide the contents; MSDN has a full example. The DocumentName is purely something to show to the user - it is not the path of an existing file to magically print.

For printing an existing PDF, maybe look at this question

like image 149
Marc Gravell Avatar answered Sep 28 '22 10:09

Marc Gravell


do this :

public static void PrintToASpecificPrinter()
        {     
                using (PrintDialog printDialog=new PrintDialog ())
                {
                printDialog.AllowSomePages = true;
                printDialog.AllowSelection = true;
                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    var StartInfo = new ProcessStartInfo();
                    StartInfo.CreateNoWindow = true;
                    StartInfo.UseShellExecute = true;
                    StartInfo.Verb = "printTo";
                    StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
                    StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    StartInfo.FileName = fileName;

                    Process.Start(StartInfo);
                }
                    
                }
                

        }
like image 45
bigtheo Avatar answered Sep 28 '22 08:09

bigtheo