WPF 打印以适合页面 [英] WPF Printing to fit page
问题描述
我搜索了如何打印 WPF 控件的选项并找到了一些解决方案.我确实需要使我的打印控件适合打印页面,同时保留纵横比(我的控件是方形的;数独网格).
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.
这是我用于打印和缩放的代码:
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());
我尝试了两种方法来解决这个问题:
I tried two aproaches to solve this:
克隆我的控件,然后转换克隆的控件,不影响原始控件.没有工作,出于某种原因,我以异常结束:提供的 DependencyObject 不是此 Freezable 的上下文,但奇怪的是仅在某些情况下.
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() 方法,该方法似乎有效,但仅在第一次调用 print 方法期间.在第二次通话中,它没有工作.
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;
}
}
这篇关于WPF 打印以适合页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!