title: WPF Printing to fit page
body: <p>i searched for options how to print WPF controls and found some solutions. I do need to fit my printed control to printing page while preserving aspect ration (my control is square; sudoku grid).</p>
  
  <p>I found a solution that resizes and repositions control to fit a page. That works well, but it also repositions that control on my window.</p>
  
  <p>here is the code i use for print and scaling :</p>
  
  <pre><code>        //get selected printer capabilities
              System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
          //get scale of the print wrt to screen of WPF visual
          double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / mrizka.ActualWidth, capabilities.PageImageableArea.ExtentHeight / mrizka.ActualHeight);
  
          //Transform the Visual to scale
          mrizka.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.
          mrizka.Measure(sz);
          mrizka.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
  
          dialog.PrintVisual(mrizka, mrizka.getID().ToString());
  </code></pre>
  
  <p>I tried two aproaches to solve this:</p>
  
  <ol>
  <li><p>Clone my control and then transform cloned one, unaffecting original.
  Didnt work, for some reason i ended with exception: The provided DependencyObject is not a context for this Freezable, but oddly only in some cases.</p></li>
  <li><p>Revert size and position changes. I tried calling InvalidateArrange() method, which seemed to work, but only during first call of print method. During second call, it didnt work.</p></li>
  </ol>
  
  <p>What should i do please, any ideas&lt; thank you.</p>