Printing AllPages with FlowDocumentScrollViewer

622 Views Asked by At

In a previous question I was trying to print from my TreeView, now i go to the parent view and I have hare an UserControl how contain an TreeView to display my search result on my WPFview, i tryed to print it and i surround with FlowDocumentScrollViewer as the code below:

view.xaml

<FlowDocumentScrollViewer x:Name="flow" Margin="10" BorderThickness="0">
<FlowDocument>
    <Section>
        <BlockUIContainer>
            <my:SearchTreeView Content="{Binding Stvm}"/>
        </BlockUIContainer>
    </Section>
</FlowDocument>

view.xaml.cs

 Printing.PrintDoc( flow.Document, txtSearch.Text + " - Result");

static printing.cs

public static void PrintDoc(System.Windows.Documents.FlowDocument fd, string description)
    {
        double h, w, cw;            
        h = fd.PageHeight ;
        w = fd.PageWidth ;
        cw = fd.ColumnWidth;

        PrintDialog pd = new PrintDialog();
        //pd.PageRangeSelection = PageRangeSelection.AllPages;
        //pd.UserPageRangeEnabled = true;

        if (pd.ShowDialog() != true || fd == null) return;

        fd.PageHeight = pd.PrintableAreaHeight;
        fd.PageWidth = pd.PrintableAreaWidth;
        fd.PagePadding = new Thickness(50);
        fd.ColumnGap = 0;
        fd.ColumnWidth = pd.PrintableAreaWidth;

        System.Windows.Documents.IDocumentPaginatorSource dps  = fd;
      //  int c = dps.DocumentPaginator.PageCount;//0

        pd.PrintDocument(dps.DocumentPaginator, description);

        fd.PageHeight = h;
        fd.PageWidth = w;
        fd.PagePadding = new Thickness(0);
        fd.ColumnWidth = cw;
    }

i tryed almost all the exmple in the Similar Questions but the best i got is just the first page of result like this.. ;(

I'm Using WPF MVVM pattern. Is this is the right control to do it?Or shall I go for any other WPF controls.?

1

There are 1 best solutions below

0
On

The way that I tackled this problem was by defining an FlowDocument control containing an empty Table object in the *.xaml portion of my view. I then dynamically added Blocks containing the controls that I wanted to print, to the Table in the FlowDocument, via the codebehind (i.e. The *.xaml.cs portion of the view). If you define each UI element that you want to print in a separate Block, the Table will automatically perform pagination when printed. You will want to figure out a way to break your SearchTreeView control into smaller controls so that it isn't contained in only one Block.

For more info on the Table control look here: http://msdn.microsoft.com/en-us/library/ms747133%28v=vs.110%29.aspx