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; }
}
上一篇:Android service的开启和绑定,以及调用service的方法


下一篇:[struts2]Struts遍历标签总结 [转]