none
WPF datagrids not flowing onto second page with PrintDocument DocumentPaginator RRS feed

  • Question

  • I have a window containing a number of textboxes and two WPF datagrids.  When printing using a FixedDocument print method and DocumentPaginator, the datagrid rows are not flowing into the second page but being truncated and printing only on the first page:

    private void PrintTest()
            {
                PrintDialog printDlg = new PrintDialog();
                printDlg.PrintTicket.PageOrientation = PageOrientation.Portrait;
                PageMediaSize pageSize = new PageMediaSize(PageMediaSizeName.ISOA4);
                printDlg.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);
                if ((bool)printDlg.ShowDialog().GetValueOrDefault())
                {
                    btnShowSummary.Visibility = Visibility.Hidden;
                    dplPastTransactions.Visibility = Visibility.Collapsed;
                    bdrTaxationSummary.Visibility =  Visibility.Collapsed; //Print summary panel on last page only
                    double windowHeight = wdwTaxationReport.Height;
                    dgdIncomeTransactions.SelectedIndex = -1;
                    dgdExpenseTransactions.SelectedIndex = -1;
                    scvTaxationReport.ScrollToTop();
                    grdPrintingGrid.RenderTransform = Transform.Identity;
                    wdwTaxationReport.Height = 3000;
                    grdTaxationItemsGrid.Height = (dgdIncomeTransactions.Items.Count + 3 + dgdExpenseTransactions.Items.Count + 3) * 20 + 15;//Blank lines showing 
                    scvTaxationReport.Height = 3000;
                    grdPrintingGrid.Height = 3000;
                    scvTaxationReport.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                    grdPrintingGrid.LayoutTransform = new ScaleTransform(0.9, 0.9);
                    grdPrintingGrid.UpdateLayout();
                    PrintMethods.PrintHelper.ShowPrintPreview(PrintMethods.PrintHelper.GetFixedDocument(grdPrintingGrid, printDlg));
                    //PrintMethods.PrintHelper.PrintNoPreview(printDlg, PrintMethods.PrintHelper.GetFixedDocument(grdPrintingGrid, printDlg));
    
                    //Restore original screen settings after printing
                    grdTaxationItemsGrid.Height = ((dgdIncomeTransactions.Items.Count + 3 + dgdExpenseTransactions.Items.Count + 3) - SortTaxationTransactions.BlankRecords) * 20 + 10;//Prevent blank lines from showing
                    dplPastTransactions.Visibility = Visibility.Visible;
                    scvTaxationReport.Height = Double.NaN;
                    scvTaxationReport.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    wdwTaxationReport.Height = windowHeight;
                    grdPrintingGrid.UpdateLayout();
                    grdPrintingGrid.LayoutTransform = new ScaleTransform(1, 1);
                    grdPrintingGrid.RowDefinitions[0].Height = new GridLength(50);
                    grdPrintingGrid.Height = SystemParameters.PrimaryScreenHeight < 850 ? 620 : 740;
                    btnShowSummary.Visibility = Visibility.Visible;
                    grdPrintingGrid.UpdateLayout();
                    if (dgdIncomeTransactions.Items.Count > 0)
                    {
                        dgdIncomeTransactions.ScrollIntoView(dgdIncomeTransactions.Items[0]);
                    }
                    scvTaxationReport.ScrollToTop();
                    grdPrintingGrid.UpdateLayout();
                }
            }

    The code for the GetFixedDocument and PrintPreview methods is:

    public static FixedDocument GetFixedDocument(FrameworkElement toPrint, PrintDialog printDialog)
                {
                    Thickness margin = new Thickness(0);
                    var capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
                    var pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
                    var visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth-margin.Left-margin.Right, capabilities.PageImageableArea.ExtentHeight-margin.Top-margin.Bottom);
                    var fixedDoc = new FixedDocument();
                    //If the toPrint visual is not displayed on screen we neeed to measure and arrange it  
                    toPrint.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    toPrint.Arrange(new Rect(new Point(0, 0), toPrint.DesiredSize));
                    //  
                    var size = toPrint.DesiredSize;
                    //Will assume for simplicity the control fits horizontally on the page  
                    double yOffset = 0;
                    while (yOffset < size.Height)
                    {
                        var vb = new VisualBrush(toPrint)
                        {
                            Stretch = Stretch.None,
                            AlignmentX = AlignmentX.Left,
                            AlignmentY = AlignmentY.Top,
                            ViewboxUnits = BrushMappingMode.Absolute,
                            TileMode = TileMode.None,
                            Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height)
                        };
                        var pageContent = new PageContent();
                        var page = new FixedPage();
                        ((IAddChild)pageContent).AddChild(page);
                        fixedDoc.Pages.Add(pageContent);
                        page.Width = pageSize.Width;
                        page.Height = pageSize.Height;
                        var canvas = new Canvas();
                        FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                        FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
                        canvas.Width = visibleSize.Width;
                        canvas.Height = visibleSize.Height;
                        canvas.Background = vb;
                        page.Children.Add(canvas);
                        yOffset += visibleSize.Height;
                    }
                    return fixedDoc;
                }
    
                public static void ShowPrintPreview(FixedDocument fixedDoc)
                {
                    var wnd = new Window();
                    var viewer = new DocumentViewer();
                    viewer.Document = fixedDoc;
                    wnd.Content = viewer;
                    wnd.ShowDialog();
                    
                }

    As you can see, I resize the XAML window height and the heights of all scroll viewers and grids when printing to ensure that all rows of the datagrids will appear in the fixed document (I have just set a height of 3000 at present but future code will calculate this with reference to the actual number of datagrid rows to show) but the print helper cuts off the datagrid at the end of the first page and does not allow it to flow over into the second or subsequent pages, which are blank. 

    Can you help me to fix this?


    Mike Whalley

    Thursday, April 11, 2019 8:32 AM

All replies

  • Hi Mike,

    According to your description, you have some issue about printing DataGrid in wpf, can you upload your demo at Onedrive and share the link here, I will download your project and try to reproduce your issue at my side.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 4:43 AM
    Moderator
  • Cherry, thank you for the response.

    I have uploaded an example application to OneDrive: Click here

    It is a small subset of my application including only the window and print method that is producing the problem, with dummy data - I have not tried to make it look pretty but it is sufficiently functional for your purposes and fully replicates the print problem.  The print method is at line 145 of TaxationReport.xaml.cs. 

    Note that the window and datagrid heights are set artificially at 3000 when printing to allow for all the data to appear on the canvas - this will be adjusted in my final code with reference to the actual number of datagrid rows.

    I have previously been paginating manually, but that prevents the creation of a single, multi-page pdf, or duplex printing.

    I look forward to hearing from you if you can find a solution for me.

    Best wishes

    Mike


    Mike Whalley

    Friday, April 12, 2019 2:03 PM
  • Cherry, thank you for the response.

    I have uploaded an example application to OneDrive: Click here

    It is a small subset of my application including only the window and print method that is producing the problem, with dummy data - I have not tried to make it look pretty but it is sufficiently functional for your purposes and fully replicates the print problem.  The print method is at line 145 of TaxationReport.xaml.cs. 

    Note that the window and datagrid heights are set artificially at 3000 when printing to allow for all the data to appear on the canvas - this will be adjusted in my final code with reference to the actual number of datagrid rows.

    I have previously been paginating manually, but that prevents the creation of a single, multi-page pdf, or duplex printing.

    I look forward to hearing from you if you can find a solution for me.

    Best wishes

    Mike


    Mike Whalley

    Hi Mike,

    According to your description, you want to pagination printing datagrid?

    If yes, I find some article that you can take a look:

    https://www.codeproject.com/Articles/1092189/WPF-Pagination-for-DataGrid

    https://www.codeproject.com/Articles/1257860/Paging-WPF-DataGrid

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 2, 2019 6:49 AM
    Moderator
  • Cherry

    Thank you, but those links deal with paginating a datagrid on the screen.  My problem relates to printing, and specifically why my GetFixedDocument method fails to print the lower part of the datagrid on the second and subsequent pages (see illustration at the top of my initial posting).  The FrameworkElement (toPrint) has a DesiredSize greater than the A4 page height and although the While loop iterates three times, nothing is painted on the canvas for the second and third pages.

    Can you help to explain or fix this?

    Best wishes

    Mike


    Mike Whalley

    Friday, May 3, 2019 1:14 PM
  • Hi Mike,

    >>My problem relates to printing, and specifically why my GetFixedDocument method fails to print the lower part of the datagrid on the second and subsequent pages (see illustration at the top of my initial posting).  The FrameworkElement (toPrint) has a DesiredSize greater than the A4 page height and although the While loop iterates three times, nothing is painted on the canvas for the second and third pages.
    Can you help to explain or fix this?

    I try your sample at my side, but I don't get the same result, I get the result like this, what I have missed?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 14, 2019 10:12 AM
    Moderator
  • Cherry, thank you for continuing with this.  You have sent a screen shot of the report as it presents on screen, which is as it should be, rather than a copy of the print output, where the problem lies.  If you click on File and select Print, you will see a print preview similar to that illustrated at the beginning of this thread which shows only one page of data and no continuation onto a second or subsequent page.  This is the problem I am trying to find a solution for.

    Please keep trying for me!

    Many thanks

    Mike


    Mike Whalley

    Friday, May 17, 2019 3:32 PM