WPF DocumentViewer - print without confirmation

I have a WPF application where I use a document viewer. I am also starting to print programmatically using documentviewer.Print (); However, when this is pressed, it displays a screen using Windows printers and forces the user to click “OK” again on this screen. Is there a way to avoid confirmation and do documentviewer.Print (); immediately start the print job on the default windows printer?

+7
source share
1 answer

All you need is a default print queue, which you can get through

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 using the Document Property , which returns the IDocumentPaginatorSource that has the DocumentPaginator property:

 var paginator = documentviewer.Document.DocumentPaginator; 

and you can send this right to the XpsDocumentWriter Write method :

 writer.Write(paginator); 

Simple, right?

+7
source

All Articles