WPF PrintVisual,整页

时间:2015-12-21 19:39:53

标签: c# wpf printing

我想打印我的网格,这是代码:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
        printDlg.PrintVisual(DienstBereich, "Grid Printing.");
    }

但是现在:我希望网格在整个页面上,并且它应该是高大小的。 有可能吗?

感谢所有帮助者! :)

1 个答案:

答案 0 :(得分:0)

我正在使用MVVM,但这可以为您提供帮助

using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

private void Imprimir(object view)
{
    var fe = new Grid();
    fe = (Grid)view;
    var feSize = new Size(((Grid)view).Width, ((Grid)view).Height); //for resize back to original size

    try
    {
        var dialog = new PrintDialog();
        dialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        dialog.PrintTicket = dialog.PrintQueue.DefaultPrintTicket;
        dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
        var ret = dialog.ShowDialog() ?? false;
        if (!ret)
           return;
        //Get the capabilities of the selected printer.
        var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);

        //Here my view is resized  
        fe.Height = capabilities.PageImageableArea.ExtentHeight;
        fe.Width = capabilities.PageImageableArea.ExtentWidth;
        fe.UpdateLayout();

        fe.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        Size size = new Size(fe.DesiredSize.Width, fe.DesiredSize.Height);
        fe.Measure(size);
        size = new Size(capabilities.PageImageableArea.ExtentWidth,
                         capabilities.PageImageableArea.ExtentHeight);
        fe.Measure(size);
        fe.Arrange(new Rect(size));

        dialog.PrintVisual(fe, "Grid Printing");
    }
    finally
    {
        //Here I resize my view to the original size
        fe.Height = feSize.Height;
        fe.Width = feSize.Width;
        fe.UpdateLayout();

        fe.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        Size size = new Size(fe.Width, fe.Height);
        fe.Measure(size);
        fe.Arrange(new Rect(size));
    }

}