在C#WPF中增加页面大小PrintVisual?

时间:2015-05-13 16:15:09

标签: c# wpf printdialog

我尝试使用PrintDialog.PrintVisual进行打印,但是当页面大小超过一张打印锁定时,任何人都知道如何解决?

1 个答案:

答案 0 :(得分:2)

1。正常印刷:

与传统的窗口打印相比,WPF中的打印很容易。您需要显示PrintDialog并调用PrintDialog的PrintVisual方法。此示例已在btnPrint_OnClick事件处理程序中显示。

PrintDialog printDlg = new System.Windows.Controls.PrintDialog();

if (printDlg.ShowDialog() == true)

{

      printDlg.PrintVisual(this, "First WPF Print");

}
  1. 打印到适合窗口:
  2. 现在,如果您想让视觉效果适合打印页面,那么您必须进行更多编码。

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