WPF打印问题

时间:2011-01-25 14:14:58

标签: c# wpf printing

当我选择Microsoft XPS Document Writer作为打印机时,我的输出很完美,但当我选择HP 1020打印机时,打印机输出空白副本......以下是代码....

private void printButton_Click(object sender, RoutedEventArgs e)
    {
        PrintInvoice pi = new PrintInvoice();
        pi.DataContext = this.DataContext;
        PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
        if (printDlg.ShowDialog() == true)
        {
            pi.Margin = new Thickness(30);

            //now print the visual to printer to fit on the one page.
            printDlg.PrintVisual(pi, "First Fit to Page WPF Print");
        }
    }

2 个答案:

答案 0 :(得分:8)

这可能是由许多不同的事情引起的。您可以添加一些步骤,如果执行正确,可以cause the flying men to return with goods and knowledge.

首先,您应该扩展到打印页面(代码来自a2zdotnet):

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, "Code ganked from http://www.a2zdotnet.com/View.aspx?id=66");

货物信息代码在“测量和排列”步骤中。通常,如果您调用Measure并传入new Size(Double.MaxValue, Double.MaxValue),那就是您需要做的所有事情。

第二种仪式涉及撞击调度员。

visual.DataContext = foo;
Dispatcher.Invoke((Action)()=>{;}); // bamp
// print here

尝试这些并查看它是否有帮助。

答案 1 :(得分:0)

这个XAML在某些情况下可以解决问题

<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
  <... Name="myPrintElement" />
</ScrollViewer >