在C#WPF中增加页面大小的printVisual



我尝试使用printdialog.printvisual打印,但是当页面的大小超过一个纸上以打印锁时,任何人都知道如何解决?

1。普通印刷:

与传统的窗口打印相比,在WPF中打印很容易。您需要显示printdialog并致电PrintDialog的PrintVisual Mehod。此示例已在BTNPRINT_ONCLICK事件处理程序中显示。
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
      printDlg.PrintVisual(this, "First WPF Print");
}
  1. 打印到适合窗口:

现在,如果您想适合视觉以适合打印页面,则必须进行更多编码。

•Add Reference the ReachFramework.dll.
•Add reference of the System.Printing.dll.
•Get the capabilities of the selected printer.
•Calculate the scaling of the printer with w.r.t. to visual to be printed.
•Transform the visual to be printed to the calculated scale.
•Get the printable area of the paper size.
•Update the layout of the visual to the printable area.
•Print the visual.

代码:示例中的此代码在btnprintfit_onclick处理程序中调用。

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

相关内容

  • 没有找到相关文章

最新更新