title: How to calculate FixedPage dimensions
body: <p><em>This snippet is part of some code used to generate an XPS document.  XPS document generation is no joke.  I wish to avoid pasting any of that XPS code here if at all possible.  Instead, this code focuses on the WPF portion of the problem.</em>  </p>
  
  <p>The problem I am asking for you to help with is here.  I have hard coded the dimensions to work for a test image:</p>
  
  <pre><code>double magicNumber_X = 3.5;//trial and error...3 too small 4 too big
  fixedPage.Arrange(new Rect(new Point(magicNumber_X, 0), size));
  </code></pre>
  
  <p>Instead, how can I fix this code to calculate the coordinates?</p>
  
  <p>Full Method:</p>
  
  <pre><code>    private PageContent AddContentFromImage()
      {
          var pageContent = new PageContent();
          var fixedPage = new FixedPage();            
  
          var bitmapImage = new BitmapImage(new Uri(hardCodedImageSampleFilePath, UriKind.RelativeOrAbsolute));
  
          var image = new Image();
          image.Source = bitmapImage;            
          fixedPage.Children.Add(image);
  
          ((IAddChild)pageContent).AddChild(fixedPage);
  
          double pageWidth = 96 * 8.5;//XPS documents are 96 units per inch
          double pageHeight = 96 * 11;
  
          fixedPage.Width = pageWidth;
          fixedPage.Height = pageHeight;
  
          var size = new Size(8.5 * 96, 11 * 96);
  
          fixedPage.Measure(size);
          double magicNumber_X = 3.5;//trial and error...3 too small 4 too big
          double magicNumber_Y = 0;
          fixedPage.Arrange(new Rect(new Point(magicNumber_X, magicNumber_Y), size));
          fixedPage.UpdateLayout();
  
          return pageContent;
      }
  </code></pre>
  
  <p>I'm a little surprised <code>FixedPage.Measure(size)</code> does not correct the issue by itself.  I tried passing no params, e.g. <code>fixedPage.Arrange(new Rect(), size))</code> still no go.  </p>
  
  <p>FWIW, this calculation worked fine when I was using PrintDocument.</p>
  
  <pre><code>private void pd_PrintPage(object sender, PrintPageEventArgs e)
      {
          Graphics g = e.Graphics;
          RectangleF marginBounds = e.MarginBounds;
          RectangleF printableArea = e.PageSettings.PrintableArea;
  
          int availableWidth = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width));
          int availableHeight = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height));
  
          Rectangle rectangle = new Rectangle(0,0, availableWidth -1, availableHeight - 1);        
          g.DrawImage(_image, rectangle);
  </code></pre>