关于SL打印(二)
在本页面中有DataGrid数据绑定,可以根据数据加载实现分页打印
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
List
<
EmployeeModel
>
() employees
=
new
List
<
EmployeeModel
>
();
// 调用打印
GetPrintPage(dataPager1.PageCount, dataPager1.PageSize)
// 当前数据分页中dataPager1
// 当前数据页行数PageSize
// employees 当前填充DataGrid的集合
// PageCount总页码
// Pagesize 每一页页数
public void GetPrintPage( int PageCount, int Pagesize)
{
PrintDocument pd = new PrintDocument();
int itemindex = 0 ; // 从第一张打印
int count = Pagesize;
// 根据页码选择的数据
pd.PrintPage += (s, pe) =>
{
StackPanel stack = new StackPanel();
while (itemindex < PageCount)
{
if (itemindex == PageCount - 1 )
{
count = employees.Count - (Pagesize * itemindex);
}
List < EmployeeModel > itemsource = LoadNewList(employees, itemindex * Pagesize, count);
PageP mode = new PageP(); // 当前要打印的页面(包含数据DataGrid)
int PrintPageindex = itemindex + 1 ;
mode.MinHeight = 1058 ; // 根据A4纸显示高度
mode.dataGrid1.ItemsSource = itemsource;
stack.Children.Add(mode);
stack.Measure( new Size(pe.PrintableArea.Width, double .PositiveInfinity));
if (stack.DesiredSize.Height > pe.PrintableArea.Height | stack.Children.Count > 1 )
{
stack.Children.Remove(mode);
stack.Measure(pe.PrintableArea);
pe.HasMorePages = true ;
break ;
}
itemindex += 1 ;
}
pe.PageVisual = stack;
};
pd.Print( null );
}
// 获取指定的List集合
public static List < T > LoadNewList < T > (List < T > _source, int PIndex, int Pcounts)
{
List < T > newList = new List < T > ();
newList.AddRange(_source.GetRange(PIndex, Pcounts));
return newList;
}
// 调用打印
GetPrintPage(dataPager1.PageCount, dataPager1.PageSize)
// 当前数据分页中dataPager1
// 当前数据页行数PageSize
// employees 当前填充DataGrid的集合
// PageCount总页码
// Pagesize 每一页页数
public void GetPrintPage( int PageCount, int Pagesize)
{
PrintDocument pd = new PrintDocument();
int itemindex = 0 ; // 从第一张打印
int count = Pagesize;
// 根据页码选择的数据
pd.PrintPage += (s, pe) =>
{
StackPanel stack = new StackPanel();
while (itemindex < PageCount)
{
if (itemindex == PageCount - 1 )
{
count = employees.Count - (Pagesize * itemindex);
}
List < EmployeeModel > itemsource = LoadNewList(employees, itemindex * Pagesize, count);
PageP mode = new PageP(); // 当前要打印的页面(包含数据DataGrid)
int PrintPageindex = itemindex + 1 ;
mode.MinHeight = 1058 ; // 根据A4纸显示高度
mode.dataGrid1.ItemsSource = itemsource;
stack.Children.Add(mode);
stack.Measure( new Size(pe.PrintableArea.Width, double .PositiveInfinity));
if (stack.DesiredSize.Height > pe.PrintableArea.Height | stack.Children.Count > 1 )
{
stack.Children.Remove(mode);
stack.Measure(pe.PrintableArea);
pe.HasMorePages = true ;
break ;
}
itemindex += 1 ;
}
pe.PageVisual = stack;
};
pd.Print( null );
}
// 获取指定的List集合
public static List < T > LoadNewList < T > (List < T > _source, int PIndex, int Pcounts)
{
List < T > newList = new List < T > ();
newList.AddRange(_source.GetRange(PIndex, Pcounts));
return newList;
}
附:一般打印都是A4纸;A4纸的标准是宽21cm;高29.7cm.像素为:宽588px;高:862Px;
一般除开边距margin(70,80,70,80) 余下部分就是可以是datagrid的高度,Pagesize=余下高度/(行高)。
简单数据分页打印
如有不足地方,请指出