c# - PrintDialog/XPS Document Writer 中忽略的纸张大小

标签 c# wpf printing xps printdialog

我正在尝试使用 WPF 的 PrintDialog 进行打印类(PresentationFramework.dll 中的命名空间 System.Windows.Controls,v4.0.30319)。这是我使用的代码:

private void PrintMe()
{
    var dlg = new PrintDialog();

    if (dlg.ShowDialog() == true)
    {
        dlg.PrintVisual(new System.Windows.Shapes.Rectangle
        {
            Width = 100,
            Height = 100,
            Fill = System.Windows.Media.Brushes.Red
        }, "test");
    }
}

问题是无论我为“Microsoft XPS Document Writer”选择什么纸张尺寸,生成的 XPS 始终具有“Letter”纸张类型的宽度和高度:

这是我可以在 XPS 包中找到的 XAML 代码:

<FixedPage ... Width="816" Height="1056">

最佳答案

在打印对话框中更改纸张大小只会影响 PrintTicket,不会影响 FixedPage 内容。 PrintVisual 方法生成 Letter 大小的页面,因此为了获得不同的页面大小,您需要使用 PrintDocument 方法,如下所示:

private void PrintMe()
{
    var dlg = new PrintDialog();
    FixedPage fp = new FixedPage();
    fp.Height = 100;
    fp.Width = 100;
    fp.Children.Add(new System.Windows.Shapes.Rectangle
        {
            Width = 100,
            Height = 100,
            Fill = System.Windows.Media.Brushes.Red
        });
    PageContent pc = new PageContent();
    pc.Child = fp;
    FixedDocument fd = new FixedDocument();
    fd.Pages.Add(pc);
    DocumentReference dr = new DocumentReference();
    dr.SetDocument(fd);
    FixedDocumentSequence fds = new FixedDocumentSequence();
    fds.References.Add(dr);            

    if (dlg.ShowDialog() == true)
    {
        dlg.PrintDocument(fds.DocumentPaginator, "test");
    }
}

关于c# - PrintDialog/XPS Document Writer 中忽略的纸张大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7390818/

相关文章:

java - 无法对非静态字段进行静态引用

c# - wpf、c#、viewport3D 的 renderTargetBitmap 没有将其分配给窗口

c# - 安装 .NET 开发的 Windows 服务时出错

java - 在java中的同一行中打印

swift - 如何将热敏蓝牙打印机连接到 iOS 设备

WPF 以编程方式实例化用户控件以将其呈现为 PNG

c# - asp.net identity 获取登录用户的所有角色

c# - 像 Facebook 评论一样格式化 C# 元素符号列表

.net - 从区域中删除 View 时内存泄漏

c# - 为什么 InvokeRequired 和 Dispatcher.CheckAccess 的 bool 值颠倒?