0

该按钮被放置在第二个网格中。第一个网格包含我要打印的内容但是当我打印第一个网格时没有显示任何内容并且正在打印一个空白文档

按钮和网格代码:

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

        }


    }
4

2 回答 2

0

也许这个问题会对你有所帮助。您可以查看 Measure 和 Arrange 方法,可能在那里。

在 wpf 中动态生成的打印网格

于 2013-07-01T12:10:20.300 回答
0

此代码是否为您打印任何内容?如果是这样,则测量结果有问题。另外,不要忘记在您尝试使用 PrintVisual 方法打印网格时必须呈现网格。

PrintDialog dialog = new PrintDialog();

if (dialog.ShowDialog() == true)
{
    dialog.PrintVisual(myGrid, "CustomDescription");
}
于 2013-07-01T13:19:12.637 回答