So I have an Window which I want to Print. For that I created an Print Dialog. Which looks like that:
PrinterSettings settings = new PrinterSettings();
string Printer = settings.PrinterName;
System.Windows.Controls.PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
printDlg.PrintQueue = new PrintQueue(new PrintServer(), Printer);
printDlg.PrintTicket.CopyCount = 1;
printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape;
printDlg.PrintVisual(this, "Window Printing.");
But for some reason it opens instant an Dialog for save that Programm as PDF. But I want directly print it to my Printer, without that Dialog. So why does it not Print to my Printer? And how Can I get this to work.
CodePudding user response:
Are you sure you have chosen the correct printer? Because the PrintDialog
will behave like this if its printer is set to a file printer, for example to: "Microsoft Print to PDF"
. (Then, obviously, you have to provide the name of the file the printer should print to, hence the "save" dialog.)
Check what printers you have installed (or rather, what PrintQueues
you have available):
LocalPrintServer myPrintServer = new LocalPrintServer();
foreach (PrintQueue pq in myPrintServer.GetPrintQueues())
{
Console.WriteLine(pq.Name);
}
Are you sure you're setting one of those? Which one is the default in your system? Is it maybe a file printer?
Try setting a printer name to the correct, physical printer in code, and check if anything will print:
string myPrinterName = "My Printer"; // <= put an existing name here
LocalPrintServer myPrintServer = new LocalPrintServer();
PrintDialog printDlg = new PrintDialog();
PrintQueue printQueue = myPrintServer.GetPrintQueue(myPrinterName);
printDlg.PrintQueue = printQueue;
printDlg.PrintTicket.CopyCount = 1;
printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape;
printDlg.PrintVisual(this, "Window Printing.");
In the system, is the printer configured correctly?