设置WPF PrintDialog的页面大小

时间:2015-10-28 15:35:53

标签: c# wpf printing

我试图从WPF应用程序打印,但很难设置默认页面大小。

我打印的内容应该是C5大小的信封,大多数打印机默认为A4纸。

我想向用户显示一个对话框,允许他们选择要使用的打印机 - 但如果他们只是按OK接受默认打印机,则默认为A4纸。

如何将作业的打印默认值设置为C5信封?
我仍然可以提示用户使用打印机吗?

private void PrintVisual_Sized(UIElement toPrint)
{
    PrintDialog dlg = new PrintDialog();
    PrintQueue queue = dlg.PrintQueue;

    // Get C5 page size if possible from printer
    var availPageSizes = queue.GetPrintCapabilities().PageMediaSizeCapability;
    PageMediaSize pageSize = Utilities.GetPageSize(availPageSizes, PageMediaSizeName.ISOC5Envelope);

    if (pageSize != null)
    {
        PrintTicket ticket = new PrintTicket
        {
            PageMediaSize = pageSize,
            InputBin = InputBin.AutoSelect,
            CopyCount = 1
        };

        dlg.UserPageRangeEnabled = false;

        var result = dlg.PrintQueue.MergeAndValidatePrintTicket(dlg.PrintTicket, ticket);
        Debug.Print(result.ConflictStatus.ToString());

        // Try to get the page size honoured by someone!!!
        dlg.PrintQueue.DefaultPrintTicket = result.ValidatedPrintTicket;
        dlg.PrintQueue.UserPrintTicket = result.ValidatedPrintTicket;
        dlg.PrintTicket = result.ValidatedPrintTicket;

        // Height still seems to be A4 sized!?
        Debug.Print("Height: " + dlg.PrintableAreaHeight);
    }

    // Ask user which printer they want...
    if (dlg.ShowDialog().GetValueOrDefault(false))
    {
        Size printSize = new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight);
        toPrint.Measure(printSize);
        toPrint.Arrange(new Rect(new Point(), printSize));
        toPrint.UpdateLayout();

        dlg.PrintVisual(toPrint, "My Print Job");
    }
}

在最后一部分printSize是A4,除非用户手动选择其他纸张尺寸 有没有办法显示对话框,预设非默认页面大小?

0 个答案:

没有答案