WPF Printing to fit page

前端 未结 2 600
暗喜
暗喜 2020-12-15 01:36

i searched for options how to print WPF controls and found some solutions. I do need to fit my printed control to printing page while preserving aspect ration (my control is

2条回答
  •  孤城傲影
    2020-12-15 02:12

        private void DrawGrap_Click(object sender, RoutedEventArgs e)
        {
            // Visual v = sender as Visual;
            Visual v = song2Grid as Visual;   // the object (it is a DataGrid) that you want to print out, not a window
            PrintDialog prtDlg = new PrintDialog();
            if (prtDlg.ShowDialog() == true)
            {
                // because 96 pixels in an inch for WPF window
                double marginLeft = 96.0 * 0.75; // left margin is 0.75 inches
                double marginTop = 96.0 * 0.75; // top margin is 0.75 inches
                double marginRight = 96.0 * 0.75; // right margin is 0.75 inches
                double marginBottom = 96.0 * 0.75; // bottom margin is 0.75 inches
    
                // the following steps do not works for a WPF window
                FrameworkElement win = v as FrameworkElement;
                Transform oldLayoutTransform = win.LayoutTransform;
                Size oldSize = new Size(win.ActualWidth, win.ActualHeight);
    
                System.Printing.PrintCapabilities pCapability = prtDlg.PrintQueue.GetPrintCapabilities(prtDlg.PrintTicket);
    
                // calculate print area that you want
                double printWidth = (pCapability.PageImageableArea.ExtentWidth - pCapability.PageImageableArea.OriginWidth)
                                    - (marginLeft + marginRight);
                double printHeight = (pCapability.PageImageableArea.ExtentHeight - pCapability.PageImageableArea.OriginHeight)
                    - (marginTop + marginBottom);
    
                // calculate the scale
                double scale = Math.Min(printWidth / oldSize.Width , printHeight / oldSize.Height);
                if (scale > 1.0)
                {
                    // keep the original size and layout if printable area is greater than the object being printed
                    scale = 1.0; 
                }
    
                // store the original layouttransform
                win.LayoutTransform = new ScaleTransform(scale, scale);
    
                // new size of the visual
                Size newSize = new Size(oldSize.Width*scale , oldSize.Height*scale);
                win.Measure(newSize);
    
                // centralize print area
                double xStartPrint = marginLeft + (printWidth - newSize.Width)/2.0;
                double yStartPrint = marginTop + (printHeight - newSize.Height)/2.0;
                win.Arrange(new Rect(new Point(xStartPrint,yStartPrint),newSize));
    
                // print out the visual
                prtDlg.PrintVisual(win as Visual, "PrintTest");
    
                // resotre the original layouttransform and size on screen
                win.LayoutTransform = oldLayoutTransform;
                win.Measure(oldSize);
                win.Arrange(new Rect(new Point(0,0),oldSize));
            }
        }
    

    It was an answer to the question that was asked by user1018711. Fitting a print out on one printer page using C# and WPF. When you want to print out a visual, which might be a control what may including a lot of controls (e.g. Button, DataGrid, TextBlock, Label ,and etc). Here I want to print a DataGrid named song2Drid to printer but its content was larger than page size of the printer (its width was wider than width of a paper) so it was truncated. I could not see all of them so I had to scale the visual but I wanted to keep the ratio just as the same as the old one.

    I also set paper margins to 0.75 inches to each side of paper, which left, top, right, and bottom. I also centralized the content of visual (song2Grid) on the paper. So I could see the printed content just on the center of the paper. But if the visual was a window like Application.Current.MainWindow or any window created programmatically by new Window(), then It will not be scaled. it meant this method wont work for a Window object.

    Also, If you want to recover the original look on screen from the changed one by scaling to print, then you have to have the statements as the following win.LayoutTransform = oldLayoutTransform; win.Measure(oldSize); win.Arrange(new Rect(new Point(0,0),oldSize));

提交回复
热议问题