WPF 打印问题

4
当我选择 Microsoft XPS 文档编写器作为打印机时,输出完美,但当我选择我的 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");
        }
    }

你尝试简化PrintInvoice了吗?看看是否有任何元素会影响HP打印机的正常工作。 - RQDQ
PrintInvoice是一个页面,它在Microsoft XPS文档编写器中的输出完美无缺。 - Pankaj Upadhyay
我检查了两种情况下 pi.ActualHeight 和 pi.ActualWidth 的值。(当选择 HP 打印机时)高度为 0,宽度为 NaN...(当选择 Microsoft XPS 文档编写器时)高度为 950,宽度为 NaN。这可能是原因吗?有什么解决方法和原因? - Pankaj Upadhyay
2个回答

8
这可能是由许多不同的原因导致的。当正确执行时,您可以添加一些步骤,这些步骤可能会使飞行员带着货物和知识回来。 首先,您应该按照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");

货物崇拜式编程出现在测量和排列步骤中。通常,如果您调用测量并传递new Size(Double.MaxValue, Double.MaxValue),那么这就是您需要做的全部。
第二个仪式涉及调度程序的“bump”。
visual.DataContext = foo;
Dispatcher.Invoke((Action)()=>{;}); // bamp
// print here

尝试这些,看看是否有帮助。


1
我曾经遇到过同样的问题,但仍然无法打印。尝试唤醒调度程序并没有改变任何东西,但是我通过在我的控件上添加UpdateLayout()方法的调用来解决了这个问题。 - glacasa

0

这个XAML在某些情况下能起到很好的效果

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

2
您的答案可以通过提供更多支持信息来改进。请编辑并添加进一步的详细信息,比如引用或文档,以便他人可以确认您的回答是否正确。您可以在帮助中心中找到有关如何撰写良好答案的更多信息。 - moken

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接