6

Here is my sample code:

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();

It runs, but it's printing an empty page. What's wrong with this?

jordanz
  • 367
  • 4
  • 12
Thomas Anderson
  • 1,977
  • 7
  • 17
  • 22
  • what is type is printDocument1 ? – abhilash Nov 23 '10 at 07:07
  • @ABKolan - I think we can assume it is a [PrintDocument](http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx) – Marc Gravell Nov 23 '10 at 07:11
  • Hey mate, I know it has been a long time since this questioned was opened, but you need to accept the answer :) - good question though! – Kevdog777 Jan 16 '12 at 09:09
  • Does this answer your question? [Print Pdf in C#](https://stackoverflow.com/questions/5566186/print-pdf-in-c-sharp) – bigtheo Jun 13 '21 at 21:04

2 Answers2

7

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

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

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);
                }
                    
                }
                

        }
bigtheo
  • 624
  • 9
  • 16