Printing a WPF Window on One Page

I can print the current Window using the following code:

 PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog().GetValueOrDefault(false)) { printDialog.PrintVisual(this, this.Title); } 

However, if Window does not match the page, it is truncated. How to make Window suitable page?
I think I need to first create a graphic element and check if this graphic matches this page, but I have not found anything so far.

+4
source share
1 answer

There is one solution that many people rearrange their own. It can be found here:

http://www.a2zdotnet.com/View.aspx?id=66

The w / problem is that it resizes the user interface. So this next link takes the previous decision and resizes to its original size when it is done. This really works, although I can't help but think that there might be a more elegant solution somewhere:

http://www.slickthought.net/post/2009/05/26/Visual-Tree-Printing-in-WPF-Applications.aspx


Slickthought.net domain is not working. Wayback Machine to the rescue.

https://web.archive.org/web/20130603071346/http://www.slickthought.net/post/2009/05/26/Visual-Tree-Printing-in-WPF-Applications.aspx


 <Button Content="Print" Command="{Binding Path=PrintCommand}" CommandParameter="{Binding ElementName=ReportPanel}"></Button> 

There are two important things here. First, I use the WPF command to start the printing process. You don’t have to do it this way, but it allows me to associate the presenter with the user interface quite carefully. Secondly, CommandParameter. It is passed to the ReportPanel link. ReportPanel is just a WPF grid control that includes a TextBlock header and a list containing actual charts. Simplified XAML:

 <Grid x:Name="ReportPanel" > <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBlock /> <ListBox/> </Grid> 

With the user interface installed, go to the code. When the user clicks the Print button, the following WPF command is executed:

 this.PrintCommand = new SimpleCommand<Grid> { CanExecuteDelegate = execute => true, ExecuteDelegate = grid => { PrintCharts(grid); } }; 

This is pretty simple stuff. SimpleCommand implements the ICommand interface and allows you to pass some lambda expressions that define the code that I want to run when this command is run. Clearly, the magic happens in a call to PrintCharts (grid). The code shown below is basically the same code that you will find in Pankaj's article with several modifications highlighted in red.

 private void PrintCharts(Grid grid) { PrintDialog print = new PrintDialog(); if (print.ShowDialog() == true) { PrintCapabilities capabilities = print.PrintQueue.GetPrintCapabilities(print.PrintTicket); double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / grid.ActualWidth, capabilities.PageImageableArea.ExtentHeight / grid.ActualHeight); Transform oldTransform = grid.LayoutTransform; grid.LayoutTransform = new ScaleTransform(scale, scale); Size oldSize = new Size(grid.ActualWidth, grid.ActualHeight); Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); grid.Measure(sz); ((UIElement)grid).Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); print.PrintVisual(grid, "Print Results"); grid.LayoutTransform = oldTransform; grid.Measure(oldSize); ((UIElement)grid).Arrange(new Rect(new Point(0, 0), oldSize)); } } 

Ok, what are these changes? The most obvious is that I am replacing the use of the original of this object (which represented the entire application window in the source code) using the Grid control that was passed as part of the command. Thus, all measurements and transformations are performed using the Grid. Another change is that I keep the original transform and grid size. The reason is that when converting the grid to a print page, this also changes the actual user interface of the application. It doesn’t look so good on your screen, so after sending the Grid to the printer, I convert it back to the original screen layout.

+5
source

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


All Articles