该按钮被放置在第二个网格中。第一个网格包含我要打印的内容但是当我打印第一个网格时没有显示任何内容并且正在打印一个空白文档
按钮和网格代码:
Button Print = new Button();
Print.Content = "Print";
Print.Click += new RoutedEventHandler(OnPrintClick);
secondGrid.Children.Add(Print);
Grid.SetColumn(Print, 2);
Grid.SetRow(Print, 5);
ColumnDefinition myColumsecondGrid = new ColumnDefinition();
RowDefinition myRowsecondGrid = new RowDefinition();
myRowsecondGrid.Height = new GridLength(300);
myColumsecondGrid.Width = new GridLength(165);
secondGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = myColumsecondGrid.Width });
secondGrid.RowDefinitions.Add(new RowDefinition() { Height = myRow.Height });
按钮打印代码:
private void OnPrintClick(object sender, RoutedEventArgs e)
{
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
//System.Printing
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
//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.
myGrid.Measure(sz);
myGrid.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.PageRangeSelection(printQty);
//now print the visual to printer to fit on the one page.
String printerName = "PDF reDirect v2";
System.Printing.PrintQueue queue = new System.Printing.LocalPrintServer().GetPrintQueue(printerName);
printDlg.PrintQueue = queue;
printDlg.PrintVisual(myGrid, "");
}
}