.net WPF打印以适应页面

.net WPF打印以适应页面,.net,wpf,printing,.net,Wpf,Printing,我搜索了如何打印WPF控件的选项,并找到了一些解决方案。我确实需要在保持纵横比的同时将打印控件与打印页面相匹配(我的控件是正方形;数独网格) 我找到了一个解决方案,可以调整控件的大小和位置以适应页面。这很好,但它也可以在我的窗口上重新定位该控件 以下是我用于打印和缩放的代码: //get selected printer capabilities System.Printing.PrintCapabilities capabilities = dialog

我搜索了如何打印WPF控件的选项,并找到了一些解决方案。我确实需要在保持纵横比的同时将打印控件与打印页面相匹配(我的控件是正方形;数独网格)

我找到了一个解决方案,可以调整控件的大小和位置以适应页面。这很好,但它也可以在我的窗口上重新定位该控件

以下是我用于打印和缩放的代码:

        //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
        //get scale of the print wrt to screen of WPF visual
        double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / mrizka.ActualWidth, capabilities.PageImageableArea.ExtentHeight / mrizka.ActualHeight);

        //Transform the Visual to scale
        mrizka.LayoutTransform = new ScaleTransform(scale, scale);

        //get the size of the printer page
        Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

        //update the layout of the visual to the printer page size.
        mrizka.Measure(sz);
        mrizka.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

        dialog.PrintVisual(mrizka, mrizka.getID().ToString());
我尝试了两种方法来解决这个问题:

  • 克隆我的控件,然后转换克隆的控件,不影响原始控件。 不起作用,出于某种原因,我以例外结束:提供的DependencyObject不是这个Freezable的上下文,但奇怪的是,只有在某些情况下

  • 恢复大小和位置更改。我尝试调用InvalidateArrange()方法,这似乎有效,但仅在第一次调用print方法时有效。在第二次通话中,它不起作用


  • 我该怎么办,有什么想法吗?<谢谢。

    我知道这个问题很老了,但我自己正在寻找解决这个问题的办法。这是我目前使用的解决方案。我针对框架元素存储原始转换,然后在打印完成后重新应用它

        private void Print(Visual v)
        {
    
            System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement ;
            if (e == null)
                return;
    
            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() == true)
            {
                //store original scale
                Transform originalScale = e.LayoutTransform;
                //get selected printer capabilities
                System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
    
                //get scale of the print wrt to screen of WPF visual
                double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / e.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                               e.ActualHeight);
    
                //Transform the Visual to scale
                e.LayoutTransform = new ScaleTransform(scale, scale);
    
                //get the size of the printer page
                System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
    
                //update the layout of the visual to the printer page size.
                e.Measure(sz);
                e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
    
                //now print the visual to printer to fit on the one page.
                pd.PrintVisual(v, "My Print");
    
                //apply the original transform.
                e.LayoutTransform = originalScale;
            }
        }
    
    这是对用户1018711提出的问题的回答。使用C#和WPF在一个打印机页面上安装打印输出。当您想要打印一个可视控件时,它可能是一个控件,可能包括许多控件(例如按钮、数据网格、文本块、标签等)。这里我想将名为song2Drid的数据网格打印到打印机上,但其内容大于打印机的页面大小(其宽度大于纸张宽度),所以被截断。我看不到所有这些,所以我必须缩放视觉效果,但我想保持比例与旧的一样

    我还将纸张的边距设置为纸张两侧各0.75英寸,包括左侧、顶部、右侧和底部。我还将visual(song2Grid)的内容集中在纸上。所以我可以在纸的中央看到印刷的内容。但是,如果该可视化对象是类似于窗口的Application.Current.MainWindow或由new window()以编程方式创建的任何窗口,则不会对其进行缩放。这意味着此方法不适用于窗口对象

    另外,如果您想通过缩放到打印来恢复屏幕上的原始外观,那么您必须有如下语句
    win.LayoutTransform=旧LayoutTransform;win.Measure(旧尺寸);win.Arrange(新矩形(新点(0,0),旧尺寸))

    您需要添加对System.Printing和ReachFramework的引用。它似乎还引用了System.Windows.Media和System.Windows.Controls。这对我来说不正常,无论我对布局转换进行何种转换,它似乎都会被忽略。
        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));
            }
        }