Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 849063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:02:39+00:00 2026-05-15T07:02:39+00:00

I am able to print the current Window using the following code: PrintDialog printDialog

  • 0

I am able to print the current Window using the following code:

PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog().GetValueOrDefault(false))
{
    printDialog.PrintVisual(this, this.Title); 
}

However if the Window does not fit the page it get truncated.
How do I make the Window fit the Page ?
I guess I need to make a graphics element first and check if this graphics fits the page, but I have found nothing so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:02:40+00:00Added an answer on May 15, 2026 at 7:02 am

    There is one solution out there that lots of people are reposting as their own. It can be found here:

    http://www.a2zdotnet.com/View.aspx?id=66

    The problem w/ that is that it does resize your UI. So this next link takes the previous solution and resizes back to the original size when it’s done. This does work, although I can’t help but to think there’s likely a more elegant solution out there somewhere:

    http://www.slickthought.net/post/2009/05/26/Visual-Tree-Printing-in-WPF-Applications.aspx


    Slickthought.net domain is defunct. Wayback Machine to the rescue.

    https://web.archive.org/web/20130603071346/http://www.slickthought.net/post/2009/05/26/Visual-Tree-Printing-in-WPF-Applications.aspx


    <Button Content="Print" Command="{Binding Path=PrintCommand}" CommandParameter="{Binding ElementName=ReportPanel}"></Button>
    

    There are two important things to note here. First, I am using a WPF command to start the printing process. You don’t have to do it this way, but it lets me tie the presenter to the UI pretty cleanly. The second thing is the CommandParameter. It is passing in a reference to the the ReportPanel. ReportPanel is just a WPF Grid control that wraps the title TextBlock and a Listbox that contains the actual charts. The simplified XAML is:

    <Grid x:Name="ReportPanel" > 
        <Grid.RowDefinitions> 
            <RowDefinition Height="Auto" /> 
            <RowDefinition Height="*" /> 
        </Grid.RowDefinitions> 
        <TextBlock /> 
        <ListBox/>
    </Grid>
    

    With that UI established, lets jump to the code. When the user clicks the Print button, the following WPF command is executed:

    this.PrintCommand = new SimpleCommand<Grid> 
    { 
        CanExecuteDelegate = execute => true, 
        ExecuteDelegate = grid => 
            { 
                PrintCharts(grid); 
            } 
    };
    

    This is pretty simple stuff. SimpleCommand implements the ICommand interface and lets me pass in some lambda expressions defining the code I want to run when this command is fired. Clearly, the magic happens in the PrintCharts(grid) call. The code shown below is basically the same code you would find in Pankaj’s article with a couple of modification highlighted in red.

    private void PrintCharts(Grid grid) 
    { 
      PrintDialog print = new PrintDialog(); 
      if (print.ShowDialog() == true) 
      { 
          PrintCapabilities capabilities = print.PrintQueue.GetPrintCapabilities(print.PrintTicket); 
    
          double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / grid.ActualWidth, 
                                  capabilities.PageImageableArea.ExtentHeight / grid.ActualHeight); 
    
          Transform oldTransform = grid.LayoutTransform; 
    
          grid.LayoutTransform = new ScaleTransform(scale, scale); 
    
          Size oldSize = new Size(grid.ActualWidth, grid.ActualHeight); 
          Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); 
          grid.Measure(sz); 
          ((UIElement)grid).Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), 
              sz)); 
    
          print.PrintVisual(grid, "Print Results"); 
          grid.LayoutTransform = oldTransform; 
          grid.Measure(oldSize); 
    
          ((UIElement)grid).Arrange(new Rect(new Point(0, 0), 
              oldSize)); 
      } 
    }
    

    All right, what are these modifications? The most obvious is that I am replacing the use of the original this object (which represented the entire application window in the original code) with the Grid control that was passed in as part of the Command. So all of the measurements and transforms are executed using the Grid. The other change is that I have save the original Transform and Size of the Grid as well. The reason is that when you transform the Grid to fit to the printing page, it causes the actual application UI to change as well. This doesn’t look so good on your screen, so after sending the Grid to the printer, I transform it back to its original screen layout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to be able to print the definition code of a lambda function.
Can a Java Applet able to print out text/html easily to standard printer driver(s)
I seem to remember being able to print out (or locate) the specific switches
I'm using the Google Maps API v2 and I'd like to be able to
I am using below function to make a image of current form and set
The point of const-correctness is to be able to provide a view of an
Application able to record error in OnError, but we are not able to do
anyone able to tell me how often a materialized view is set to refresh
You should be able to create a generic form: public partial class MyGenericForm<T> :
I'm able to connect to and read an excel file no problem. But when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.