title: Print XpsDocument scaled to fit page
body: <p>We are using Telerik's WPF charting controls in our application. The charting controls have built-in support for exporting the graphics to an XPS file. I want to utilize WPF's support for printing XPS documents to print the chart. For the most part, this works great, but I'm having trouble scaling the chart fit into one page. The chart graphic is always cut off on the right and bottom. The chart is coming out too large for the page. Any ideas? </p>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>const string packageUriString = "pack://inMemoryChart.xps";
  
  using (var memoryStream = new MemoryStream())
  {
      //utilize Telerik ChartView method to export to XPS... 
      _chart.ExportToXps(memoryStream);
  
      using (var package = Package.Open(memoryStream))
      {
          var packageUri = new Uri(packageUriString);
  
          PackageStore.AddPackage(packageUri, package);
  
          var xpsDocument = new XpsDocument(package, CompressionOption.Maximum, packageUriString);
  
          var printDialog = new PrintDialog();
          var result = printDialog.ShowDialog();
  
          if (!result.GetValueOrDefault())
              return;
  
          var fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
  
          if (fixedDocumentSequence == null)
              return;
  
          var documentPaginator = fixedDocumentSequence.DocumentPaginator;
  
          if (documentPaginator == null)
              return;
  
          //attempt to adjust to fit page?
          AdjustFlowDocumentToPage(documentPaginator, printDialog.PrintQueue); 
  
          printDialog.PrintDocument(documentPaginator , "Chart");
  
          PackageStore.RemovePackage(packageUri);
      }
  }
  </code></pre>
  
  <p>I found the AdjustFlowDocumentToPage on another <a href="http://roecode.wordpress.com/category/c/page/4/" rel="nofollow">site</a>:</p>
  
  <pre><code>public static void AdjustFlowDocumentToPage(DocumentPaginator documentPaginator, PrintQueue printQueue)
  {
     const double inch = 96;
  
     var printTicket = printQueue.UserPrintTicket;
  
     // Get the media size.
     double width = printTicket.PageMediaSize.Width.Value;
     double height = printTicket.PageMediaSize.Height.Value;
  
     // Set the margins.
     const double leftmargin = 1.25 * inch;
     const double rightmargin = 1.25 * inch;
     const double topmargin = 1 * inch;
     const double bottommargin = 1 * inch;
  
     // Calculate the content size.
     var contentwidth = width - leftmargin - rightmargin;
     var contentheight = height - topmargin - bottommargin;
  
     documentPaginator.PageSize = new Size(contentwidth, contentheight);
  }
  </code></pre>