首页 > 代码库 > 流文档 打印范围
流文档 打印范围
//打印页码范围设置
private void PrintPageRangeSet(DocumentPaginator doc,int pageFrom,int pageTo)
{
//写入内存的XPS文档
var ms = new MemoryStream();
string pack = "pack://temp" + ".xps";
var package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PackageStore.AddPackage(new Uri(pack), package);
var xpsDoc = new XpsDocument(package, CompressionOption.SuperFast, pack);
//创建xpsDoc的xpsDocumentWriter对象
var xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
//将传入的doc写入临时的XPS对象中
xpsDocumentWriter.Write(doc);
//在docCopy中显示临时的XPS文档
var docCopy = xpsDoc.GetFixedDocumentSequence();
//创建系统打印机中的writer对象
XpsDocumentWriter printwriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
//返回一个可将 Visual 对象写入文档或打印队列中的 VisualsToXpsDocument。
var visualsToXpsDocument = (VisualsToXpsDocument)printwriter.CreateVisualsCollator();
//最大页码判断
if (pageTo > Viewer.PageCount)pageTo = Viewer.PageCount;
//讲分页器中的每一页加入容器中
for (int i = pageFrom-1; i < pageTo; i++)
{
var visual = ((IDocumentPaginatorSource)docCopy).DocumentPaginator.GetPage(i).Visual;
var cv = new ContainerVisual();
cv.Children.Add(visual);
visualsToXpsDocument.Write(cv, printDialog.PrintTicket);
cv.Children.Remove(visual);
}
visualsToXpsDocument.EndBatchWrite();
xpsDoc.Close();
}
流文档 打印范围