博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView Print and Print Preview
阅读量:6816 次
发布时间:2019-06-26

本文共 9961 字,大约阅读时间需要 33 分钟。

1 sing System.Linq;   2 using System.Printing;   3 using System.Windows;   4 using System.Windows.Controls;   5 using System.Windows.Documents;   6 using System.Windows.Markup;   7 using System.Windows.Media;   8 using Telerik.Windows.Controls;   9 using Telerik.Windows.Controls.GridView;  10   11 namespace YourNamespaceHere  12 {  13     ///   14     /// Support Printing Related Methods  15     ///   16     public static class PrintExtensions  17     {  18         #region ___________ Properties ____________________________________________________________________________________________  19         ///   20         /// Zoom Enumeration to specify how pages are stretched in print and preview  21         ///   22         public enum ZoomType  23         {  24             ///   25             /// 100% of normal size  26             ///   27             Full,  28   29             ///   30             /// Page Width (fit so one page stretches to full width)  31             ///   32             Width,  33   34             ///   35             /// Page Height (fit so one page stretches to full height)  36             ///   37             Height,  38   39             ///   40             /// Display two columsn of pages  41             ///   42             TwoWide  43         };         44         #endregion  45         #region ___________ Methods _______________________________________________________________________________________________  46         ///   47         /// Print element to a document  48         ///   49         /// GUI Element to Print  50         /// Reference to Print Dialog  51         /// Page Orientation (i.e. Portrait vs. Landscape)  52         /// 
Destination document
53 static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog, PageOrientation orientation = PageOrientation.Portrait) 54 { 55 dialog.PrintTicket.PageOrientation = orientation; 56 PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket); 57 Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight); 58 Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); 59 60 FixedDocument fixedDocument = new FixedDocument(); 61 62 element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 63 element.Arrange(new Rect(new Point(0, 0), element.DesiredSize)); 64 65 for (double y = 0; y < element.DesiredSize.Height; y += extentSize.Height) 66 { 67 for (double x = 0; x < element.DesiredSize.Width; x += extentSize.Width) 68 { 69 VisualBrush brush = new VisualBrush(element); 70 brush.Stretch = Stretch.None; 71 brush.AlignmentX = AlignmentX.Left; 72 brush.AlignmentY = AlignmentY.Top; 73 brush.ViewboxUnits = BrushMappingMode.Absolute; 74 brush.TileMode = TileMode.None; 75 brush.Viewbox = new Rect(x, y, extentSize.Width, extentSize.Height); 76 77 PageContent pageContent = new PageContent(); 78 FixedPage page = new FixedPage(); 79 ((IAddChild)pageContent).AddChild(page); 80 81 fixedDocument.Pages.Add(pageContent); 82 page.Width = pageSize.Width; 83 page.Height = pageSize.Height; 84 85 Canvas canvas = new Canvas(); 86 FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth); 87 FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight); 88 canvas.Width = extentSize.Width; 89 canvas.Height = extentSize.Height; 90 canvas.Background = brush; 91 92 page.Children.Add(canvas); 93 } 94 } 95 return fixedDocument; 96 } 97 98 /// 99 /// Convert GridView to Printer-Friendly version of a GridView 100 /// 101 /// Input GridView 102 ///
Printer-Friendly version of source
103 static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source) 104 { 105 RadGridView grid = new RadGridView(); 106 107 grid.ItemsSource = source.ItemsSource; 108 grid.RowIndicatorVisibility = Visibility.Collapsed; 109 grid.ShowGroupPanel = false; 110 grid.CanUserFreezeColumns = false; 111 grid.IsFilteringAllowed = false; 112 grid.AutoExpandGroups = true; 113 grid.AutoGenerateColumns = false; 114 115 foreach (GridViewDataColumn column in source.Columns.OfType
()) 116 { 117 GridViewDataColumn newColumn = new GridViewDataColumn(); 118 newColumn.Width = column.ActualWidth; 119 newColumn.DisplayIndex = column.DisplayIndex; 120 //newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName); 121 newColumn.DataMemberBinding = column.DataMemberBinding; // Better to just copy the references to get all the custom formatting 122 newColumn.DataFormatString = column.DataFormatString; 123 newColumn.TextAlignment = column.TextAlignment; 124 newColumn.Header = column.Header; 125 newColumn.Footer = column.Footer; 126 grid.Columns.Add(newColumn); 127 } 128 129 StyleManager.SetTheme(grid, StyleManager.GetTheme(grid)); 130 131 grid.SortDescriptors.AddRange(source.SortDescriptors); 132 grid.GroupDescriptors.AddRange(source.GroupDescriptors); 133 grid.FilterDescriptors.AddRange(source.FilterDescriptors); 134 135 return grid; 136 } 137 138 ///
139 /// Perform a Print Preview on GridView source 140 /// 141 ///
Input GridView 142 ///
Page Orientation (i.e. Portrait vs. Landscape) 143 ///
Zoom Enumeration to specify how pages are stretched in print and preview 144 public static void PrintPreview(GridViewDataControl source, PageOrientation orientation = PageOrientation.Landscape, ZoomType zoom = ZoomType.TwoWide) 145 { 146 Window window = new Window(); 147 window.Title = "Print Preview"; 148 if (!string.IsNullOrWhiteSpace(source.ToolTip as string)) window.Title += " of " + source.ToolTip; 149 window.Width = SystemParameters.PrimaryScreenWidth * 0.92; 150 window.Height = SystemParameters.WorkArea.Height; 151 window.Left = constrain(SystemParameters.VirtualScreenWidth - SystemParameters.PrimaryScreenWidth, 0, SystemParameters.VirtualScreenWidth - 11); 152 window.Top = constrain(0, 0, SystemParameters.VirtualScreenHeight - 25); 153 154 DocumentViewer viewer = new DocumentViewer(); 155 viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), new PrintDialog(), orientation); 156 Zoom(viewer, zoom); 157 window.Content = viewer; 158 159 window.ShowDialog(); 160 } 161 162 ///
163 /// Constrain val to the range [val_min, val_max] 164 /// 165 ///
Value to be constrained 166 ///
Minimum that will be returned if val is less than val_min 167 ///
Maximum that will be returned if val is greater than val_max 168 ///
val in [val_min, val_max]
169 private static double constrain(double val, double val_min, double val_max) 170 { 171 if (val < val_min) return val_min; 172 else if (val > val_max) return val_max; 173 else return val; 174 } 175 176 ///
177 /// Perform a Print on GridView source 178 /// 179 ///
Input GridView 180 ///
True to show print dialog before printing 181 ///
Page Orientation (i.e. Portrait vs. Landscape) 182 ///
Zoom Enumeration to specify how pages are stretched in print and preview 183 public static void Print(GridViewDataControl source, bool showDialog = true, PageOrientation orientation = PageOrientation.Landscape, ZoomType zoom = ZoomType.TwoWide) 184 { 185 PrintDialog dialog = new PrintDialog(); 186 bool? dialogResult = showDialog ? dialog.ShowDialog() : true; 187 188 if (dialogResult == true) 189 { 190 DocumentViewer viewer = new DocumentViewer(); 191 viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), dialog, orientation); 192 Zoom(viewer, zoom); 193 dialog.PrintDocument(viewer.Document.DocumentPaginator, ""); 194 } 195 } 196 197 ///
198 /// Scale viewer to size specified by zoom 199 /// 200 ///
Document to zoom 201 ///
Zoom Enumeration to specify how pages are stretched in print and preview 202 public static void Zoom(DocumentViewer viewer, ZoomType zoom) 203 { 204 switch (zoom) 205 { 206 case ZoomType.Height: viewer.FitToHeight(); break; 207 case ZoomType.Width: viewer.FitToWidth(); break; 208 case ZoomType.TwoWide: viewer.FitToMaxPagesAcross(2); break; 209 case ZoomType.Full: break; 210 } 211 } 212 #endregion 213 }

 

转载地址:http://enbzl.baihongyu.com/

你可能感兴趣的文章
《软件工程-理论、方法与实践》读书笔记一
查看>>
POJ Problem Radar Installation 【贪心】
查看>>
redis 持久化方式
查看>>
Vue2.0设置反向代理解决跨域问题
查看>>
伪类link,hover,active,visited,focus的区别
查看>>
WTL 实践笔记
查看>>
漫谈C++:良好的编程习惯与编程要点(转载)
查看>>
Jquery plugin ScrollUp使用和实现
查看>>
使用HTML5 FormData对象实现大文件分块上传(断点上传)功能
查看>>
在 xilinx SDK 使用 math.h
查看>>
项目中自定义返回任意数据或者消息
查看>>
IOS设计模式的六大设计原则之单一职责原则(SRP,Single Responsibility Principle)
查看>>
How to run ASP file on VS 2010
查看>>
Manacher算法
查看>>
Linux 的cp命令
查看>>
JavaScript类型转换
查看>>
OnClientClick="return confirm('确定要删除吗?')"
查看>>
Android 中间白色渐变到看不见的线的Drawable
查看>>
Oracle创建用户、表空间并设置权限
查看>>
10.5 集合ArrayList 和 io流
查看>>