WPF to XPS in landscape orientation

Felix picture Felix · Mar 22, 2010 · Viewed 8.8k times · Source

i am trying to to generate a XPS Document from a WPF Control. Printing works so far, but i cannot find a way to create the XPS in landscape mode.

My code to create the XPS file, mostly taken from another SO page

    public FixedDocument ReturnFixedDoc()
    {
        FixedDocument fixedDoc = new FixedDocument();
        PageContent pageContent = new PageContent();
        FixedPage fixedPage = new FixedPage();

        var ctrl = new controlToPrint();

        //Create first page of document
        fixedPage.Children.Add(ctrl);
        ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
        fixedDoc.Pages.Add(pageContent);
        //Create any other required pages here

        return fixedDoc;
    }


    public void SaveCurrentDocument()
    {
        // Configure save file dialog box
        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = "MyReport"; // Default file name
        dlg.DefaultExt = ".xps"; // Default file extension
        dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension

        // Show save file dialog box
        Nullable<bool> result = dlg.ShowDialog();

        // Process save file dialog box results
        if (result == true)
        {
            // Save document
            string filename = dlg.FileName;

            FixedDocument doc = ReturnFixedDoc();
            XpsDocument xpsd = new XpsDocument(filename, FileAccess.Write);
            System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
            xw.Write(doc);
            xpsd.Close();
        }
    }

Any help is appreciated.

Answer

b-rad picture b-rad · Mar 23, 2010

Try setting the size of your FixedPage in ReturnFixedDoc:

// hard coded for A4
fixedPage.Width = 11.69 * 96;
fixedPage.Height = 8.27 * 96;

The numbers are in the form (inches) x (dots per inch). 96 is the DPI of WPF. I have used the dimensions of an A4 page.