【问题标题】:WPF to XPS in landscape orientation横向 WPF 到 XPS
【发布时间】:2011-01-30 09:32:32
【问题描述】:

我正在尝试从 WPF 控件生成 XPS 文档。到目前为止打印工作正常,但我找不到在横向模式下创建 XPS 的方法。

我创建 XPS 文件的代码,大部分来自另一个 SO 页面

    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();
        }
    }

感谢任何帮助。

【问题讨论】:

    标签: c# wpf orientation xps landscape


    【解决方案1】:

    尝试在 ReturnFixedDoc 中设置 FixedPage 的大小:

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

    数字的格式为(英寸)x(每英寸点数)。 96 是 WPF 的 DPI。我使用了 A4 页面的尺寸。

    【讨论】:

    • 谢谢,这似乎可以解决问题。我使用“new RotateTransform(90)”来旋转控件,但将页面调整到适当的尺寸更好:-)
    猜你喜欢
    • 1970-01-01
    • 2015-05-07
    • 2020-05-10
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2012-11-30
    • 2015-03-27
    • 2012-12-05
    相关资源
    最近更新 更多