2

WPF のPrintDialogクラス (PresentationFramework.dll、v4.0.30319 の名前空間 System.Windows.Controls) で印刷しようとしています。これは私が使用するコードです:

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 の幅と高さは常に「レター」用紙タイプです。

これは、XPS パッケージ内にある XAML コードです。

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

4

1 に答える 1

2

印刷ダイアログで用紙サイズを変更すると、FixedPageコンテンツではなく、PrintTicketにのみ影響します。PrintVisualメソッドはレターサイズのページを生成するため、ページサイズを変えるには、次のように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 に答える