简体   繁体   中英

WPF DocumentViewer - Print with no confirmation

I have a WPF application where I use a document viewer. I also start printing programmatically with documentviewer.Print(); However, when that is pressed it brings up the screen with the Windows printers and makes the user have to click "OK" again on that screen to start. Is there a way to avoid the confirmation and make documentviewer.Print(); immediately start the print job on the default Windows printer?

All you need is the default print queue, which you can get via

var pq = LocalPrintServer.GetDefaultPrintQueue()

From this, you can create an XpsDocumentWriter :

var writer = PrintQueue.CreateXpsDocumentWriter(pq);

Now, you can get the DocumentPaginator from your DocumentViewer via the Document property , which returns an IDocumentPaginatorSource that has a DocumentPaginator property :

var paginator = documentviewer.Document.DocumentPaginator;

and you can send that right to the XpsDocumentWriter's Write method :

writer.Write(paginator);

Simple, isn't it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM