title: How to set the Page Size for an Xps Document?
body: <p>I am trying to save an XPS document with a <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.fixeddocument%28v=vs.90%29.aspx" rel="nofollow noreferrer">FixedDocument</a>. So far, I have failed to change the page size. <strong>How can I change it to a custom size?</strong></p>
  
  <p>I have based my code on <a href="https://stackoverflow.com/questions/502198/convert-wpf-xaml-control-to-xps-document">the first answer to this question</a>, more specifically, I am using this code:</p>
  
  <pre><code>        if (File.Exists(filename)) {
              File.Delete(filename);
          }
  
          var oldParent = LogicalTreeHelper.GetParent(this) as ContentControl;
          try {
              oldParent.Content = null;
  
              FixedDocument doc = new FixedDocument();
  
              PageContent pageCnt = new PageContent();
              FixedPage page = new FixedPage();
  
              page.Children.Add(this);
              try {
                  ((System.Windows.Markup.IAddChild)pageCnt).AddChild(page);
                  doc.Pages.Add(pageCnt);
  
                  XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.ReadWrite);
                  try {
                      var writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                      writer.Write(doc);
                  }
                  finally {
                      xpsDoc.Close();
                  }
              }
              finally {
                  page.Children.Clear();
              }
          }
          finally {
              ((ContentControl)oldParent).Content = this;
          }
  </code></pre>
  
  <p>It copies a user control into an XPS document and does so successfully, but, as I said, uses a default paper size.</p>
  
  <p>I have tried to use the <code>DocumentPaginator.PageSize</code> property to set a new page size (after instantiating <code>FixedDocument</code>), but whatever I assign to that property seems to be ignored; the page in the resulting XPS document retains its default paper size.</p>
  
  <p>When executing stepwise, I can see that the value of the <code>PageSize</code> property has really changed, so it's not like the new value somehow isn't accepted by the <code>DocumentPaginator</code>.</p>
  
  <p>I have found various online resources, none of which has solved my problem as yet:</p>
  
  <ul>
  <li><a href="http://social.msdn.microsoft.com/Forums/is/wpf/thread/d615c4b5-80fa-4eeb-808b-d79171b753e6" rel="nofollow noreferrer">This forum posting at MS Social</a> insists that setting the <code>PageSize</code> property works, but it does not as far as I can tell.</li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpaginator.pagesize%28v=vs.90%29.aspx" rel="nofollow noreferrer">The docs</a> claim that setting the <code>PageSize</code> property works and provide an example which does the same as what I've tried. (Other than that, based on this docs page I can't even tell the unit of the numbers to use.)</li>
  <li>The docs also point to the <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpage.size%28v=vs.90%29.aspx" rel="nofollow noreferrer"><code>DocumentPage.Size</code> property</a>, however that property cannot be publicly changed. Do I really have to override some page class before adding the page to the document just to get a different page size?</li>
  <li><a href="http://mindfusion.eu/Forum/YaBB.pl?board=wpfdg_disc;action=display;num=1295285990" rel="nofollow noreferrer">This forum posting</a> describes the same problem, but the answer seems nonsensical to me. I am using the <code>DocumentPaginator</code> property only ever once, so there is no "calling (...).DocumentPaginator again" for which I could save an instance.</li>
  <li><a href="https://stackoverflow.com/questions/9495914/fixeddocument-page-size">This question</a> sounds promising, but it is actually not about the page size, but about the scale of an image on a given page.</li>
  <li>Aside from the aforementioned <code>PageSize</code> property (which is set to what seems to be the default size anyway here), <a href="http://www.ericsink.com/wpf3d/B_Printing.html" rel="nofollow noreferrer">this tutorial</a> uses the <code>Width</code> and <code>Height</code> properties of a <code>FixedPage</code>. However, assigning some positive random values to these for a quick test would result in my XPS document apparently being corrupted and XPS Viewer displaying an error message when opening it.</li>
  </ul>