Skip Navigation LinksHome > Articles > WPF > How to Print in WPF to Fit to printing page.

How to Print in WPF to Fit to printing page.

It explains WPF printing and to fit the visual element to the selected printing page.

By Pankaj   On   Sunday, 10 August 2008

Page Views : 12135   |   Technologies : WPF

Rating : Rated :
0

Printing WPF window(visual) to printer and fit on a page.

 

1 . Normal Printing:

Printing in WPF is easy as compare to traditional window printing. You need to show the PrintDialog and call the PrintVisual mehod of the PrintDialog. This example has been shown in the btnPrint_OnClick event handler.

 

 PrintDialog printDlg = new System.Windows.Controls.PrintDialog();

if (printDlg.ShowDialog() == true)

{

      printDlg.PrintVisual(this, "First WPF Print");

}

 

 

2. Print to Fit Window:

Now if you want to fit your visual to fit into the print page then you have to do little more coding.

 

  • Add Reference the ReachFramework.dll.
  • Add reference of the System.Printing.dll.
  • Get the capabilities of the selected printer.
  • Calculate the scaling of the printer with w.r.t. to visual to be printed.
  • Transform the visual to be printed to the calculated scale.
  • Get the printable area of the paper size.
  • Update the layout of the visual to the printable area.
  • Print the visual.

 

 

Code: This code in the sample is called in the btnPrintFit_OnClick handler.

 

PrintDialog printDlg = new System.Windows.Controls.PrintDialog();

if (printDlg.ShowDialog() == true)

   {

      //get selected printer capabilities

      System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);

 

     //get scale of the print wrt to screen of WPF visual

     double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight /

                    this.ActualHeight);

 

     //Transform the Visual to scale

     this.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.

     this.Measure(sz);

     this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

 

      //now print the visual to printer to fit on the one page.

      printDlg.PrintVisual(this, "First Fit to Page WPF Print");

 

}

 

 

Summary: Using WPF you can print any visual element in the normal way or to fit on the page easily.

 


Keywords :
Tags :
Rate This Article :

Comments :

# 1 Annonymous Wrote on 10/29/2008


Thanks Pankaj.... It's very nice and very useful for me.... Regards, Arun..



# 2 Annonymous Wrote on 10/31/2008


Very helpful -Serkan



# 3 Annonymous Wrote on 12/05/2008


what if you are printing from a image viewer?



# 4 Annonymous Wrote on 02/19/2009


Thank you! To preserve the aspect ratio, don't Arrange using the page size. This is what I ended up with: //get the size of the printer page PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); double pageMargin = Math.Min(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight); double additionalMargin = Math.Max(0, minMargin - pageMargin); Size pageSize = new Size( capabilities.PageImageableArea.ExtentWidth - additionalMargin * 2, capabilities.PageImageableArea.ExtentHeight - additionalMargin * 2); //get scale of the print wrt to screen of WPF visual double scale = Math.Min( pageSize.Width / visual.ActualWidth, pageSize.Height / visual.ActualHeight); Size printSize = new Size( visual.ActualWidth * scale, visual.ActualHeight * scale); Point upperLeft = new Point( capabilities.PageImageableArea.OriginWidth + additionalMargin, capabilities.PageImageableArea.OriginHeight + additionalMargin); //Transform the Visual to scale visual.LayoutTransform = new ScaleTransform(scale, scale); visual.Measure(printSize); visual.Arrange(new Rect(upperLeft, printSize)); printDlg.PrintVisual(visual, description);



# 5 Annonymous Wrote on 03/23/2009


Is there any possibility to remove controls and display only the values of that controls when printing. Thanks,



# 6 Annonymous Wrote on 04/23/2009


Very helpful! @anonymous : use the FlowDocuments ;)



# 7 Annonymous Wrote on 06/26/2009


Any way to do print to screen, but not change the way the UI looks??



# 8 Annonymous Wrote on 07/02/2009


capture the image of the screen and print that screen would change the effect on page. it is explained in http://www.a2zdotnet.com/View.aspx?id=67



# 9 Annonymous Wrote on 12/03/2009


Hi, Thanks for the information. my requirement is like this, I have one DataGrid and print button.DataGrid have columns with Imagename and imagepath.When i click on Print button,I want to print Image name with first page of the image from loading image. am beginer in WPF. how can do this by using WPF? thanks in advance



Write a Comment / Question / Feedback ...


User Login
Username :
Password :
Register Login

Forgot Password


Related Articles