我有一个WPF页面,我需要缩小它以便它适合A4纸张。 很多人使用了以下方法:
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.FrameworkElement scaled = report.gridReport as System.Windows.FrameworkElement;
if (e == null)
return;
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == true)
{
//store original scale
Transform originalScale = scaled.LayoutTransform;
//get selected printer capabilities
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 / scaled.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
scaled.ActualHeight);
//Transform the Visual to scale
scaled.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.
scaled.Measure(sz);
scaled.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
//now print the visual to printer to fit on the one page.
pd.PrintVisual(scaled, "My Print");
//apply the original transform.
scaled.LayoutTransform = originalScale;
}
}
按照这种方法,打印机仍然只能打印页面的一部分。
我尝试在整个页面上使用RenderTransform并尝试直接打印它,这再次只打印了页面的一部分。如果我使用Microsoft XPS Document打印机在RenderTransform之后将WPF页面保存为xps文档,我会得到正确的输出,但这种方法需要每次创建一个新文件并单独打印。