wpf 大控件 打印 将控件转成 xps格式 并分页打印
//PayRollPrintList:要打印的 list 可换成自己要打印的类型
private List<PayRoll> _PayRollPrintList = new List<PayRoll>();
public List<PayRoll> PayRollPrintList
{
get { return _PayRollPrintList; }
set
{
if (value != null)
{
_PayRollPrintList = value;
OnPropertyChanged("PayRollPrintList");
}
}
}
//打印的方法
public void print()
{
try
{
// PayRollListModel.PayRollList 要打印的list SalaryItemListModel.Instance.SalaryItemPrUseList 要打印的list
PrintDataPage(17, PayRollListModel.PayRollList, SalaryItemListModel.Instance.SalaryItemPrUseList);
}
catch (Exception ex)
{
MessageBox.Show("打印日志出现错误:" + ex.Message, "历史事件记录");
}
} //PageSize 为每页打印的条数
public void PrintDataPage(int PageSize, PayRollList PageData, SalaryItemList SalaryItemData)
{
int iCount = PageData.Count;
if (iCount == 0)
{
MessageBox.Show(string.Format("未查询到相应数据,无法打印!"));
return;
}
int CacheIndex = 0;//记录索引
int[] size = PageBarXPS.getPageCount(PageSize, iCount);
//要打印的页数
int iPageCount = size[0];
int iPageCount1 = iPageCount;
//最后一页的条数
int PageAmount = size[1];
FixedPage[] fixedPages = new FixedPage[iPageCount]; if (PageAmount != 0)
{
iPageCount = iPageCount - 1;
PayRollPrintList.Clear();
for (int i = iPageCount * PageSize; i < iCount; i++)
{
PayRollPrintList.Add(PageData[i]);
}
fixedPages[iPageCount] = new FixedPage();
Size fsize = new Size(96 * 11, 96 * 8.5);
fixedPages[iPageCount].Width = 96 * 11;
fixedPages[iPageCount].Height = 96 * 8.5;
fixedPages[iPageCount].Measure(fsize);
StackPanel StackPanels = ClearControls(iPageCount, iPageCount);
fixedPages[iPageCount].Children.Add(StackPanels);
}
for (int i = 0; i < iPageCount; i++)
{
PayRollPrintList.Clear();
for (; CacheIndex < PageSize * (i + 1); CacheIndex++)
{
PayRollPrintList.Add(PageData[CacheIndex]);
}
Size fsize = new Size(96 * 11, 96 * 8.5); fixedPages[i] = new FixedPage(); StackPanel StackPanels = new StackPanel();
fixedPages[i].Width = 96 * 11;
fixedPages[i].Height = 96 * 8.5;
fixedPages[i].Measure(fsize);
StackPanels = CreatePrint(iPageCount, i);
fixedPages[i].Children.Add(StackPanels); } IDocumentPaginatorSources = FileHelper.SaveXPS(fixedPages, true, iPageCount, IDocumentPaginatorSources); }
public StackPanel CreatePrint(int count, int currentPageIndex)
{
int currentPage = currentPageIndex + 1;
Width = 60 * 16 / (SalaryItemListModel.SalaryItemPrUseList.Count(L => L.IsPrint == 1));
string xaml =
"<StackPanel x:Name='grid'" + " " +
"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" + " " +
"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'" + " " +
"xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'" + " " +
"xmlns:d='http://schemas.microsoft.com/expression/blend/2008'" + " " +
"xmlns:cmd='clr-namespace:Ship.Client.WPF.UI.Commands'" + " " +
"xmlns:vm='clr-namespace:Ship.Client.WPF.UI.ViewModels'" + " " +
"xmlns:be='clr-namespace:Ship.Client.WPF.Lib.Behaviors;assembly=Ship.Client.WPF.Lib'>" + " ";
xaml += "<Grid Margin='40,45,40,20'> <Grid.RowDefinitions> <RowDefinition Height='*' /> <RowDefinition Height='Auto' /> </Grid.RowDefinitions>" + " "; xaml += "<Border BorderBrush='Black' BorderThickness='1,1,0,0' >" + " ";
xaml += "<StackPanel VerticalAlignment='Center' HorizontalAlignment='Center'>" + " "; if (PayRollPrintList != null && SalaryItemListModel.Instance.SalaryItemPrUseList != null)
{
Type type = typeof(PayRoll);
for (int i = 0; i < PayRollPrintList.Count; i++)
{
int index = 0;
xaml += "<StackPanel Orientation='Horizontal' VerticalAlignment='Center' HorizontalAlignment='Center'>" + " ";
foreach (var item in SalaryItemListModel.Instance.SalaryItemPrUseList)
{
xaml += "<StackPanel Orientation='Horizontal' VerticalAlignment='Center' HorizontalAlignment='Center'>" + " ";
if (item.IsPrint == 1)
{
index++;
string pay = type.GetProperty(item.Code).GetValue(PayRollPrintList[i], null).ToString();
xaml += "<StackPanel VerticalAlignment='Center' HorizontalAlignment='Center'>" + " ";
xaml += "<Border BorderBrush='Black' BorderThickness='0,0,1,1'>" + " ";
xaml += "<TextBlock Text='" + item.Name + "' Width='" + Width + "' Height='20'/>" + " ";
xaml += "</Border>" + " ";
xaml += "<Border BorderBrush='Black' BorderThickness='0,0,1,1'>" + " ";
xaml += "<TextBlock Text='" + pay + "' Width='" + Width + "' Height='20'/>" + " ";
xaml += "</Border>" + " ";
xaml += "</StackPanel>" + " "; } xaml += "</StackPanel>" + " ";
} xaml += " </StackPanel>" + " "; }
} xaml += "</StackPanel></Border>"; xaml += "<StackPanel Margin='0,5,0,0' Height='20' Grid.Row='1' Orientation='Horizontal' VerticalAlignment='Center' HorizontalAlignment='Center'>" + " "; xaml += "<TextBlock Text='第'/>" + " ";
xaml += "<TextBlock Text='" + currentPage + "'/>" + " ";
xaml += "<TextBlock Text='页'/>" + " ";
xaml += "</StackPanel></Grid> </StackPanel> " + " ";
var stringReader = new StringReader(xaml);
var xmlReader = XmlReader.Create(stringReader);
var tree = (FrameworkElement)XamlReader.Load(xmlReader);
//tree.DataContext = this;
StackPanels[currentPageIndex] = new StackPanel();
StackPanels[currentPageIndex].Children.Add(tree);
return StackPanels[currentPageIndex];
}
public class PageBarXPS
{
/// <summary>
/// 创建FixedPage的PageBar
/// </summary>
/// <param name="iSize">总页数</param>
/// <param name="iCount">每页记录数</param>
/// <param name="curPageNo">当前页号</param>
/// <returns></returns>
public static string createPageBar(int iSize, int iCount, int curPageNo)
{
string strPageBar = string.Empty;
int Size = iSize;
int Count = iCount;
int iPageCount = 0;
int iLastPageCount = 0;
int iCurPageNo = curPageNo; if ((iCount / iSize) == 0)
{
iPageCount = 1;
}
else
{
iPageCount = iCount / iSize;
iLastPageCount = iCount - iPageCount * iSize;
if (iLastPageCount == 0)
{
iPageCount = iCount / iSize;
}
else
{
iPageCount = iCount / iSize + 1;
}
} strPageBar = "第" + iCurPageNo + "页//" + "共" + iPageCount + "页"; return strPageBar;
} /// <summary>
/// 获得页数即需要创建的fixedPage数,A[0]为页数,A[1]为最后一页记录数
/// </summary>
/// <param name="iSize"></param>
/// <param name="iCount"></param>
/// <returns></returns>
public static int[] getPageCount(int iSize, int iCount)
{
int Size = iSize;
int Count = iCount;
int iPageCount = 0;
int iLastPageCount = 0;
int[] result = new int[2];
if ((iCount / iSize) == 0)
{
iPageCount = 1;
iLastPageCount = iCount;
}
else
{
iPageCount = iCount / iSize;
iLastPageCount = iCount - iPageCount * iSize;
if (iLastPageCount == 0)
{
iPageCount = iCount / iSize;
iLastPageCount = iCount;
}
else
{
iPageCount = iCount / iSize + 1;
}
}
result[0] = iPageCount;
result[1] = iLastPageCount;
return result;
}
} public class FileHelper
{ private string GetXPSFromDialog()
{ return string.Format("d:\\temp.xps");//制造一个临时存储
} public void PrintDocument(IDocumentPaginatorSource dps)
{
LocalPrintServer printServer = new LocalPrintServer();
PrintQueue queue = printServer.DefaultPrintQueue;
PrintTicket ticket = queue.DefaultPrintTicket;
ticket.PageOrientation = PageOrientation.Landscape; XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(queue);
//PrintDocumentImageableArea area=null;
//XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(ref area);
DocumentPaginator paginator = dps.DocumentPaginator;
paginator.PageSize = new Size(96*11,96*8.5);
writer.Write(paginator, ticket); } public static IDocumentPaginatorSource LoadDocumentViewer(string filename, XpsDocument _xpsDocument)
{
XpsDocument xpsPackage = _xpsDocument;
//filename = OpenXPSFileFromDialog();
XpsDocument oldXpsPackage = xpsPackage;//保存原来的XPS包
xpsPackage = new XpsDocument(filename, FileAccess.Read, CompressionOption.NotCompressed);//从文件中读取XPS文档 FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();//从XPS文档对象得到FixedDocumentSequence
IDocumentPaginatorSource s = fixedDocumentSequence as IDocumentPaginatorSource; if (oldXpsPackage != null)
oldXpsPackage.Close();
xpsPackage.Close();
return s;
} /// <summary>
/// 这个静态方法主要是显示选择对话框以提供文件的保存位置
/// 将传入的FixedPage对象数组(多页)写入到.xps文件
/// </summary>
/// <param name="page"></param>
/// <param name="page"></param>
/// <param name="isSaved"></param>
/// <returns></returns>
public static IDocumentPaginatorSource SaveXPS(FixedPage[] page, bool isSaved, int iPageCount, IDocumentPaginatorSource s)
{
FixedDocument fixedDoc = new FixedDocument();//创建一个文档 fixedDoc.DocumentPaginator.PageSize = new Size(96 * 11, 96 * 8.5); PageContent[] pageContent = new PageContent[iPageCount];
for (int i = 0; i < iPageCount; i++)
{
pageContent[i] = new PageContent();
Size size = new Size(96 * 11, 96 * 8.5);
pageContent[i].Measure(size);
((IAddChild)pageContent[i]).AddChild(page[i]);
fixedDoc.Pages.Add(pageContent[i]);//将对象加入到当前文档中
} FileHelper fh = new FileHelper();
string containerName = fh.GetXPSFromDialog();
if (containerName != null)
{
try
{
File.Delete(containerName);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
XpsDocument _xpsDocument = new XpsDocument(containerName, FileAccess.Write); LocalPrintServer printServer = new LocalPrintServer();
PrintQueue queue = printServer.DefaultPrintQueue;
PrintTicket ticket = queue.DefaultPrintTicket;
ticket.PageOrientation = PageOrientation.Landscape;
XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(_xpsDocument); xpsdw.Write(fixedDoc,ticket);//写入XPS文件
_xpsDocument.Close();
IDocumentPaginatorSource ss = LoadDocumentViewer(containerName,_xpsDocument); return ss; }
}
wpf 大控件 打印 将控件转成 xps格式 并分页打印的更多相关文章
- JS 实现分页打印
在调用window.print()时,可以实现打印效果,但内容太多时要进行分页打印. 在样式中有规定几个打印的样式 page-break-before和page-break-after CSS属性并不 ...
- WPF 打印界面(控件)到A4纸
这次遇到一个需求,就是将整个界面打印在A4纸上. 需求清楚后,Bing一下关于打印,就找到一个类PrintDialog ,其中两个方法可能会用到: 特别是public void PrintVisual ...
- WPF布局之让你的控件随着窗口等比放大缩小,适应多分辨率满屏填充应用
一直以来,我们设计windows应用程序,都是将控件的尺寸定好,无论窗体大小怎么变,都不会改变,这样的设计对于一般的应用程序来说是没有问题的,但是对于一些比较特殊的应用,比如有背景图片的,需要铺面整个 ...
- WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 菜单M ...
- WPF:理解ContentControl——动态添加控件和查找控件
WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...
- Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用
一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...
- 【WPF学习】第二十章 内容控件
内容控件(content control)是更特殊的控件类型,它们可包含并显示一块内容.从技术角度看,内容控件时可以包含单个嵌套元素的控件.与布局容器不同的是,内容控件只能包含一个子元素,而布局容器主 ...
- WPF进阶技巧和实战03-控件(3-文本控件及列表控件)
系列文章链接 WPF进阶技巧和实战01-小技巧 WPF进阶技巧和实战02-布局 WPF进阶技巧和实战03-控件(1-控件及内容控件) WPF进阶技巧和实战03-控件(2-特殊容器) WPF进阶技巧和实 ...
- WPF进阶技巧和实战03-控件(4-基于范围的控件及日期控件)
系列文章链接 WPF进阶技巧和实战01-小技巧 WPF进阶技巧和实战02-布局 WPF进阶技巧和实战03-控件(1-控件及内容控件) WPF进阶技巧和实战03-控件(2-特殊容器) WPF进阶技巧和实 ...
随机推荐
- 单片机Keil软件仿真与调试技巧
一.引言 单片机软件开发过程中,软件调试遇到的各种问题常令初学者感到不知所措.实际上.各种仿真开发软件的程序调试基本方法和技巧大同小异,掌握正确的程序调试基本技巧.对于排查这些程序错误问题可以起到举一 ...
- c++ RAII 资源管理就是初始化
RAII:(Resource Acquisition Is Initialization),也就是“资源获取就是初始化”,是C++语言的一种管理资源.避免泄漏的惯用法.C++标准保证任何情况下,已构造 ...
- Linux数据备份
今天需要用到备份网站,写了个脚本,备份了mysql和redis数据. #!/bin/bash BACKDIR="/home/tan/getll_backup" if [ ! -d ...
- 使用LoadRunner对Web Services进行调用--Import Soap
利用LoadRunner对Web Services进行测试时,通常有三种可供采用的方法: 在LoadRunner的Web Services虚拟用户协议中,[Add Service Call] 在Loa ...
- Longest Consecutive Sequence——Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- spring框架DI(IOC)和AOP 原理及方案
http://www.blogjava.net/killme2008/archive/2007/04/20/112160.html http://www.oschina.net/code/snippe ...
- lightoj 1027 简单概率dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 #include<cstdio> #include<cstri ...
- UCloud EIP 你真的懂得如何使用么? - SegmentFault
UCloud EIP 你真的懂得如何使用么? - SegmentFault UCloud EIP 你真的懂得如何使用么?
- crontab使用--linux下的定时任务程序
crontab是一个linux下的定时运行程序,如果我们想让自己的程序定时执行, 可以把自己的程序交给这个程序来完成 第一步:配置crontab的脚本的默认编辑器,它的默认的编辑器不好用,我们配置vi ...
- Codeforces Round #387(div 2)
A =w= B VOV C QoQ D 题意:贝尔兰冬天很冷,那么司机要换上冬天专用轮胎才能开车.假设冬天一共有n天,有一套冬天专用轮胎,仅能使用k天,这套轮胎不管什么温度都能用,而夏天用的轮胎只能在 ...