Scale and fit to page for printing WPF page

Viewed 1089

I have a WPF page that I need to scale down so that it fits inside a A4 sheet. A lot of people have used the following approach :

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

Following this approach, the printer is still only able to print a section of the page.

I tried using RenderTransform on the complete page and tried printing it directly which again only printed a section of the page. If I save the WPF page as xps document after RenderTransform using Microsoft XPS Document printer, I get the correct output but this approach requires creating a new file everytime and printing it separately.

  1. How can I directly print my Wpf page with the proper scaling?
  2. single Wpf page with proper scaling takes about half the A4 page. How can print 2 copies of the same Wpf page on a single sheet.
0 Answers
Related