WPF PrintDialog 支持选择打印范围
坑爹的PrintDialog 其实并没有队PageRange这个属性做处理。自己处理一下~
public class PrintDialogWithPageRange:PrintDialog
{
public new void PrintDocument(DocumentPaginator doc, string desp)
{
if (this.PageRangeSelection == PageRangeSelection.AllPages)
{
base.PrintDocument(doc,desp);
}
else
{
string timeStamp = DateTime.Now.DayOfYear.ToString() + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
string pack = "pack://temp" + timeStamp + ".xps";
var ms = new MemoryStream();
var package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PackageStore.AddPackage(new Uri(pack), package);
var xpsDoc = new XpsDocument(package, CompressionOption.SuperFast, pack);
var xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsDocumentWriter.Write(doc);
var fdsCopy = xpsDoc.GetFixedDocumentSequence();
var xdw =System.Printing.PrintQueue.CreateXpsDocumentWriter(this.PrintQueue);
var vtxd = (VisualsToXpsDocument)xdw.CreateVisualsCollator();
for (int i = this.PageRange.PageFrom - 1; i < this.PageRange.PageTo; i++)
{
var v = fdsCopy.DocumentPaginator.GetPage(i).Visual;
var cv = new ContainerVisual();
cv.Children.Add(v);
vtxd.Write(cv, this.PrintTicket);
cv.Children.Remove(v);
}
vtxd.EndBatchWrite();
xpsDoc.Close();
ms.Close();
ms.Dispose();
}
}
{
public new void PrintDocument(DocumentPaginator doc, string desp)
{
if (this.PageRangeSelection == PageRangeSelection.AllPages)
{
base.PrintDocument(doc,desp);
}
else
{
string timeStamp = DateTime.Now.DayOfYear.ToString() + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
string pack = "pack://temp" + timeStamp + ".xps";
var ms = new MemoryStream();
var package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PackageStore.AddPackage(new Uri(pack), package);
var xpsDoc = new XpsDocument(package, CompressionOption.SuperFast, pack);
var xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsDocumentWriter.Write(doc);
var fdsCopy = xpsDoc.GetFixedDocumentSequence();
var xdw =System.Printing.PrintQueue.CreateXpsDocumentWriter(this.PrintQueue);
var vtxd = (VisualsToXpsDocument)xdw.CreateVisualsCollator();
for (int i = this.PageRange.PageFrom - 1; i < this.PageRange.PageTo; i++)
{
var v = fdsCopy.DocumentPaginator.GetPage(i).Visual;
var cv = new ContainerVisual();
cv.Children.Add(v);
vtxd.Write(cv, this.PrintTicket);
cv.Children.Remove(v);
}
vtxd.EndBatchWrite();
xpsDoc.Close();
ms.Close();
ms.Dispose();
}
}
}
QQ群:1022985150 VX:kklldog 一起探讨学习.NET技术
作者:Agile.Zhou(kklldog)
出处:http://www.cnblogs.com/kklldog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。