0

Trying to print a flowdocument. I print the document within a foreach and change the name every pass through. My problem is that after i print it out the first time the name does not change the second/etc. time through the loop. Why is this? Here is the code

PrintDialog p = new PrintDialog();
                if (p.ShowDialog().Value == true)
                {
                    foreach (CustCnt c in customerContacts)
                    { 
                        ((TextBlock)doc.FindName("Name")).Text = c.Name;

                        SelectCOADelivery("FaxLabel", "FaxNumber", c.CheckBox17, c.FaxNum, doc);
                        SelectCOADelivery("EmailLabel", "Email", c.CheckBox16, c.EMailAddress, doc);
                        SelectCOADelivery("LoadLabel", null, c.CheckBox18, null, doc);

                        ((TextBlock)doc.FindName("FaxNumber")).Text = c.FaxNum;
                        ((TextBlock)doc.FindName("Email")).Text = c.EMailAddress;
                        p.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Baker Data");
                    }
                }
MikeC
  • 284
  • 1
  • 3
  • 14

1 Answers1

2

I think your trying to print a document before the layout renders. Try calling

UpdateLayout();

before you print the document.

MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
  • How do you call `UpdateLayout` before printing the document? Does the document need to displayed in one of the FlowDocument viewers (`FlowDocumentScrollViewer`)? – Dennis Feb 25 '12 at 22:47
  • When you want to print something you need to give the control to a PrintDialog. The control you print needs to be visible and rendered before you pass it to Paginator. Some people update the usercontrol but its not rendered yet. That is why you would call `UpdateLayout()` In the example above you would call `UpdateLayout()` right before `p.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Baker Data");` – MyKuLLSKI Feb 26 '12 at 04:27
  • Thanks @MyKuLLSKI. I ask as I'm having difficulty printing a `FlowDocument` to `XpsDocument`. I'm certain it is because the document has not been rendered however I cannot see how/where to call `UpdateLayout()` See: http://stackoverflow.com/questions/9447338/printing-blockuicontainer-to-xpsdocument-fixeddocument – Dennis Feb 26 '12 at 09:29
  • You can only call `UpdateLayout()` within a WPF Control. – MyKuLLSKI Feb 26 '12 at 17:21
  • Yes, only from a `UIElement`, however as `FlowDocument` does not descend from `UIElement` the only way that I've that could work would be to walk the logical tree or use the `ContextualLayoutManager` for the current dispatcher. – Dennis Feb 26 '12 at 22:50