我搜索了如何打印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方法时才有效。在第二次通话期间,它没有用。
我应该做什么,任何想法<谢谢。
答案 0 :(得分:22)
我知道这个问题很老,但我自己也在寻找解决这个问题的方法。这是我目前使用的解决方案。我将原始转换存储在框架元素中,然后在打印完成后重新应用它。
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;
}
}
答案 1 :(得分:1)
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));
}
}
这是对user1018711提出的问题的回答。使用C#和WPF在一个打印机页面上打印。当你想要打印出一个视觉效果时,它可能是一个包含许多控件的控件(例如Button,DataGrid,TextBlock,Label等)。在这里,我想将一个名为song2Drid的DataGrid打印到打印机,但其内容大于打印机的页面大小(其宽度比纸张的宽度宽),因此它被截断。我无法看到所有这些,所以我不得不缩放视觉,但我想保持比例与旧的相同。
我还将纸张边距设置为0.75英寸,纸张的左侧,顶部,右侧和底部。我还将视觉内容(song2Grid)集中在纸上。所以我可以在纸张的中心看到打印的内容。但是如果visual是一个类似Application.Current.MainWindow的窗口或者是由新的Window()以编程方式创建的窗口,那么它将不会被缩放。这意味着这个方法不适用于Window对象。
此外,如果您想要通过缩放打印来恢复屏幕上的原始外观,那么您必须具有以下语句: win.LayoutTransform = oldLayoutTransform; win.Measure(oldSize); win.Arrange(new Rect(new Point(0,0),oldSize));