.Net 4.5 wpf 打印问题,纸张尺寸始终为 NorthAmericaLetter [英] .Net 4.5 wpf printing issue, paper size is always NorthAmericaLetter

查看:39
本文介绍了.Net 4.5 wpf 打印问题,纸张尺寸始终为 NorthAmericaLetter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an issue with printing in WPF. In .Net 4.5 it always prints with paper size NorthAmericaLetter. In .Net Framework 3.5 it prints on the correct paper size, which I defined in Code. I tried it in the same Solution, with the same Code and just changed the Target Framework.

我的问题是,我是否缺少 PrintTicket 配置中的某些内容,或者如何在 .Net 4.5 中正确设置纸张大小?

My question is, am I missing something in the PrintTicket configuration or how can I set the paper size in .Net 4.5 correctly?

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

推荐答案

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

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

You'll have to merge and validate the printticket:

这篇关于.Net 4.5 wpf 打印问题,纸张尺寸始终为 NorthAmericaLetter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆