WPF Printable Document

Hi, I have a print problem in WPF. I create a stream document and add some controls to this stream document. Print Preview works fine, and I have no problem printing from the preview window. The problem occurs when I print directly to the printer without previewing the print. But what is more surprising - when I use the XPS Document Writer as everyting printer in order, when I use any physical printer, some controls in my stream document do not appear. thanks in advance

+6
printing wpf flowdocument
source share
3 answers

Important to note: You can use XpsDocumentWriter even when directly printing to a physical printer. Do not make the mistake that I made, avoiding it just because you are not creating a .xps file!

Anyway - I had the same problem and none of the DoEvents() tricks seemed to work. I was also not particularly pleased with the need to use them first. In my situation, some of the data management commands printed well, but some others (nested UserControls) did not. It seemed that only one "level" was data binding, and the rest did not even bind to "DoEvents ()".

The solution was simple, though. Use XpsDocumentWriter as follows. it will open a dialog box in which you can select any physical printer you want.

  // 8.5 x 11 paper Size sz = new Size(96 * 8.5, 96 * 11); // create your visual (this is a WPF UserControl) var template = new PackingSlipTemplate() { DataContext = new PackingSlipViewModel(order) }; // arrange template.Measure(sz); template.Arrange(new Rect(sz)); template.UpdateLayout(); // print to XpsDocumentWriter // this will open a dialog and you can print to any installed printer // not just a 'virtual' .xps file PrintDocumentImageableArea area = null; XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area,); xps.Write(template); 

I found the OReilly book in the WPF Programming section โ€œquite useful in the chapterโ€œ Printing - found through Google Books .


If you do not want the print dialog to be displayed, but you want to print directly to the default printer, you can do the following. (For me, the application is designed to print packing lists in a warehouse environment - and I do not want a dialog to appear every time).

  var template = new PackingSlipTemplate() { DataContext = new PackingSlipViewModel(orders.Single()) }; // arrange template.Measure(sz); template.Arrange(new Rect(sz)); template.UpdateLayout(); LocalPrintServer localPrintServer = new LocalPrintServer(); var defaultPrintQueue = localPrintServer.DefaultPrintQueue; XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue); xps.Write(template, defaultPrinter.DefaultPrintTicket); 
+4
source share

XPS document can be printed without problems

+1
source share

I noticed one thing: tip: controls that do not appear are controls that I bind to some data, so the conclusion is that the binding does not work. Could it be that the binding is not performed before sending the document to the printer?

0
source share

All Articles