none
Vista previa DataGrid WPFy adaptar al tamaño del papel RRS feed

  • Pregunta

  •  

    Finalmente he podido imprimir el contenido total de un DataGrid con el mismo formato con este código. ¿Alguien puede decirme como puedo adaptar la impresión al tamaño del papel y como puedo generar una vista previa?

     

     

    public class UIPrinter

    {

        #region Properties

        public string s_EN_Question_Print = "Print?";

        public string s_EN_Info_PrintingAborted = "Print Aborted";

        public string s_EN_Info_Printing = "Printing";

        public string s_EN_Info_PrintingDone = "Print Succesful";

     

        public Int32 VerticalOffset { get; set; }

        public Int32 HorizontalOffset { get; set; }

        public String Title { get; set; }

        public UIElement Content { get; set; }

     

        #endregion

     

        #region Initialization

     

        public UIPrinter()

        {

            HorizontalOffset = 20;

            VerticalOffset = 20;

            Title = "Print " + DateTime.Now;

        }

     

        #endregion

     

        #region Methods

     

        public Int32 Print()

        {

            var dlg = new PrintDialog();

            if (dlg.ShowDialog() == true)

            {

                //---FIRST PAGE---//

                // Size the Grid.

                Content.Measure(new Size(Double.PositiveInfinity,

                                         Double.PositiveInfinity));

     

                Size sizeGrid = Content.DesiredSize;

     

                //check the width

                if (sizeGrid.Width > dlg.PrintableAreaWidth)

                {

                    MessageBoxResult result = MessageBox.Show(s_EN_Question_Print, "Print", MessageBoxButton.YesNo, MessageBoxImage.Question);

                    if (result == MessageBoxResult.No)

                        throw new System.InvalidOperationException(s_EN_Info_PrintingAborted);

                }

     

                // Position of the grid

                var ptGrid = new Point(HorizontalOffset, VerticalOffset);

     

                // Layout of the grid

                Content.Arrange(new Rect(ptGrid, sizeGrid));

     

                //print

                dlg.PrintVisual(Content, Title);

     

                //---MULTIPLE PAGES---//

                double diff;

                int i = 1;

                while ((diff = sizeGrid.Height - (dlg.PrintableAreaHeight - VerticalOffset * i) * i) > 0)

                {

                    //Position of the grid

                    var ptSecondGrid = new Point(HorizontalOffset, -sizeGrid.Height + diff + VerticalOffset);

     

                    // Layout of the grid

                    Content.Arrange(new Rect(ptSecondGrid, sizeGrid));

     

                    //print

                    int k = i + 1;

                    dlg.PrintVisual(Content, Title + " (Page " + k + ")");

     

                    i++;

                }

     

                return i;

            }

     

            throw new System.InvalidOperationException(s_EN_Info_PrintingAborted);

          

        }

        public void UIPrinterDatagrid(DataGrid Grid)

        {

     

            MessageBoxResult result = MessageBox.Show(s_EN_Question_Print, "Print", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)

            {

                try

                {

                    var border = VisualTreeHelper.GetChild(Grid, 0) as Decorator;

                    if (border != null)

                    {

                        var scrollViewer = border.Child as ScrollViewer;

                        if (scrollViewer != null)

                        {

                            scrollViewer.ScrollToTop();

                            scrollViewer.ScrollToLeftEnd();

                        }

                    }

     

                    Title = _initialTitle + " - " + s_EN_Info_Printing;

     

                    var myPrinter = new UIPrinter { Title = Title, Content = border };

                    int nbrOfPages = myPrinter.Print();

     

                    Title = _initialTitle + " - " + myPrinter.s_EN_Info_PrintingDone + " (" + nbrOfPages + " Pages)";

                }

                catch (Exception ex)

                {

                    Title = _initialTitle + " - " + ex.Message;

                    MessageBox.Show("" + ex);

                }

            }

        }

        public string _initialTitle

        {

            get;

            set;

     

        }

    • Cambiado Joyce_AC miércoles, 21 de junio de 2017 13:49
    miércoles, 21 de junio de 2017 8:39