none
关于 WPF PrintDialog 设置打印纸张高度与宽度问题。 RRS feed

  • 问题

  • 问题如下:

    我通过WPF进行打印,WPF中PrintDialog默认纸张大小应该是A4,宽度是700多左右,高度是1000多。当我想要在X轴1000的位置打印文字时,总是打印不出来。因此我想通过设置打印纸张的大小来让X轴1000处的文字打印出来。代码如下。设置PageMediaSize为 ISOA0,应该是3000*4000多。但是我还是无法将X轴1000处的文字打印出来。网上搜了很久无果。因此在这里提问。希望能解决这个问题。代码如下。

     private void button_SinglePointPrint_Click(object sender, RoutedEventArgs e)
            {
                PrintDialog pDialog = new PrintDialog();
                pDialog.PrintTicket = pt;
                pDialog.PrintQueue = pq;
                pDialog.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA0);
                if (pDialog.ShowDialog() == true)
                {
                    pt = pDialog.PrintTicket;
                    pq = pDialog.PrintQueue;
                   
                    DrawingVisual vis = new DrawingVisual();
                    DrawingContext dc = vis.RenderOpen();
                   
                    dc.DrawText(pLib.GetFormattedText(textBox_SinglePointText.Text, TextAlignment.Center, new Typeface(fontName), fontSize), new Point(1000,100));
                    dc.Close();

                    pDialog.PrintVisual(vis, "TestSinglePoint");
                }
            }

    2014年4月21日 15:38

答案

  • 你好,

    你可以通过设置PrintTicket.PageMediaSizeSize,然后Transform你的窗体大小。请查看下面代码:

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

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月22日 5:45
    版主