网格不打印内容

时间:2013-07-01 12:02:44

标签: c# wpf

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

按钮和网格代码:

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

        }


    }

2 个答案:

答案 0 :(得分:0)

也许这个问题可以帮到你。您可以查看“测量和排列”方法,可能会在那里看到。

Print Grid which generated dynamically in wpf

答案 1 :(得分:0)

此代码是否适合您打印?如果是这样,测量结果有问题。另外,不要忘记在尝试使用PrintVisual方法打印网格时必须渲染网格。

PrintDialog dialog = new PrintDialog();

if (dialog.ShowDialog() == true)
{
    dialog.PrintVisual(myGrid, "CustomDescription");
}