.Net 4.5 wpf打印问题,纸张尺寸始终是NorthAmericaLetter

时间:2015-01-23 09:11:11

标签: c# .net wpf printing .net-4.5

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

我的问题是,我是否遗漏了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);

2 个答案:

答案 0 :(得分:1)

        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();
            }
        }

您必须合并并验证printticket:

答案 1 :(得分:0)

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