横向 WPF 到 XPS

发布于 2024-08-26 06:43:31 字数 1581 浏览 14 评论 0原文

我正在尝试从 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();
        }
    }

任何帮助都是值得赞赏的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缺⑴份安定 2024-09-02 06:43:31

尝试在 ReturnFixedDoc 中设置固定页面的大小:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文