Printing from canvas control in WPF

4.5k views Asked by At

My application prints from a canvas (WPF, C#), but the application needs to have assigned a default printer. The default printer is saved as a .ini file on the PC. The concept is, staff can click the print button on my canvas, the canvas would generate a layout and then send to the printer.

So, the 2 things I need to do are:

  1. Set the default printer based upon a string within a text file
  2. Print after binding

After I click the print button, the canvas updates with the layout I want (via databinding) and the job is sent to the printer (the printer detects the job and is displayed in the printer queue), but the printer never prints the document. It's almost as if I've queued a job but never said 'start'.

My canon then gives me an error (this could be a red hearing though) which is "Another printer is using the printer. The following status is the usage status of that computer ---"

Here are the 2 main methods I'm using.

internal void AutoPrint()
{
        string printerName = GetPrinterNameFromTextFile();
        PrintDialog dialog = new PrintDialog();
        getView(); //queries database, returns model and binds

        Print(dialog, config.PrinterName);
}

internal void Print(PrintDialog dialog, string printerName)                  
{                  
        Canvas canvas = new Canvas();
        canvas = this.PrintCanvas;

        PrintQueue queue = new LocalPrintServer().GetPrintQueue(printerName);
        dialog.PrintQueue = queue;
        Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(delegate()
            {
                dialog.PrintVisual(canvas, "");
            }));
}
1

There are 1 answers

0
Dave On BEST ANSWER

OK, I asked a different question else where but provided source code with the answer: C# printing with WPF