GridView Print and Print Preview
sing System.Linq;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView; namespace YourNamespaceHere
{
/// <summary>
/// Support Printing Related Methods
/// </summary>
public static class PrintExtensions
{
#region ___________ Properties ____________________________________________________________________________________________
/// <summary>
/// Zoom Enumeration to specify how pages are stretched in print and preview
/// </summary>
public enum ZoomType
{
/// <summary>
/// 100% of normal size
/// </summary>
Full, /// <summary>
/// Page Width (fit so one page stretches to full width)
/// </summary>
Width, /// <summary>
/// Page Height (fit so one page stretches to full height)
/// </summary>
Height, /// <summary>
/// Display two columsn of pages
/// </summary>
TwoWide
};
#endregion
#region ___________ Methods _______________________________________________________________________________________________
/// <summary>
/// Print element to a document
/// </summary>
/// <param name="element">GUI Element to Print</param>
/// <param name="dialog">Reference to Print Dialog</param>
/// <param name="orientation">Page Orientation (i.e. Portrait vs. Landscape)</param>
/// <returns>Destination document</returns>
static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog, PageOrientation orientation = PageOrientation.Portrait)
{
dialog.PrintTicket.PageOrientation = orientation;
PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); FixedDocument fixedDocument = new FixedDocument(); element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(new Point(, ), element.DesiredSize)); for (double y = ; y < element.DesiredSize.Height; y += extentSize.Height)
{
for (double x = ; x < element.DesiredSize.Width; x += extentSize.Width)
{
VisualBrush brush = new VisualBrush(element);
brush.Stretch = Stretch.None;
brush.AlignmentX = AlignmentX.Left;
brush.AlignmentY = AlignmentY.Top;
brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.TileMode = TileMode.None;
brush.Viewbox = new Rect(x, y, extentSize.Width, extentSize.Height); PageContent pageContent = new PageContent();
FixedPage page = new FixedPage();
((IAddChild)pageContent).AddChild(page); fixedDocument.Pages.Add(pageContent);
page.Width = pageSize.Width;
page.Height = pageSize.Height; Canvas canvas = new Canvas();
FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
canvas.Width = extentSize.Width;
canvas.Height = extentSize.Height;
canvas.Background = brush; page.Children.Add(canvas);
}
}
return fixedDocument;
} /// <summary>
/// Convert GridView to Printer-Friendly version of a GridView
/// </summary>
/// <param name="source">Input GridView</param>
/// <returns>Printer-Friendly version of source</returns>
static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source)
{
RadGridView grid = new RadGridView(); grid.ItemsSource = source.ItemsSource;
grid.RowIndicatorVisibility = Visibility.Collapsed;
grid.ShowGroupPanel = false;
grid.CanUserFreezeColumns = false;
grid.IsFilteringAllowed = false;
grid.AutoExpandGroups = true;
grid.AutoGenerateColumns = false; foreach (GridViewDataColumn column in source.Columns.OfType<GridViewDataColumn>())
{
GridViewDataColumn newColumn = new GridViewDataColumn();
newColumn.Width = column.ActualWidth;
newColumn.DisplayIndex = column.DisplayIndex;
//newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName);
newColumn.DataMemberBinding = column.DataMemberBinding; // Better to just copy the references to get all the custom formatting
newColumn.DataFormatString = column.DataFormatString;
newColumn.TextAlignment = column.TextAlignment;
newColumn.Header = column.Header;
newColumn.Footer = column.Footer;
grid.Columns.Add(newColumn);
} StyleManager.SetTheme(grid, StyleManager.GetTheme(grid)); grid.SortDescriptors.AddRange(source.SortDescriptors);
grid.GroupDescriptors.AddRange(source.GroupDescriptors);
grid.FilterDescriptors.AddRange(source.FilterDescriptors); return grid;
} /// <summary>
/// Perform a Print Preview on GridView source
/// </summary>
/// <param name="source">Input GridView</param>
/// <param name="orientation">Page Orientation (i.e. Portrait vs. Landscape)</param>
/// <param name="zoom">Zoom Enumeration to specify how pages are stretched in print and preview</param>
public static void PrintPreview(GridViewDataControl source, PageOrientation orientation = PageOrientation.Landscape, ZoomType zoom = ZoomType.TwoWide)
{
Window window = new Window();
window.Title = "Print Preview";
if (!string.IsNullOrWhiteSpace(source.ToolTip as string)) window.Title += " of " + source.ToolTip;
window.Width = SystemParameters.PrimaryScreenWidth * 0.92;
window.Height = SystemParameters.WorkArea.Height;
window.Left = constrain(SystemParameters.VirtualScreenWidth - SystemParameters.PrimaryScreenWidth, , SystemParameters.VirtualScreenWidth - );
window.Top = constrain(, , SystemParameters.VirtualScreenHeight - ); DocumentViewer viewer = new DocumentViewer();
viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), new PrintDialog(), orientation);
Zoom(viewer, zoom);
window.Content = viewer; window.ShowDialog();
} /// <summary>
/// Constrain val to the range [val_min, val_max]
/// </summary>
/// <param name="val">Value to be constrained</param>
/// <param name="val_min">Minimum that will be returned if val is less than val_min</param>
/// <param name="val_max">Maximum that will be returned if val is greater than val_max</param>
/// <returns>val in [val_min, val_max]</returns>
private static double constrain(double val, double val_min, double val_max)
{
if (val < val_min) return val_min;
else if (val > val_max) return val_max;
else return val;
} /// <summary>
/// Perform a Print on GridView source
/// </summary>
/// <param name="source">Input GridView</param>
/// <param name="showDialog">True to show print dialog before printing</param>
/// <param name="orientation">Page Orientation (i.e. Portrait vs. Landscape)</param>
/// <param name="zoom">Zoom Enumeration to specify how pages are stretched in print and preview</param>
public static void Print(GridViewDataControl source, bool showDialog = true, PageOrientation orientation = PageOrientation.Landscape, ZoomType zoom = ZoomType.TwoWide)
{
PrintDialog dialog = new PrintDialog();
bool? dialogResult = showDialog ? dialog.ShowDialog() : true; if (dialogResult == true)
{
DocumentViewer viewer = new DocumentViewer();
viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), dialog, orientation);
Zoom(viewer, zoom);
dialog.PrintDocument(viewer.Document.DocumentPaginator, "");
}
} /// <summary>
/// Scale viewer to size specified by zoom
/// </summary>
/// <param name="viewer">Document to zoom</param>
/// <param name="zoom">Zoom Enumeration to specify how pages are stretched in print and preview</param>
public static void Zoom(DocumentViewer viewer, ZoomType zoom)
{
switch (zoom)
{
case ZoomType.Height: viewer.FitToHeight(); break;
case ZoomType.Width: viewer.FitToWidth(); break;
case ZoomType.TwoWide: viewer.FitToMaxPagesAcross(); break;
case ZoomType.Full: break;
}
}
#endregion
}
GridView Print and Print Preview的更多相关文章
- 为什么 echo 3 . print(2) . print(4) . 5 . 'c'的结果是45c2131
例子:请写出echo 3 . print(2) . print(4) . 5 . 'c'的输出结果为____? 许多人看到这个题的第一印象是输出结果不就是3245c嘛,然而正确的是答案却是45c213 ...
- Web window.print() 打印
web打印 window.print() 我只给出比较有效的,方便的打印方法,有些WEB打印是调用ActiveX控件的,这样就需要用户去修改自己IE浏览器的Internet选项里的安全里的Active ...
- python print及格式化
print(value,sep=' ',end='\n',file=sys.stdout, flush=False) sep=' '默认空格 print('hello','world') #hello ...
- echo print() print_r() var_dump()的区别
常用调试方法 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函数 ...
- 为什么print在python3中变成了函数?
转自:http://www.codingpy.com/article/why-print-became-a-function-in-python-3/ 在Python 2中,print是一个语句(st ...
- JAVA 之print,printf,println
print:将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后. println: 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始. printf:是格式 ...
- PHP之echo/print
1.PHP中有两个基本的输出方式:echo和print: 2.echo和print的区别: **echo:可以输出一个或多个字符串: **print:只允许输出一个字符串,返回值总为1: 3.echo ...
- echo(),print(),print_r(),var_dump的区别?
常见的输出语句 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函 ...
- echo、print、sprint、sprintf输出
echo() 函数 定义和用法 echo() 函数输出一个或多个字符串. 语法 echo(strings) 参数 描述 strings 必需.一个或多个要发送到输出的字符串. 提示和注释 注释:ech ...
随机推荐
- atitit.html编辑器的设计要点与框架选型 attilax总结
atitit.html编辑器的设计要点与框架选型 attilax总结 1. html编辑器的设计要求1 1.1. 障碍訪问 1 1.2. 强大Ajax上传 1 1.3. Word完美支持 2 1.4. ...
- eclipse下SVN subclipse插件
本文目的 让未使用过版本控制器软件或者未使用过subversion软件的人员尽快上手. subversion的使用技巧很多,这里只总结了最小使用集,即主要的基本功能,能够用来应付日常工作. 因此不涉及 ...
- iOS 画平滑曲线的方法及取音频数据的方法
源码:http://files.cnblogs.com/ios8/iOS%E5%BF%83%E7%94%B5%E5%9B%BEDemo.zip 取音频数据和画波形图的方法 ViewController ...
- 【iOS XMPP】使用XMPPFramewok(一):添加XMPPFramework(XCode 4.6.2)
转自:http://www.cnblogs.com/dyingbleed/archive/2013/05/09/3069145.html XMPPFramework GitHub: https://g ...
- HTTP模块SuperAgent
superagent它是一个强大并且可读性很好的轻量级ajaxAPI,是一个关于HTTP方面的一个库,而且它可以将链式写法玩的出神入化. var superagent = require('super ...
- pandas数组获取最大值索引的方法-argmax和idxmax
pandas Series 的 argmax 方法和 idxmax 方法用于获取 Series 的最大值的索引值: 举个栗子: 有一个pandas Series,它的索引是国家名,数据是就业率,要找出 ...
- git无法提交,存在未提交的修改,在重新合并前或者撤销更改
其实我没有修改.但是却无法同步. 解决方法: 1.在vs里, 打开git的命令提示符 2.输入一下命令: git stashgit stash pop 3.然后再git checkout试试,然后提示 ...
- vim学习笔记(10):vim命令大全
进入vim的命令: vim filename :打开或新建文件,并将光标置于第一行首 vim +n filename :打开文件,并将光标置于第n行首 vim + filename :打开文件,并将光 ...
- 使用CountDownTimer实现倒计时功能
// 倒计时60s new CountDownTimer(60000, 1000) { @Override public void onTick(long millisUntilFinished) { ...
- pyspark 编写 UDF函数
pyspark 编写 UDF函数 前言 以前用的是Scala,最近有个东西要用Python,就查了一下如何编写pyspark的UDF. pyspark udf 也是先定义一个函数,例如: def ge ...