【问题标题】:How to take the print of wpf user control using c#?如何使用 c# 打印 wpf 用户控件?
【发布时间】:2017-10-24 13:11:52
【问题描述】:

我有一个用户控件,我在其中显示地图和数据网格中的数据。

我尝试了以下代码,但它不起作用。

        PrintDialog printDialog = new PrintDialog();

        // if user clicks on cancel button of print dialog box then no need to print the map control
        if (!(bool)printDialog.ShowDialog())
        {
            return;
        }
        else // this means that user click on the print button
        {
            // do nothing
        }

        this.Measure(new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));
        this.Arrange(new Rect(new Point(20, 20), new Size(this.ActualWidth, this.ActualHeight)));

        // print the map control
        printDialog.PrintVisual(this, "Karte drucken");

问题:当数据网格有大量记录时,用户控件获得滚动条,但打印用户控件后,只打印用户控件的可见部分,而不是我们向下滚动后可以看到的数据存在.我想打印用户控件的全部内容。

有什么解决办法吗,我们如何在 wpf 中看到打印预览?

【问题讨论】:

    标签: c# wpf printing user-controls


    【解决方案1】:

    请检查以下链接,它应该会有所帮助。 Print Preview WPF

    代码

     PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
    if (printDlg.ShowDialog() == true)
      {
       //get selected printer capabilities
       System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
    
       //get scale of the print wrt to screen of WPF visual
       double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
              this.ActualHeight);
    
       //Transform the Visual to scale
       this.LayoutTransform = new ScaleTransform(scale, scale);
    
       //get the size of the printer page
       Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
    
       //update the layout of the visual to the printer page size.
       this.Measure(sz);
       this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
    
       //now print the visual to printer to fit on the one page.
       printDlg.PrintVisual(this, "First Fit to Page WPF Print");
    
    }
    

    【讨论】:

    • 感谢swapnil的帮助,我通过使用“userControl.Arrange(new Rect(new Point(50, 50), new Size(userControl.ActualWidth, userControl.ActualHeight)))解决了这个问题; "而不是“dategrid.Arrange(new Rect(new Point(50, 50), new Size(userControl.ActualWidth, userControl.ActualHeight)));”我现在面临新问题,我更新了问题
    • @pankaj 看看这个帖子Link
    • @pankaj 也看到这篇 msdn 文章 Link 查找这些行 dlg.PrintVisual(panel.Content, "Print Button");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2013-10-10
    • 1970-01-01
    • 2016-01-20
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多