WPF PrintDialog 支持选择打印范围

2022-05-07 16:26:19 浏览数 (1)

坑爹的PrintDialog 其实并没有队PageRange这个属性做处理。自己处理一下~

代码语言:javascript复制
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();
            }
        }
代码语言:javascript复制
    } 
wpf

0 人点赞