2

我正在尝试使用 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">

4

1 回答 1

2

在打印对话框中更改纸张大小只会影响 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");
    }
}
于 2011-09-15T04:32:06.220 回答