Как распечатать окно wpf, не показывая его на экране? - PullRequest
2 голосов
/ 17 января 2012

У меня есть требование, когда мне нужно распечатать полную форму, не показывая ее на экране.

Мне нужно:

  1. Инициализировать форму
  2. Распечатать

Все без отображения на экране.

Любые предложения, пожалуйста?

1 Ответ

3 голосов
/ 17 января 2012

Вы можете сделать это, используя метод PrintDialog.PrintVisual.

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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...