Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Printing in WPF with fit to printing page.

Posted by Viral Sarvaiya on May 17, 2011

Here is the code for the print in WPF.

It is very simple to print a page in comparison with normal window application. Just make a object of the PrintDialog Class and just call function PrintVisual as below

PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
printDlg.PrintVisual(this, "First WPF Print");

This code will print the page. That will take automatically margin to the printing page

What if you want to print page with fit to the printing page?

Here are the steps to print page with fit to printing page.

Step 1: Add the reference of the ReachFramework.dll
Step 2: Add the reference of the System.Printing.dll
Step 3: Get selected Printer’s capability
Step 4: calculate scale of the printer
Step 5: Get printable area of Paper.
Step 6: Update visual layout
Step 7: Print Visual
Here is the code for the above procedure

private void btnPrint_Click(object sender, RoutedEventArgs e)
PrintDialog Objprint = new System.Windows.Controls.PrintDialog();
if (Objprint.ShowDialog() == true)
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = Objprint.PrintQueue.GetPrintCapabilities(Objprint.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 size = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

//update the layout of the visual to the printer page size.
this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), size));

//now print the visual to printer to fit on the one page.
Objprint.PrintVisual(this, "Print in WPF with fit to printing page");



2 Responses to “Printing in WPF with fit to printing page.”

  1. […] Printing in WPF with fit to printing page. […]

  2. Prabha938 said

    Is this code helps to print the aspx to print in only one page. Bcoz, I have an aspx page which is binded with gridview and some labels. As the data binded in gridview increases the page height increasing.If i take print it is resulting two pages.My requirement is to fit the entire data to single page on taking print. Please help me to do this task..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: