Skip Navigation LinksHome / Community & Support / Code Library / WPF > GridView > RadGridView Print and Print Preview

Answered RadGridView Print and Print Preview

Feed from this thread
  • Answer Womack avatar

    Posted on May 25, 2010 (permalink)

    Requirements

    RadControls version

     

    WPF Q1 2010 SP1
    .NET version

     

    4.0
    Visual Studio version

     

    2010
    programming language

     

    C#

    PROJECT DESCRIPTION
    I recently wanted to enable my GridView to perform print and print preview functions.  The example at Vladimir Enchev's Post about Print Preview was a great start, but it had the following limitations:
    • It only showed the first page of a multi-page (too wide and too high to fit on one page) grid
    • It did not support adding a custom title (I've done this through the Grid.ToolTip property)
    • It did not open up to a reasonable size (I detect multiple monitors and open it in most of the second)
    • It did not provide arguments for Portrait vs. Landscape
    • It did not provide arguments for Zoom type (i.e. Full page, Zoom to Width, Zoom to Height, Two Page Wide)
    • It was light on comments, though the code was quite clear
    The following code addresses the above, building upon Vladimir's great start.  I hope you find it of use.

    using System.Linq; 
    using System.Printing; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Markup; 
    using System.Windows.Media; 
    using Telerik.Windows.Controls; 
    using Telerik.Windows.Controls.GridView; 
     
    namespace YourNamespaceHere 
        /// <summary> 
        /// Support Printing Related Methods 
        /// </summary> 
        public static class PrintExtensions 
        { 
            #region ___________ Properties ____________________________________________________________________________________________ 
            /// <summary> 
            /// Zoom Enumeration to specify how pages are stretched in print and preview 
            /// </summary> 
            public enum ZoomType 
            { 
                /// <summary> 
                /// 100% of normal size 
                /// </summary> 
                Full, 
     
                /// <summary> 
                /// Page Width (fit so one page stretches to full width) 
                /// </summary> 
                Width, 
     
                /// <summary> 
                /// Page Height (fit so one page stretches to full height) 
                /// </summary> 
                Height, 
     
                /// <summary> 
                /// Display two columsn of pages 
                /// </summary> 
                TwoWide 
            };        
            #endregion 
            #region ___________ Methods _______________________________________________________________________________________________ 
            /// <summary> 
            /// Print element to a document 
            /// </summary> 
            /// <param name="element">GUI Element to Print</param> 
            /// <param name="dialog">Reference to Print Dialog</param> 
            /// <param name="orientation">Page Orientation (i.e. Portrait vs. Landscape)</param> 
            /// <returns>Destination document</returns> 
            static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog, PageOrientation orientation = PageOrientation.Portrait) 
            { 
                dialog.PrintTicket.PageOrientation = orientation; 
                PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket); 
                Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight); 
                Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); 
     
                FixedDocument fixedDocument = new FixedDocument(); 
     
                element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 
                element.Arrange(new Rect(new Point(0, 0), element.DesiredSize)); 
     
                for (double y = 0; y < element.DesiredSize.Height; y += extentSize.Height) 
                { 
                    for (double x = 0; x < element.DesiredSize.Width; x += extentSize.Width) 
                    { 
                        VisualBrush brush = new VisualBrush(element); 
                        brush.Stretch = Stretch.None; 
                        brush.AlignmentX = AlignmentX.Left; 
                        brush.AlignmentY = AlignmentY.Top; 
                        brush.ViewboxUnits = BrushMappingMode.Absolute; 
                        brush.TileMode = TileMode.None; 
                        brush.Viewbox = new Rect(x, y, extentSize.Width, extentSize.Height); 
     
                        PageContent pageContent = new PageContent(); 
                        FixedPage page = new FixedPage(); 
                        ((IAddChild)pageContent).AddChild(page); 
     
                        fixedDocument.Pages.Add(pageContent); 
                        page.Width = pageSize.Width; 
                        page.Height = pageSize.Height; 
     
                        Canvas canvas = new Canvas(); 
                        FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth); 
                        FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight); 
                        canvas.Width = extentSize.Width; 
                        canvas.Height = extentSize.Height; 
                        canvas.Background = brush; 
     
                        page.Children.Add(canvas); 
                    } 
                } 
                return fixedDocument; 
            } 
     
            /// <summary> 
            /// Convert GridView to Printer-Friendly version of a GridView 
            /// </summary> 
            /// <param name="source">Input GridView</param> 
            /// <returns>Printer-Friendly version of source</returns> 
            static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source) 
            { 
                RadGridView grid = new RadGridView(); 
     
                grid.ItemsSource = source.ItemsSource; 
                grid.RowIndicatorVisibility = Visibility.Collapsed; 
                grid.ShowGroupPanel = false
                grid.CanUserFreezeColumns = false
                grid.IsFilteringAllowed = false
                grid.AutoExpandGroups = true
                grid.AutoGenerateColumns = false
     
                foreach (GridViewDataColumn column in source.Columns.OfType<GridViewDataColumn>()) 
                { 
                    GridViewDataColumn newColumn = new GridViewDataColumn(); 
                    newColumn.Width = column.ActualWidth; 
                    newColumn.DisplayIndex = column.DisplayIndex; 
                    //newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName); 
                    newColumn.DataMemberBinding = column.DataMemberBinding; // Better to just copy the references to get all the custom formatting 
                    newColumn.DataFormatString = column.DataFormatString; 
                    newColumn.TextAlignment = column.TextAlignment; 
                    newColumn.Header = column.Header; 
                    newColumn.Footer = column.Footer; 
                    grid.Columns.Add(newColumn); 
                } 
     
                StyleManager.SetTheme(grid, StyleManager.GetTheme(grid)); 
     
                grid.SortDescriptors.AddRange(source.SortDescriptors); 
                grid.GroupDescriptors.AddRange(source.GroupDescriptors); 
                grid.FilterDescriptors.AddRange(source.FilterDescriptors); 
     
                return grid; 
            } 
     
            /// <summary> 
            /// Perform a Print Preview on GridView source 
            /// </summary> 
            /// <param name="source">Input GridView</param> 
            /// <param name="orientation">Page Orientation (i.e. Portrait vs. Landscape)</param> 
            /// <param name="zoom">Zoom Enumeration to specify how pages are stretched in print and preview</param> 
            public static void PrintPreview(GridViewDataControl source, PageOrientation orientation = PageOrientation.Landscape, ZoomType zoom = ZoomType.TwoWide) 
            { 
                Window window = new Window(); 
                window.Title = "Print Preview"
                if (!string.IsNullOrWhiteSpace(source.ToolTip as string)) window.Title += " of " + source.ToolTip; 
                window.Width = SystemParameters.PrimaryScreenWidth * 0.92; 
                window.Height = SystemParameters.WorkArea.Height; 
                window.Left = constrain(SystemParameters.VirtualScreenWidth - SystemParameters.PrimaryScreenWidth, 0, SystemParameters.VirtualScreenWidth - 11); 
                window.Top = constrain(0, 0, SystemParameters.VirtualScreenHeight - 25); 
     
                DocumentViewer viewer = new DocumentViewer(); 
                viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), new PrintDialog(), orientation); 
                Zoom(viewer, zoom); 
                window.Content = viewer; 
     
                window.ShowDialog(); 
            } 
     
            /// <summary> 
            /// Constrain val to the range [val_min, val_max] 
            /// </summary> 
            /// <param name="val">Value to be constrained</param> 
            /// <param name="val_min">Minimum that will be returned if val is less than val_min</param> 
            /// <param name="val_max">Maximum that will be returned if val is greater than val_max</param> 
            /// <returns>val in [val_min, val_max]</returns> 
            private static double constrain(double val, double val_min, double val_max) 
            { 
                if (val < val_min) return val_min; 
                else if (val > val_max) return val_max; 
                else return val; 
            } 
     
            /// <summary> 
            /// Perform a Print on GridView source 
            /// </summary> 
            /// <param name="source">Input GridView</param> 
            /// <param name="showDialog">True to show print dialog before printing</param> 
            /// <param name="orientation">Page Orientation (i.e. Portrait vs. Landscape)</param> 
            /// <param name="zoom">Zoom Enumeration to specify how pages are stretched in print and preview</param> 
            public static void Print(GridViewDataControl source, bool showDialog = true, PageOrientation orientation = PageOrientation.Landscape, ZoomType zoom = ZoomType.TwoWide) 
            { 
                PrintDialog dialog = new PrintDialog(); 
                bool? dialogResult = showDialog ? dialog.ShowDialog() : true
     
                if (dialogResult == true
                { 
                    DocumentViewer viewer = new DocumentViewer(); 
                    viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), dialog, orientation); 
                    Zoom(viewer, zoom); 
                    dialog.PrintDocument(viewer.Document.DocumentPaginator, ""); 
                } 
            } 
     
            /// <summary> 
            /// Scale viewer to size specified by zoom 
            /// </summary> 
            /// <param name="viewer">Document to zoom</param> 
            /// <param name="zoom">Zoom Enumeration to specify how pages are stretched in print and preview</param> 
            public static void Zoom(DocumentViewer viewer, ZoomType zoom) 
            { 
                switch (zoom) 
                { 
                    case ZoomType.Height: viewer.FitToHeight(); break
                    case ZoomType.Width: viewer.FitToWidth(); break
                    case ZoomType.TwoWide: viewer.FitToMaxPagesAcross(2); break
                    case ZoomType.Full: break
                } 
            } 
            #endregion 
        } 

    Reply

  • Vlad Vlad admin's avatar

    Posted on May 26, 2010 (permalink)

    Hi Brian,

    Great post! I've added 5000 Telerik points to your account.

    Best wishes,
    Vlad
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.

    Reply

  • frdotnet avatar

    Posted on Jul 23, 2010 (permalink)

    Quick question. The grid does not show the entire content of the cells. so when the grid is created I added the line 
    grid.width = gridviewlength.auto. But with this , the grids width after expanding the text of all the columns is not known, so it does not continue printing rest of the columns on next page . Any ideas ?

    Reply

  • Vlad Vlad admin's avatar

    Posted on Jul 29, 2010 (permalink)

    Hi frdotnet,

     Printing rest of the columns on the next page will not be a trivial task unfortunately. The only solution I can think of currently is to hide columns out of view and print only these columns explicitly on the next page (hide other columns). 

    All the best,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

    Reply

  • Christian avatar

    Posted on Oct 18, 2010 (permalink)

    element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

    This line of code (inside ToFixedDocument method) produces an error, hangs my program. What could be the problem?

    Reply

  • Vlad Vlad admin's avatar

    Posted on Oct 19, 2010 (permalink)

    Hello,

     Can you post more info about the error?

    Greetings,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

    Reply

  • Roman avatar

    Posted on Jan 19, 2011 (permalink)

    Hi,

    I have similar problem as Christian mentioned above-  "element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    This line of code (inside ToFixedDocument method) produces an error, hangs my program. What could be the problem? "

    I get InvalidOpeartionException - "Specified element is already the logical child of another element. Disconnect it first."
    Most likely it happens without nested child grids.  Any help with this?

    Reply

  • Alan avatar

    Posted on Feb 24, 2011 (permalink)

    I have this same problem when printing.

    System.InvalidOperationException occurred
      Message=Specified element is already the logical child of another element. Disconnect it first.
      Source=PresentationFramework
      StackTrace:
           at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
      InnerException: 


    I am printing a RadTreeListVIew using this example.

    Reply

  • Yordanka Yordanka admin's avatar

    Posted on Feb 28, 2011 (permalink)

    Hello,

    Could you open a separate support ticket and attach the sample project where this exception can be seen ? We have tried to reproduce it locally but unfortunately, we could not.
     
    All the best,
    Yordanka
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!

    Reply

  • Alan avatar

    Posted on Feb 28, 2011 (permalink)

    Yordanka,

    I figured out what the problem was. If you set the column header (in the xaml) to be a be TextBlock and then add newColumn.Header = column.Header inside the for loop, you will see the problem. 

    foreach (GridViewDataColumn column in source.Columns.OfType<GridViewDataColumn>()) 
    {
    ...
    newColumn.Header = column.Header;
    }

    I was able to work around it by copying the TextBlock's "Text" property to the newColumn.Header, and then it worked.
    Let me know if you still need a support ticket, and I'll create one.

    Reply

  • Yordanka Yordanka admin's avatar

    Posted on Mar 2, 2011 (permalink)

    Hi Alan,

    Thank you for cooperation.

    In your case, the TextBlock element is already part of the column's visual tree. So, you cannot connect it to nextColumn's visual tree in this way. You need first to disconnect it from column's visual tree (column.Header = null;) or to use the logic pointed by you. 
     
    All the best,
    Yordanka
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!

    Reply

  • Yogesh avatar

    Posted on Jul 14, 2011 (permalink)

    hello,

    PROJECT DESCRIPTION
     
    I recently wanted to enable my GridView to perform print and print preview functions.  The example a Yogesh Post about Print Preview was a great start, but it had the following limitations:


    using System.Linq;
    using System.Printing;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Markup;
    using System.Windows.Media;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.GridView;
    using System;


    namespace Enaptive.Synergy.DeliveryPlanner
    {
        public static class PrintExtensions
        {
            static string PageTitle = "";


            static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog)
            {
                var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
                //var pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
                var extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);


                var fixedDocument = new FixedDocument();
                fixedDocument.PrintTicket = dialog.PrintTicket;
                element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
                var totalHeight = element.DesiredSize.Height;
                var totalWidth = element.DesiredSize.Width;
                //var yOffset = 0d;
                //while (yOffset < totalHeight)
                //{
                //    var brush = new VisualBrush(element)
                //    {
                //        Stretch = Stretch.None,
                //        AlignmentX = AlignmentX.Left,
                //        AlignmentY = AlignmentY.Top,
                //        ViewboxUnits = BrushMappingMode.Absolute,
                //        TileMode = TileMode.None,
                //        Viewbox = new Rect(0, yOffset, extentSize.Width, extentSize.Height)
                //    };


                //    var pageContent = new PageContent();
                //    var page = new FixedPage();
                //    ((IAddChild)pageContent).AddChild(page);


                //    fixedDocument.Pages.Add(pageContent);
                //    page.Width = pageSize.Width;
                //    page.Height = pageSize.Height;


                //    // add a title to the page
                //    double top = capabilities.PageImageableArea.OriginHeight;
                //    TextBlock pageText = new TextBlock();
                //    pageText.Text = PageTitle;
                //    pageText.FontSize = 24; 
                //    pageText.Margin = new Thickness(5); 
                //    FixedPage.SetLeft(pageText, top);
                //    pageText.Width = extentSize.Width;                
                //    page.Children.Add(pageText);
                //    top += 18;


                //    TextBlock dateText = new TextBlock();
                //    dateText.Text = "As of " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                //    dateText.FontStyle = FontStyles.Italic;
                //    dateText.FontSize = 10;
                //    dateText.Margin = new Thickness(5); 
                //    FixedPage.SetLeft(dateText, capabilities.PageImageableArea.OriginWidth);
                //    FixedPage.SetTop(dateText, top);
                //    dateText.Width = extentSize.Width;
                //    page.Children.Add(dateText);
                //    top += 24;


                //    var canvas = new Canvas();
                //    FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                //    FixedPage.SetTop(canvas, top);
                //    canvas.Width = extentSize.Width;
                //    canvas.Height = extentSize.Height-top;
                //    canvas.Background = brush;


                //    page.Children.Add(canvas);


                //    yOffset += extentSize.Height;
                //}


                var yOffset = 0d;
                var xOffset = 0d;


                //double tmpHeight = 0;
                int j = 0, k = 0, i;
                while (yOffset < totalHeight)
                {
                    double tmpHeight = 0;
                    while (xOffset < totalWidth)
                    {
                        //for Width
                        double tmpWidth = 0;
                        var tmpElementWidth = (GridViewDataControl)element;
                        for (i = j; i < tmpElementWidth.Columns.Count; i++)
                        {
                            tmpWidth += tmpElementWidth.Columns[i].ActualWidth;
                            if (tmpWidth > extentSize.Width)
                            {
                                tmpWidth = extentSize.Width - (tmpWidth - tmpElementWidth.Columns[i].ActualWidth);
                                j = i - 1;
                                break;
                            }


                        }
                        if ((i < tmpElementWidth.Columns.Count) == false)
                        {
                            tmpWidth = 0;
                            j = 0;
                        }
                        //


                        //for Height
                        //var tmpElementHeight = (GridViewDataControl)element;
                        //for (i = k; i < tmpElementHeight.Items.Count; i++)
                        //{
                        //    tmpHeight += tmpElementHeight.RowHeight;
                        //    if (tmpHeight >= extentSize.Height)
                        //    {
                        //        tmpHeight = extentSize.Height - (tmpHeight - tmpElementHeight.RowHeight);
                        //        k = i - 1;
                        //        break;
                        //    }
                        //}
                        //if ((i < tmpElementWidth.Columns.Count) == false)
                        //{
                        //    tmpHeight = 0;
                        //    k = 0;
                        //}
                        //


                        var brush = new VisualBrush(element)
                        {
                            Stretch = Stretch.None,
                            AlignmentX = AlignmentX.Left,
                            AlignmentY = AlignmentY.Top,
                            ViewboxUnits = BrushMappingMode.Absolute,
                            TileMode = TileMode.None,
                            Viewbox = new Rect(xOffset, yOffset, extentSize.Width - tmpWidth, extentSize.Height - tmpHeight)
                        };


                        var pageContent = new PageContent();
                        var page = new FixedPage();
                        page.PrintTicket = dialog.PrintTicket;
                        ((IAddChild)pageContent).AddChild(page);
                        fixedDocument.Pages.Add(pageContent);
                        page.Width = extentSize.Width;
                        page.Height = extentSize.Height;


                        // add a title to the page
                        double top = capabilities.PageImageableArea.OriginHeight;
                        TextBlock pageText = new TextBlock();
                        pageText.Text = PageTitle;
                        pageText.FontSize = 24;
                        pageText.Margin = new Thickness(5);
                        FixedPage.SetLeft(pageText, top);
                        pageText.Width = extentSize.Width;
                        page.Children.Add(pageText);
                        top += 30;


                        TextBlock dateText = new TextBlock();
                        dateText.Text = "As of " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                        dateText.FontStyle = FontStyles.Italic;
                        dateText.FontSize = 10;
                        dateText.Margin = new Thickness(5);
                        FixedPage.SetLeft(dateText, capabilities.PageImageableArea.OriginWidth);
                        FixedPage.SetTop(dateText, top);
                        dateText.Width = extentSize.Width;
                        page.Children.Add(dateText);
                        top += 24;


                        var canvas = new Canvas();
                        FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                        FixedPage.SetTop(canvas, top);
                        canvas.Width = extentSize.Width - tmpWidth;
                        canvas.Height = extentSize.Height - top - tmpHeight;
                        canvas.Background = brush;


                        page.Children.Add(canvas);


                        xOffset += extentSize.Width - tmpWidth;
                        tmpHeight = 0;
                    }
                    xOffset = 0d;
                    yOffset += extentSize.Height - tmpHeight;
                }
                return fixedDocument;
            }


            static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source)
            {
                var grid = new RadGridView()
                {
                    ItemsSource = source.ItemsSource,
                    RowIndicatorVisibility = Visibility.Collapsed,
                    ShowGroupPanel = false,
                    CanUserFreezeColumns = false,
                    IsFilteringAllowed = false,
                    AutoExpandGroups = true,
                    AutoGenerateColumns = false,
                };


                foreach (var column in source.Columns.OfType<GridViewDataColumn>())
                {
                    string HideRedToggleButtonColumn = column.Header.ToString();
                    if (HideRedToggleButtonColumn != "Telerik.Windows.Controls.RadToggleButton Content: IsChecked:False" && HideRedToggleButtonColumn != "Telerik.Windows.Controls.RadToggleButton Content: IsChecked:True")
                    {
                        //MessageBox.Show(column.UniqueName);
                        var newColumn = new GridViewDataColumn();
                        newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName);
                        //newColumn.Width.IsSizeToCells = column.Width.IsSizeToCells;
                        //newColumn.DisplayIndex = column.DisplayIndex; 
                        //newColumn.DataType = typeof(string);
                        newColumn.Header = column.Header;
                        newColumn.Width = column.ActualWidth;


                        //newColumn.DisplayIndex = column.DisplayIndex;
                        newColumn.DataMemberBinding = column.DataMemberBinding; // Better to just copy the references to get all the custom formatting 
                        newColumn.DataFormatString = column.DataFormatString;
                        grid.Columns.Add(newColumn);
                    }
                }


                //foreach (GridViewDataColumn colms in source.Items.OfType<GridViewDataColumn>())
                //{
                //    var newColumn = new GridViewDataColumn();
                //    newColumn.DataMemberBinding = new System.Windows.Data.Binding(colms.UniqueName);
                //    grid.Columns.Add(newColumn);
                //}


                //for (int i = 0; i < source.Columns.Count; i++)
                //{
                //    var newColumn = new GridViewDataColumn();
                //    newColumn.DataMemberBinding = new System.Windows.Data.Binding(source.Columns[i].UniqueName);
                //    grid.Columns.Add(newColumn);
                //}
                //********************
                StyleManager.SetTheme(grid, StyleManager.GetTheme(grid));


                grid.SortDescriptors.AddRange(source.SortDescriptors);
                grid.GroupDescriptors.AddRange(source.GroupDescriptors);
                grid.FilterDescriptors.AddRange(source.FilterDescriptors);


                return grid;
            }


            public static void PrintPreview(this GridViewDataControl source, string pageTitle)
            {
                PageTitle = pageTitle;
                var dialog = new PrintDialog();
                dialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
                dialog.PrintTicket = dialog.PrintQueue.DefaultPrintTicket;
                //for Landscape
                dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
                var window = new Window()
                {
                    Title = "Print Preview - " + pageTitle,
                    Content = new DocumentViewer()
                    {
                        Document = ToFixedDocument(ToPrintFriendlyGrid(source), dialog)
                    }
                };


                window.ShowDialog();
            }


            public static void Print(this GridViewDataControl source, bool showDialog, string pageTitle)
            {
                PageTitle = pageTitle;


                var dialog = new PrintDialog();
                dialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
                dialog.PrintTicket = dialog.PrintQueue.DefaultPrintTicket;
                //for Landscape
                dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
                var dialogResult = showDialog ? dialog.ShowDialog() : true;


                if (dialogResult == true)
                {
                    var viewer = new DocumentViewer();
                    viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), dialog);
                    dialog.PrintDocument(viewer.Document.DocumentPaginator, "");
                }
            }
        }
    }

    regards
    Yogesh

    Reply

  • Marco Beretta avatar

    Posted on Mar 28, 2012 (permalink)

    Hi,
    I was trying to use the Print and printpreview code which published at the start of the discussion. its great to see the result. Except one small issue. At the end of each page, half of the row displayed in the first page, and the remaining half displaying in next page. Is there any way to control this display?

    Thanks,
    Marco

    Reply

  • Michael Intermediate avatar

    Posted on Apr 14, 2012 (permalink)

    OK

    How do you do this with MVC model?

    So far I have 
    <telerik:RadButton Content="Print" Command="{Binding Path=GridPrint.Command}"  Margin="25,5,5,5" Padding="5" Width="150" />
                                          <telerik:RadButton Content="Print Preview" Command="{Binding Path=GridPreview.Command}" Margin="25,5,5,5" Padding="5" Width="150" />OK

    but how do i send a reference to the grid from this?

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / WPF > GridView > RadGridView Print and Print Preview