c# - WPF 打印问题

标签 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");
        }
    }

最佳答案

这可能是由多种不同的原因造成的。您可以添加几个步骤,如果正确执行,可能会 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");

cargo-cult 代码在 Measure 和 Arrange 步骤中。通常,如果您调用 Measure 并传入一个 new Size(Double.MaxValue, Double.MaxValue),这就是您需要做的所有事情。

第二个仪式涉及碰撞调度员。

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

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

关于c# - WPF 打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4794480/

相关文章:

wpf - 两个自定义依赖对象之间的绑定(bind)

c# - 工厂方法 - 未知参数

python-3.x - Python打印具有给定位数的 float

c# - UWP 直接从 Windows 应用程序打印而不带打印对话框

c# - jQuery 核心/数据或自定义属性(数据驱动)

c# - 是否存在字段优于自动属性的情况

java - 嵌套类在C#中不能调用父类,但在java中可以

c# - UWP 等待用户与 ContentDialog 交互

wpf - 附加属性的多重绑定(bind)

html - 媒体查询打印不适用于 margin-left