WPF Printing Problem

When I select Microsoft XPS Document Writer as Printer, my output is fine, but when I select my HP 1020 printer, the printer displays a blank copy ... The code below is ...

private void printButton_Click(object sender, RoutedEventArgs e) { PrintInvoice pi = new PrintInvoice(); pi.DataContext = this.DataContext; PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); if (printDlg.ShowDialog() == true) { pi.Margin = new Thickness(30); //now print the visual to printer to fit on the one page. printDlg.PrintVisual(pi, "First Fit to Page WPF Print"); } } 
+4
source share
2 answers

This can be caused by a number of different things. There are several steps you can add that, when done correctly, can cause flying people to return with goods and knowledge.

First, you have to scale to a printed page (code from a2zdotnet ):

 System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); //get scale of the print wrt to screen of WPF visual double 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 Size 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(this, "Code ganked from http://www.a2zdotnet.com/View.aspx?id=66"); 

The cult-load code is in the “Measurement” and “Arrange” steps. Often, if you call "Measure" and pass in everything new that you need to do in new Size(Double.MaxValue, Double.MaxValue) .

The second ritual involves the controller's strike.

 visual.DataContext = foo; Dispatcher.Invoke((Action)()=>{;}); // bamp // print here 

Try and see if this helps.

+7
source

this XAML does the trick in some situations

 <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> <... Name="myPrintElement" /> </ScrollViewer > 
0
source

Source: https://habr.com/ru/post/1336990/


All Articles