简体   繁体   English

如何使用C#打印WPF用户控件?

[英]How to take the print of wpf user control using c#?

I have one user control in which i displayed the map and the data in the datagrid. 我有一个用户控件,可以在其中显示地图和数据网格中的数据。

I have tried following code but it does not works. 我试过下面的代码,但它不起作用。

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

Issue : When data grid has large number of records then user control gets scroll bar, but after printing the user control, only visible part of user control is printed and not the data present which we can see after scrolling down.I want to print whole content of user control. 问题:当数据网格中有大量记录时,用户控件会显示滚动条,但是在打印用户控件后,仅打印用户控件的可见部分,而没有向下滚动后可以看到的现有数据。我想整体打印用户控制的内容。

Is there any solution for this, also how can we see print preview in wpf ? 有没有解决的办法,我们如何在wpf中看到打印预览?

Please check the following link, it should be helpful. 请检查以下链接,它应该会有所帮助。 Print Preview WPF 打印预览WPF

Code

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM