.Net 4.5 wpf 打印问题,纸张大小始终为 NorthAmericaLetter

.Net 4.5 wpf printing issue, paper size is always NorthAmericaLetter

我在 WPF 中打印时遇到问题。在 .Net 4.5 中,它始终以纸张大小 NorthAmericaLetter 打印。在 .Net Framework 3.5 中,它打印在我在代码中定义的正确纸张尺寸上。我在相同的解决方案中使用相同的代码进行了尝试,只是更改了目标框架。

我的问题是,我是否遗漏了 PrintTicket 配置中的某些内容,或者我如何才能在 .Net 4.5 中正确设置纸张尺寸?

var visual = new DrawingVisual();

using (var context = visual.RenderOpen())
{
    context.DrawRectangle(Brushes.CadetBlue, new Pen(Brushes.Black, 2),
        new Rect(new Point(0, 0), new Size(793, 1122)));
    context.DrawRectangle(Brushes.Brown, new Pen(Brushes.Black, 2),
        new Rect(new Point(20, 20), new Size(40, 40)));
    context.DrawRectangle(Brushes.Brown, new Pen(Brushes.Black, 2),
        new Rect(new Point(20, 257), new Size(40, 40)));
}


var queue = new LocalPrintServer().DefaultPrintQueue;

queue.UserPrintTicket.PageMediaSize = queue
    .GetPrintCapabilities()
    .PageMediaSizeCapability
    .Single(x => x.PageMediaSizeName == PageMediaSizeName.ISOA4);

queue.UserPrintTicket.PageOrientation = PageOrientation.Portrait;

var writer = PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(visual);

也许queue.Commit();不见了?

        PageMediaSize pageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);
        if (printCapabilities.PageMediaSizeCapability.Contains(pageMediaSize))
        {

            PrintTicket deltaPrintTicket = new PrintTicket {PageMediaSize = pageMediaSize };

            var result =
                queue.MergeAndValidatePrintTicket(queue.UserPrintTicket,
                    deltaPrintTicket);

            if (result.ValidatedPrintTicket.PageMediaSize == pageMediaSize)
            {
                queue.UserPrintTicket = result.ValidatedPrintTicket;
                queue.Commit();
            }
        }

您必须合并并验证打印票: