Loading

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();
            }
        }

    } 

 

posted @ 2013-03-27 10:23  Agile.Zhou  阅读(2373)  评论(0编辑  收藏  举报