WPF打印到适合页面适合、页面、WPF

2023-09-04 01:19:27 作者:ゝ时间不停伴你永恒°

我搜索选项如何打印WPF控件,并发现了一些解决方案。我确实需要适合我的打印控制,打印页面,而preserving长宽比(我的控制是方形的;数独电网)。

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 square; sudoku grid).

我找到了一个解决方案,重新调整和重新定位控制,以适应页面。这工作得很好,但它也重新定位在我的窗口控制。

I found a solution that resizes and repositions control to fit a page. That works well, but it also repositions that control on my window.

这里是codeI用于打印和缩放:

here is the code i use for print and scaling :

        //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());

我尝试了两种aproaches来解决这个问题:

I tried two aproaches to solve this:

克隆我的控制,然后转化克隆之一,未影响原。 没有工作,由于某种原因,我与异常结束:所提供的DependencyObject不是一个上下文这可冻结,但奇怪的是只在某些情况下,

Clone my control and then transform cloned one, unaffecting original. Didnt work, for some reason i ended with exception: The provided DependencyObject is not a context for this Freezable, but oddly only in some cases.

还原大小和位置的变化。我打过电话InvalidateArrange()方法,这似乎工作,但只在印刷方法的第一个电话。在第二个电话,就没有工作。

Revert size and position changes. I tried calling InvalidateArrange() method, which seemed to work, but only during first call of print method. During second call, it didnt work.

我应该怎么做,请,任何想法<谢谢你。

What should i do please, any ideas< thank you.

推荐答案

我知道这个问题是很老,但寻找一个解决这个问题我自己。这是我目前使用的解决方案。我对存储框架元件原始的变换,然后重新应用后的印刷已经完成。

I know this question is quite old but looking for a solution to this problem myself. Here is the solution I am currently using. I store the original transformation against the framework element and then reapply it after the printing has finished.

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