How to set the Page Size for an Xps Document?

7.2k views Asked by At

I am trying to save an XPS document with a FixedDocument. So far, I have failed to change the page size. How can I change it to a custom size?

I have based my code on the first answer to this question, more specifically, I am using this 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;
        }

It copies a user control into an XPS document and does so successfully, but, as I said, uses a default paper size.

I have tried to use the DocumentPaginator.PageSize property to set a new page size (after instantiating FixedDocument), but whatever I assign to that property seems to be ignored; the page in the resulting XPS document retains its default paper size.

When executing stepwise, I can see that the value of the PageSize property has really changed, so it's not like the new value somehow isn't accepted by the DocumentPaginator.

I have found various online resources, none of which has solved my problem as yet:

  • This forum posting at MS Social insists that setting the PageSize property works, but it does not as far as I can tell.
  • The docs claim that setting the PageSize 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.)
  • The docs also point to the DocumentPage.Size property, 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?
  • This forum posting describes the same problem, but the answer seems nonsensical to me. I am using the DocumentPaginator property only ever once, so there is no "calling (...).DocumentPaginator again" for which I could save an instance.
  • This question sounds promising, but it is actually not about the page size, but about the scale of an image on a given page.
  • Aside from the aforementioned PageSize property (which is set to what seems to be the default size anyway here), this tutorial uses the Width and Height properties of a FixedPage. 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.
2

There are 2 answers

0
Monstieur On

I believe a FixedDocument will only print at the size of its pages. Even when loading a FixedDocument into a DocumentViewer, changing the printer settings' page size when you click the print button will have no effect. A FixedDocument by its very definition preserves the fidelity of the its contents exactly.

The only way to modify it is to create a derived DocumentPaginator which calls the FixedDocument.DocumentPaginator's functions internally and modifies the return values accordingly.

0
user1788092 On

FixedDocuments have fixed pages. The height and width of FixedPage can be controlled. Somewhat like this:

        FixedPage pageOne = new FixedPage();
        pageOne.Height = 20;
        pageOne.Width = 10;

or in XAML:

Height="20" Width="10"