前面https://www.cnblogs.com/yinyu5/p/18633910使用PrintVisual方法只打印了可见部分的元素,所以这里改为使用PrintDocument方法来进行打印。
需要引用System.Printing.dll
界面代码:
1 <Window x:Class="WpfApp123.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp123" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="600" Width="800"> 9 <Grid> 10 <Grid.RowDefinitions> 11 <RowDefinition></RowDefinition> 12 <RowDefinition></RowDefinition> 13 <RowDefinition Height="auto"></RowDefinition> 14 </Grid.RowDefinitions> 15 <Canvas x:Name="print_Canvas"> 16 <Ellipse Margin="10" Height="100" Width="300"> 17 <Ellipse.Fill> 18 <LinearGradientBrush> 19 <GradientStop Color="Purple" Offset="0"></GradientStop> 20 <GradientStop Color="DarkBlue" Offset="1"></GradientStop> 21 </LinearGradientBrush> 22 </Ellipse.Fill> 23 </Ellipse> 24 <Rectangle Height="80" Width="200" Margin="400 30 0 0"> 25 <Rectangle.Fill> 26 <LinearGradientBrush> 27 <GradientStop Color="Red" Offset="0"></GradientStop> 28 <GradientStop Color="Green" Offset="0.5"></GradientStop> 29 <GradientStop Color="DarkBlue" Offset="1"></GradientStop> 30 </LinearGradientBrush> 31 </Rectangle.Fill> 32 </Rectangle> 33 <TextBlock Canvas.Left="200" Canvas.Top="150" FontSize="50"> 34 ABCDEFG 35 </TextBlock> 36 </Canvas> 37 <Grid Grid.Row="1"> 38 <DataGrid Name="print_dg"> 39 </DataGrid> 40 </Grid> 41 <Grid Grid.Row="2" Margin="10"> 42 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 43 <Button Height="40" Width="200" FontSize="20" Margin="3" Click="Button_Click">打印Canvas</Button> 44 <Button Height="40" Width="200" FontSize="20" Margin="3" Click="Button1_Click">打印DataGrid</Button> 45 </StackPanel> 46 </Grid> 47 </Grid> 48 </Window>View Code
使用PrintDocument方法的后台代码:
private void PrintDocument_Method() { PrintDialog printDialog = new PrintDialog(); printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue(); printDialog.PageRangeSelection = PageRangeSelection.AllPages; if (printDialog.ShowDialog() == true) { FixedDocument fixedDoc = new FixedDocument(); Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight); PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Width = pageSize.Width; fixedPage.Height = pageSize.Height; Canvas canvas = new Canvas(); canvas.Height = print_Canvas.Height; canvas.Width = print_Canvas.Width; //复制Canvas里面的元素 foreach (UIElement element in print_Canvas.Children) { UIElement clonedElement = CloneElement(element); canvas.Children.Add(clonedElement); } fixedPage.Children.Add(canvas); ((IAddChild)pageContent).AddChild(fixedPage); fixedDoc.Pages.Add(pageContent); printDialog.PrintDocument(fixedDoc.DocumentPaginator, "打印 Canvas"); } } private UIElement CloneElement(UIElement element) { // 使用 XAML 序列化反序列化来实现 UIElement 的深度复制 string xamlString = System.Windows.Markup.XamlWriter.Save(element); return (UIElement)System.Windows.Markup.XamlReader.Parse(xamlString); }View Code
可以看到被遮挡的元素也能打印出来:
那么DataGrid中遮挡的数据该如何打印呢?特别是数据行数较多,可能需要多页打印时。
还是使用PrintDocument,这里做一个简单的分页处理。
标签:Canvas,UIElement,C#,打印,canvas,new,printDialog,WPF,PrintDialog From: https://www.cnblogs.com/yinyu5/p/18634080