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的更多相关文章

  1. 为什么 echo 3 . print(2) . print(4) . 5 . 'c'的结果是45c2131

    例子:请写出echo 3 . print(2) . print(4) . 5 . 'c'的输出结果为____? 许多人看到这个题的第一印象是输出结果不就是3245c嘛,然而正确的是答案却是45c213 ...

  2. Web window.print() 打印

    web打印 window.print() 我只给出比较有效的,方便的打印方法,有些WEB打印是调用ActiveX控件的,这样就需要用户去修改自己IE浏览器的Internet选项里的安全里的Active ...

  3. python print及格式化

    print(value,sep=' ',end='\n',file=sys.stdout, flush=False) sep=' '默认空格 print('hello','world') #hello ...

  4. echo print() print_r() var_dump()的区别

    常用调试方法 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函数 ...

  5. 为什么print在python3中变成了函数?

    转自:http://www.codingpy.com/article/why-print-became-a-function-in-python-3/ 在Python 2中,print是一个语句(st ...

  6. JAVA 之print,printf,println

    print:将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后. println: 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始. printf:是格式 ...

  7. PHP之echo/print

    1.PHP中有两个基本的输出方式:echo和print: 2.echo和print的区别: **echo:可以输出一个或多个字符串: **print:只允许输出一个字符串,返回值总为1: 3.echo ...

  8. echo(),print(),print_r(),var_dump的区别?

    常见的输出语句 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函 ...

  9. echo、print、sprint、sprintf输出

    echo() 函数 定义和用法 echo() 函数输出一个或多个字符串. 语法 echo(strings) 参数 描述 strings 必需.一个或多个要发送到输出的字符串. 提示和注释 注释:ech ...

随机推荐

  1. tcp拥堵算法

    http://m.blog.csdn.net/article/details?id=6739189

  2. 《深入应用C++11:代码优化与工程级应用》开始发售

    我的新书<深入应用C++11:代码优化与工程级应用>已经开始在华章微店发售了,下面是链接. 京东发售链接 china-pub发售链接 亚马逊发售链接 天猫商城发售链接 适用读者:C++11 ...

  3. c++并行计算库TBB和PPL的基本用法

    并行库充分利用多核的优势,通过并行运算提高程序效率,本文主要介绍c++中两个知名的并行库,一个是intel开发的TBB,一个是微软开发的PPL.本文只介绍其基本的常用用法:并行算法和任务. TBB(I ...

  4. HBase的Write Ahead Log (WAL) —— 整体架构、线程模型【转】

    转自:http://www.cnblogs.com/ohuang/p/5807543.html 解决的问题 HBase的Write Ahead Log (WAL)提供了一种高并发.持久化的日志保存与回 ...

  5. crontab格式,命令

    http://www.blogjava.net/xiaomage234/archive/2007/12/26/170490.html crontab格式: 第1列分钟1-59 第2列小时1-23(0表 ...

  6. Python(三)之Python的表达式和语句概述

    Python常用的表达式操作符: 算术运算符: x+y, x-y, x / y, x*y, x // y, x%y 比较运算符: x>y, x<y, x>=y, x<=y, x ...

  7. idea 设置字体

    1.设置 ui字体 修改编辑器的字体(也就是代码的字体):设置-Editor-Color&Font,默认的scheme是不可以更改的,你需要save as,建立一个新的(名字可以随意写个,My ...

  8. 通过Python的JIRA库操作JIRA

    [本文出自天外归云的博客园] 前提 需要安装jira库: pip install jira 编写脚本 例如,我想统计一下某一jira的备注里是否有如下信息,没有则给予提示: [产品需求文档地址]:ht ...

  9. celery 实例进阶

    认识 这里有几个概念,task.worker.broker.顾名思义,task 就是老板交给你的各种任务,worker 就是你手下干活的人员. 那什么是 Broker 呢? 老板给你下发任务时,你需要 ...

  10. C#学习笔记(10)——再谈委托,不同国家问候的例子

    说明(2017-6-14 09:08:38): 1. 首先是一个简单的委托 (1)命名空间下定义一个委托MyDel: public delegate void MyDel(string name); ...