2

I have a requirement where I need to print a complete form without showing it on screen.

What I need is to :

  1. Initialize the form
  2. Print it

All without showing on the screen.

Any suggestions please?

Stewbob
  • 16,759
  • 9
  • 63
  • 107
DS2020
  • 279
  • 1
  • 4
  • 20

1 Answers1

3

You can do this using PrintDialog.PrintVisual method.

var capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);

//get scale of the print wrt to screen of WPF visual
var scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);

//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);

// Get the size of the printer page
var sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(visual, String.Empty);
Maksim Gladkov
  • 3,051
  • 1
  • 14
  • 16
  • I see this has two votes, but does it really answer the question? It looks like the visual (this) still has to be shown on the screen before it can print. – MojoFilter Oct 30 '13 at 12:39