【问题标题】:printing a wpf usercontrol on custom paper size在自定义纸张尺寸上打印 wpf 用户控件
【发布时间】:2015-02-16 16:20:48
【问题描述】:

我有一个具有以下属性的 wpf 用户控件:

  1. 宽度=170mm(642px)
  2. 高度=85mm(321px)

我想将它打印在上述尺寸的纸张上(宽度=170 毫米,高度=85 毫米)

我的问题是:当我打印它时,有些项目从纸上打印出来,我认为纸张尺寸默认为 Letter,如果正确,如何将其更改为高于 Width 和 Height?

我的代码如下:

        var p = new myUserControl();
        var pDoc = new System.Windows.Controls.PrintDialog();

        if (pDoc.ShowDialog().Value)
        {
            pDoc.PrintVisual(p, "MyPrint");
        }

可能需要这样的东西(这是 System.Windows.Forms.PrintDialog 的设置,但我使用 System.Windows.Controls.PrintDialog 它没有 PrinterSettings 属性):

    var printerSettings = new PrinterSettings();
    var labelPaperSize = new PaperSize { RawKind = (int)PaperKind.A6, Height = 148, Width = 105 };
    printerSettings.DefaultPageSettings.PaperSize = labelPaperSize;
    var labelPaperSource = new PaperSource { RawKind = (int)PaperSourceKind.Manual };
    printerSettings.DefaultPageSettings.PaperSource = labelPaperSource;
    if (printerSettings.CanDuplex)
    {
        printerSettings.Duplex = Duplex.Default;
    }

【问题讨论】:

    标签: c# wpf printing


    【解决方案1】:

    在 WPF 中 1 个单位 = 1/96 英寸,因此您可以使用此公式计算您的尺寸(英寸)

    您可以将 printDlg.PrintTicket.PageMediaSize 设置为 Paper 的大小,然后将窗口转换为在该区域进行打印,如下所示:

    private void _print()
     {
          PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
    
           PrintTicket pt = printDlg.PrintTicket;
           Double printableWidth = pt.PageMediaSize.Width.Value;
           Double printableHeight = pt.PageMediaSize.Height.Value;
    
           Double xScale = (printableWidth - xMargin * 2) / printableWidth;
           Double yScale = (printableHeight - yMargin * 2) / printableHeight;
    
    
            this.Transform = new MatrixTransform(xScale, 0, 0, yScale, xMargin, yMargin);
    
    
        //now print the visual to printer to fit on the one page.
         printDlg.PrintVisual(this, "Print Page");
    }
    

    【讨论】:

    • 如何将 printDlg.PrintTicket.PageMediaSize 设置为 Paper 的大小?为什么我必须转换我的用户控件以在该区域打印?
    • 是的,将您的用户控件转换为在该区域打印。
    • 我的 wpf 用户控件的宽度=642 高度=321,如果 p 是我的用户控件的一个实例,我使用 printDlg.PrintTicket.PageMediaSize = new PageMediaSize(p.Width / 96, p.Height / 96);好吗?
    • 不完整的解决方案和解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    相关资源
    最近更新 更多