/// <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, System.Printing.PageOrientation orientation = System.Printing.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, "");
            }
        }
Beispiel #2
0
        public static void Print(FrameworkElement 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(source, dialog, orientation);
                Zoom(viewer, zoom);
                dialog.PrintDocument(viewer.Document.DocumentPaginator, "");
            }
        }
        /// <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, System.Printing.PageOrientation orientation = System.Printing.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>
        /// 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, System.Printing.PageOrientation orientation = System.Printing.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;
                    page.Margin = new Thickness(25);

                    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);
        }